2017-07-26 26 views
1

我想通過setBrush使用QBrush來設置5個QGraphicsRectItem的畫筆。但它不起作用。這種行爲很混亂,因爲它在某些情況下起作用。如何在QGraphicsScene中設置QGraphicsRectItem的畫筆

example image

代碼包括我在Qt的創建者創建了*的.ui。我已經添加了一個QGraphiocsView。

QGraphicsScene *scene_; 

scene_ = new QGraphicsScene(ui->graphicsView); 
ui->graphicsView->setScene(scene_); 

scene_->setBackgroundBrush(Qt::red); 


for (int i=0; i<5; i++) 
{ 
    QBrush tmpBrush; 
    tmpBrush.setColor(QColor(200-i*15, i*15, 50)); 
    QPen tmpPen; 
    tmpPen.setColor(Qt::blue);  tmpPen.setWidth(2); 

    QGraphicsRectItem*tmpRect = scene_->addRect(2, 25*i, 100, 20, tmpPen, tmpBrush); 
    tmpRect->setPen(tmpPen);   tmpRect->setBrush(tmpBrush); 
} 

我在Ubuntu 16.04中用QT 5.7編碼。

回答

2

你刷沒有風格:-)或者,也許更有益,你創建一個新的QBrush與...

QBrush tmpBrush; 

調用默認的構造函數,其documentation狀態

構造一個默認黑色刷與風格Qt :: NoBrush(即這個 刷不會填充形狀)。

所以,僅僅將其更改爲...

QBrush tmpBrush(Qt::SolidPattern); 

,你應該得到期望的結果。

+0

謝謝!這很有幫助,代碼現在正確運行 –