PDA

View Full Version : QGraphicsScene brush not working



gib
4th October 2018, 02:10
I've given up on trying to paint a QMdiArea, and instead I'm using the code below to paint on a scene -> view -> layout -> groupBox. Strangely, although I get a black rectangle, from the pen, the brush has no effect - I can't fill the rect.

EDIT: I found the answer:
brush.setStyle(Qt::SolidPattern);



QVBoxLayout *layout = new QVBoxLayout();
groupBox_test->setLayout(layout);
QBrush brush;
QPen pen;
brush.setColor(Qt::red);
pen.setColor(Qt::black);
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsView *aview = new QGraphicsView(scene);
layout->addWidget(aview);
scene->addRect(0,0,50,50,pen,brush);
aview->show();

d_stranz
4th October 2018, 17:33
Oh yes, there's a third way to paint onscreen without a paint event, and that's to use the graphics / view architecture. In this case, the QGraphicsView::paintEvent() is doing the work of converting the scene graph into pixels.