Draw on top of a non empty widget
I'm trying to understand the paintEvent function. What I want to do is to draw on top of a widget which contains different objects such as QLabel, QTextEdit, QPushButton or even other widgets. I made a quick example to demonstrate my wish. Consider the following code:
MyWidget.h
Code:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QTextEdit>
{
Q_OBJECT
protected:
public:
private:
};
#endif
MyWidget.cpp
Code:
#include <QVBoxLayout>
#include <QPainter>
#include "MyWidget.h"
MyWidget
::MyWidget(QWidget *parent
){
label
= new QLabel(text,
this);
label->setAlignment(Qt::AlignCenter);
vbox->addWidget(label);
setLayout(vbox);
}
{
painter.drawLine(100,50,300,50);
}
main.cpp
Code:
#include <QApplication>
#include "MyWidget.h"
int main(int argc, char *argv[])
{
MyWidget window;
window.resize(400,400);
window.show();
return app.exec();
}
This draws a line on my widget even if it contains a QLabel object.
Now, if I replace the QLabel with a QTextEdit covering the whole widget, the line is no longer visible. I guess the QTextEdit gets drawn on top of the line. What I'm wondering is if it's possible to force the widget to draw the line after the QTextEdit has been drawn? Or in general, is it possible to specify when a paintEvent shall occur? If this is impossible, what should I do if I want to draw on say a QTextEdit but still be able to write text?
Help much appreciated
Thanks!
Re: Draw on top of a non empty widget
Quote:
What I'm wondering is if it's possible to force the widget to draw the line after the QTextEdit has been drawn?
When the child widgets of a widget are inside a layout, the layout will decide the order of painting the child widget. When the child widgets are not in a layout, then first parent is painted and then children are painted in the order of creation.
Quote:
Or in general, is it possible to specify when a paintEvent shall occur?
Generally speaking no, but order of painting can be controlled by indirect means, like using stacked layout/stacked widgets etc.
Quote:
If this is impossible, what should I do if I want to draw on say a QTextEdit but still be able to write text?
Well you can write a custom QTextEdit and draw on top it, and put this custom TextEdit in a layout or a child widget.
Code:
{
public:
protected:
{
painter.drawLine(100, 50, 300, 150);
}
};
int main(int argc, char *argv[])
{
TextEdit window;
window.resize(400,400);
window.show();
return app.exec();
}
Re: Draw on top of a non empty widget
Exactly the kind of answer I was hoping for. Thanks a lot Santosh Reddy! Custom made objects will be perfect in my application. Thanks for letting me know how to create them!
Problem SOLVED