2012-08-10 10 views
3

用下面的代碼我試圖使用QStyle.drawControl()呈現一個紅色的按鈕:如何使用QStyle.drawControl()呈現紅色按鈕?

#include <QtCore/QtCore> 
#include <QtGui/QtGui> 

class Widget : public QWidget 
{ 
    virtual void paintEvent(QPaintEvent* event) 
    { 
     QStyleOptionButton opt; 
     opt.palette = QPalette(Qt::red); 
     opt.state = QStyle::State_Active | QStyle::State_Enabled; 
     opt.rect = QRect(50, 25, 100, 50); 
     QPainter painter(this); 
     style()->drawControl(QStyle::CE_PushButton, &opt, &painter); 
    } 
}; 

int main(int argc, char** argv) 
{ 
    QApplication app(argc, argv); 
    Widget w; 
    w.resize(200, 100); 
    w.show(); 
    return app.exec(); 
} 

不過,我得到以下結果:

enter image description here

如何渲染使用QStyle.drawControl()一個紅色的按鈕?

我使用Qt 4.8.1和Windows XP Visal Studio 2010中。

+0

你設置這個按鈕一些文字?也許「Qt的::紅色」正在下降成文本顏色,背景/幀顏色 – 2012-08-10 13:07:58

+0

@Kamil Klimek:「QPalette :: QPalette(QT :: GlobalColor按鈕)」的文檔的狀態「構造從按鈕顏色的調色板。其他顏色將根據此顏色自動計算。窗口也是按鈕的顏色。請參閱http://qt-project.org/doc/qt-4.8/qpalette.html#QPalette-3。 – user763305 2012-08-10 16:40:17

+0

@Kamil Klimek:我還測試了構建調色板的其他幾種方式,但似乎不管調色板是什麼,該按鈕結束有相同的顏色。 – user763305 2012-08-10 16:41:50

回答

6

的按鈕由本地的樣式引擎繪製,所以調色板可能不會在所有使用(請參閱從FAQ that question)。

您可以使用一個實際的按鈕,您傳遞的最後一個參數給自己的按鈕的樣式drawControl功能的樣式表。

class Widget : public QWidget 
{ 
    // To allow the automatic deletion without parenting it 
    QScopedPointer<QPushButton> button; 
public: 
    Widget() : button(new QPushButton) { 
     button->setStyleSheet("background-color: red"); 
    } 
    virtual void paintEvent(QPaintEvent* event) 
    { 
     QStyleOptionButton opt; 
     opt.state = QStyle::State_Active | QStyle::State_Enabled; 
     opt.rect = QRect(50, 25, 100, 50); 
     QPainter painter(this); 
     button->style()->drawControl(QStyle::CE_PushButton, &opt, &painter, 
            button.data()); 
    } 
}; 

但是你將失去本地的樣式,所以你必須捏造事實(bali182's answer可能是那部分有用)。

或者你也可以用彩色化效果使用同一個按鈕並調用其render()功能把它漆成:

Colorized Button

class Widget : public QWidget { 
    QScopedPointer<QPushButton> button; 
public: 
    Widget() : button(new QPushButton) { 
     QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(button.data()); 
     effect->setColor(Qt::red); 
     button->setGraphicsEffect(effect); 
    } 
    virtual void paintEvent(QPaintEvent* event) { 
     button->setFixedSize(100, 50); 
     button->render(this, QPoint(50, 25)); 
    } 
}; 
+0

謝謝!很好的答案! – user763305 2012-08-11 07:02:19

3

你正在嘗試做的,似乎過於複雜。如果你只是想要一個紅色的按鈕,爲什麼不使用QPushButton的setStyleSheet()方法?它需要一個QString,並且你可以定義類似於CSS的按鈕。在這裏,我創造了你一個紅色的按鈕,類似於XP用戶界面按鈕:

QPushButton 
{ 
    background: qlineargradient(x1:0,y1:0,x2:0,y2:1, stop:0 #f4a3a3,stop: 1 #cc1212); 
    border-width: 1px; 
    border-color: #d91414; 
    border-style: solid; 
    padding: 5px; 
    padding-left:10px; 
    padding-right:10px; 
    border-radius: 3px; 
    color:#000; 
} 

QPushButton:hover 
{ 
    border-color: #e36666; 
} 

QPushButton:pressed 
{ 
    background:qlineargradient(x1:0,y1:0,x2:0,y2:1,stop: 0 #de8383, stop: 1 #ad0C0C); 
    border-color: #d91414; 
} 

現在你只需要爲一個字符串通過上面的代碼到你的按鈕setStyleSheet()方法。如果你想創建一個按鈕控件,什麼是默認的紅色,然後擴展QPushButton類,創建上面的內容靜態的QString場,並設置按鈕,在構造樣式表。

更容易理解的例子樣式: Stylesheet Examples

+0

我通過將alexisdm的答案與您的樣式表結合起來解決了這個問題。 – user763305 2012-08-11 08:09:07