2011-07-05 89 views
0

我在QPushbuuton中設置文本時遇到問題。 我在一個按鈕上設置文本,但它永遠不會出現。QPushbutton settext在QT中的問題?

我已經爲QPushbutton的mousepress,mouserelease事件編寫了一個自定義類。

我推廣我的QPushbutton這個類。

我的代碼如下所示:

//Customclass.h

枚舉ButtonState { 正常, 鼠標懸停, 推 };

class CustomButtonStates : public QPushButton 
{ 
    Q_OBJECT 


public: 
    explicit CustomButtonStates(QWidget *parent = 0,const QString &normal = "", const QString &active = "",QString strText = ""); 
    virtual ~CustomButtonStates(); 
    void mousePressEvent(QMouseEvent *event); 
    void mouseReleaseEvent(QMouseEvent *event); 
    void paintEvent(QPaintEvent *e); 



private: 

    QString m_PixmapNormal; 
    QString m_PixmapActive; 
    QString m_strText; 
    bool m_bPressed; 
    ButtonState state; 

public: 
    QImage *NormalImage; 
    QImage *MouseOverImage; 
    QImage *PushedImage; 




}; 

//的.cpp

CustomButtonStates::CustomButtonStates(QWidget *parent,const QString &normal, const QString &active,QString strText) : 
    QPushButton(parent) 
{ 

    m_PixmapNormal = normal ; 
    m_PixmapActive = active ; 
    m_strText = strText ; 
    state = Normal; 



} 


void CustomButtonStates::mousePressEvent(QMouseEvent *event) 
{ 
    QPushButton::mousePressEvent(event); 
    state = Pushed;  
    this->repaint(); 

} 

void CustomButtonStates::mouseReleaseEvent(QMouseEvent *event) 
{ 
    QPushButton::mouseReleaseEvent(event); 
    state = Normal; 
    emit clicked(); 
    this->repaint(); 

} 

void CustomButtonStates::paintEvent(QPaintEvent *event) 
{ 
    QPainter painter(this); 
    QImage *pic = NULL; 

    NormalImage = new QImage(m_PixmapNormal); 
    PushedImage = new QImage(m_PixmapActive); 
    switch (state) 
    { 
    case Normal: 
     pic = NormalImage; 
     break; 
    case MouseOver: 
     pic = MouseOverImage; 
     break; 
    case Pushed: 
     pic = PushedImage; 
     break; 
    default: 
     pic = NormalImage; 
     break; 
    } 

    painter.drawImage(0, 0, *pic); 
} 

CustomButtonStates::~CustomButtonStates() 
{ 
    delete NormalImage; 
    delete MouseOverImage; 
    delete PushedImage; 
} 

在我的網頁:

CustomButtonStates *btnMain ; 
btnMain = new CustomButtonStates(this,":/Sampl1.png",":/Sample2.png","Users"); 
    btnMain->setText("Users"); 
    btnMain->setGeometry(QRect(3,6,65,33)); 

    connect(btnMain,SIGNAL(clicked()),this,SLOT(on_btnUsers_clicked())); 

回答

1

如果你不需要任何的塗在QPushButton除了文字和數據(圖像/像素圖)的東西,我建議你可以去得更深一些的直接繼承了QAbstractButton代替。

按鈕,您將添加一個文本成員(加上accesors setText(const QString&...)text()),然後重新實現Paint事件來繪製圖像,並在這樣的背景下與QPainterdrawText()方法之一高於去年文本。

paintEvent看起來就像這樣:

void YourButton::paintEvent(QPaintEvent* event) 
{ 
    QPainter painter(this); 
    //hereadd some logic to determine witch pixmap goes background 
    QPixmap* selectedPixmap ... 
    painter.drawPixmap(rect(), *selectedPixmap); 
    //here add some logic to set painter pen and brush 
    //according to button status (enabled/checked/...) 
    painter.drawText(rect(), Qt::AlignCenter, text()); 
} 

如果你需要像自動換行,或Qt的「CSS」的風格票友的東西應用到你的文字,我建議直接使用一個QLabel和轉發文字存取器這QLabel。在這種情況下,請不要忘記重新實現resizeEvent,以便在按鈕矩形頂部正確設置疊加標籤,並且每當它發生更改時。

0

嘗試調用QPushButton ::的paintEvent(事件)第一次在自定義的paintEvent然後應用自定義的QImage。

+0

我已經添加QPushButton :: paintevent(event)作爲我的自定義繪畫事件的最後一行現在我將圖像分配給未來的按鈕? – user662285

+0

嘗試先調用繪畫事件,然後應用您的圖像。我的錯。如果這不起作用,那麼在繪製QImage(應該是QPixmap btw)後,您將不得不自己繪製文本。 –

+0

如何繪製文本? – user662285

1

您應該在覆蓋paintEvent方法中手動繪製文本。 但是更容易 - 正如OrcunC建議的那樣,請改用Stylesheet。

QPushButton pb; 
pb.setStyleSheet("background-image: url(:/Users/nav_on.png);"); 
+0

但我創建了一個自定義類來設置鼠標按下和釋放圖像,如果我有100頁,我需要做110次...如何手動繪製文本? – user662285