2011-10-30 45 views
4

我想畫一個滑塊的背景。我嘗試過,但顏色覆蓋了整個滑塊。這是在繼承的類的QSliderQt drawRect在後臺

void paintEvent(QPaintEvent *e) { 
    QPainter painter(this); 
    painter.begin(this); 
    painter.setBrush(/*not important*/); 

    // This covers up the control. How do I make it so the color is in 
    // the background and the control is still visible? 
    painter.drawRect(rect()); 

    painter.end(); 
} 
+1

你想要做的是繪製一個小部件的背景?請更具體一點。 –

回答

9

設置控件的背景,你可以將樣式表:

theSlider->setStyleSheet("QSlider { background-color: green; }"); 

下面我們來設置widget的背景,讓你做更多:

void paintEvent(QPaintEvent *event) { 
    QPainter painter; 
    painter.begin(this); 
    painter.fillRect(rect(), /* brush, brush style or color */); 
    painter.end(); 

    // This is very important if you don't want to handle _every_ 
    // detail about painting this particular widget. Without this 
    // the control would just be red, if that was the brush used, 
    // for instance. 
    QSlider::paintEvent(event);  
} 

而順便說一句。的示例代碼下面的兩行會產生一個警告:

QPainter painter(this); 
painter.begin(this); 

即這一個使用GCC:

了QPainter ::開始:一個塗料裝置只能由一個畫家在 塗漆時間。

所以一定要確保,因爲我在我的例子做,你要麼做QPainter painter(this)painter.begin(this)