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!
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!