I basically want to display a rectangle on a dialog window widget. Using another question as reference, I tried to adapt the framework of using a QLabel and painting to it (the process overall seems overly complicated).
I started by making a member in the dialog box’s class:
QLabel* label;
In the constructor of the dialog box:
label = new QLabel(this);
label->setGeometry(20, 50, 50, 100);
Just to try and make it work, I gave the dialog box a button to make the “rectangle” created with the label appear on the widget. I connected the “pressed” signal of this button to a slot which does the following:
QPixmap pixmap(50, 100);
pixmap.fill(QColor("transparent"));
QPainter painter(&pixmap);
painter.setBrush(QBrush(Qt::black));
painter.drawRect(20, 50, 50, 100);
label->setPixmap(pixmap);
update();
Unfortunately, nothing appears in the widget when I press the button. What am I missing here?
I tried this with PyQt and it generally works, but I’m not 100% sure about the procedure. Maybe you should try calling
painter.end()the painter before callingsetPixmap(). Also, I’m not sure if one is supposed to draw onto a QPixmap outside ofQWidget:paintEvent, it might be safer to draw a QImage and create a QPixmap from it.For simply drawing a rectangle this is certainly very complicated. I don’t know what you are planning, be aware that there is QGraphicsView for drawing figures.