I have QVBoxLayout
with multiple children and I want to be able to draw on top of it. I've tried implementing paintEvent(QPaintEvent *)
for the layout but everything I draw stays under the children. How can I do it? I'd be thankful for sample code.
Asked
Active
Viewed 2,350 times
4

Sebastian Nowak
- 5,607
- 8
- 67
- 107
1 Answers
4
Layouts don't have paintEvent
member so you can't reimplement it. I'm surprised you managed to get some effect from this action.
- Add new
QWidget
(let's call it wrapper) into your form and add yourQVBoxLayout
into this widget. - Create another widget (overlay) and add it to the wrapper using
setParent()
, not adding it into layout. - Reimplement overlay's paintEvent or add some other widgets to the overlay.
Simple example (tested):
class MyWidget : public QWidget {
public:
void paintEvent(QPaintEvent *e) {
QWidget::paintEvent(e);
QPainter p(this);
p.fillRect(4, 4, 30, 30, QBrush(Qt::red));
}
};
QWidget* wrapper = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(wrapper);
layout->addWidget(new QLabel("test1"));
layout->addWidget(new QLabel("test2"));
MyWidget* overlay = new MyWidget();
overlay->setParent(wrapper);
wrapper->show();

Pavel Strakhov
- 39,123
- 5
- 88
- 127