Qt-UI
Specializing QWidget
← Events | ● | Drag and Drop →
In the following we look at a Qt example application that handles paint events to paint a text on the main widget.
First we specialize QWidget by overriding its paintEvent() method.
class MyQPainterWidget: public QWidget
{
public:
//! default ctor
MyQPainterWidget(QWidget *parent = 0)
: QWidget(parent)
{}
//! dtor
~MyQPainterWidget()
{}
protected:
// reimplemented paint event
void paintEvent(QPaintEvent *)
{
// paint events handled here
}
};
{
public:
//! default ctor
MyQPainterWidget(QWidget *parent = 0)
: QWidget(parent)
{}
//! dtor
~MyQPainterWidget()
{}
protected:
// reimplemented paint event
void paintEvent(QPaintEvent *)
{
// paint events handled here
}
};
Then we reimplement the paintEvent() method by drawing a centered text on the widget’s canvas by using the QPainter class:
#include <QPainter>
// reimplemented paint event
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::green);
painter.setFont(QFont("Arial", 100));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
// reimplemented paint event
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::green);
painter.setFont(QFont("Arial", 100));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
Access the example via WebSVN:
Checkout the Qt example via SVN:
svn co svn://schorsch.efi.fh-nuernberg.de/qt-examples/example-02
← Events | ● | Drag and Drop →