我有QGraphicsScene
。在那个场景中,我有一个rects,我想在那个rects中设置不同的颜色。
我的代码是
QGraphicsScene* scene;
scene = new QGraphicsScene(this);
QGraphicsView* view;
view = new QGraphicsView(this);
view->setGeometry(x, y, mapColumns * min_height_size, mapRows * min_width_size);
view->setScene(scene);
for(int i=0; i<mapRows; ++i){
for(int j=0; j<mapColumns; ++j){
if (j == 0) {
y = 20;
}
if(map.getCell(i,j).getTerrain() == Cell::forest) {
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::green); //but it didn't work
y += min_width_size;
} else if(map.getCell(i,j).getTerrain() == Cell::mountain){
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::blue); //but it didn't work
y += min_width_size;
} else if(map.getCell(i,j).getTerrain() == Cell::plain) {
scene->addRect(x,y,min_height_size,min_width_size);
scene->setPalette(Qt::yellow); //but it didn't work
y += min_width_size;
}
view->setDragMode(QGraphicsView::ScrollHandDrag);
}
x += min_height_size;
}
}
答案 0 :(得分:0)
您可以使用setBrush()
功能设置颜色。
QGraphicsRectItem *rect_item1 = scene->addRect(100, 100, 50, 50);
rect_item1->setBrush(Qt::yellow);
QGraphicsRectItem *rect_item2 = scene->addRect(200, 250, 50, 50);
rect_item2->setBrush(QColor(100,50,200));