PDA

View Full Version : Draw on top of a non empty widget



Ponnytail
6th November 2013, 17:29
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

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QTextEdit>

class MyWidget : public QWidget
{
Q_OBJECT
protected:
void paintEvent(QPaintEvent *);
public:
MyWidget(QWidget *parent = 0);
private:
QLabel *label;
};

#endif
MyWidget.cpp

#include <QVBoxLayout>
#include <QPainter>
#include "MyWidget.h"

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QString text = "Some random text";
label = new QLabel(text, this);
label->setAlignment(Qt::AlignCenter);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addWidget(label);
setLayout(vbox);
}

void MyWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawLine(100,50,300,50);
}

main.cpp

#include <QApplication>
#include "MyWidget.h"

int main(int argc, char *argv[])
{
QApplication app(argc, 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!

Santosh Reddy
6th November 2013, 18:50
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.


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.


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.


class TextEdit : public QTextEdit
{
public:
explicit TextEdit(QWidget * parent = 0) : QTextEdit(parent) { }
protected:
void paintEvent(QPaintEvent * event)
{
QTextEdit::paintEvent(event);
QPainter painter(this->viewport());
painter.drawLine(100, 50, 300, 150);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TextEdit window;
window.resize(400,400);
window.show();
return app.exec();
}

Ponnytail
6th November 2013, 19:28
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