2011-10-30 7 views
4

スライダの背景をペイントしたい。私はこれを試しましたが、色はスライダー全体をカバーします。あなたがすることができ、以下はウィジェットの背景を設定しますQt drawRect in background

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

:これは、スタイルシートを設定することができ、ウィジェットの背景を設定するにはQSlider

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

の継承されたクラスであります詳細:

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);  
} 

とbtw。 GCCを使用して

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

はすなわち、この1:

QPainterの::開始:ペイントデバイスのみ Aで1人の画家によって塗装することができ、あなたのサンプルコードの次の2行は警告を得られます時間。

私の例では、QPainter painter(this)またはpainter.begin(this)のいずれかを実行するようにしてください。