C-Programmierung

Qt Spezialisierung

Compilation eines Qt Projekts mit CMake | | Qt App Menu

Now we create an application that will simply open a plain window containing a single widget, that is a QWidget without any functionality yet.

We run the application by returning execution control to its event loop:

#include <QtGui/QApplication>
#include <QtGui/QWidget>

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);

   QWidget main;
   main.show();

   return(app.exec());
}

Then we derive from QWidget to specialize its paintEvent() method:

#include <QWidget>
#include <QPainter>

class MyQPainterWidget: public QWidget
{
public:

   //! default ctor
   MyQPainterWidget(QWidget *parent = 0)
      : QWidget(parent)
   {}

   //! dtor
   ~MyQPainterWidget()
   {}

protected:

   //! reimplemented paint event method
   void paintEvent(QPaintEvent *)
   {
      QPainter painter(this);

      painter.setPen(Qt::green);
      painter.setFont(QFont("Arial", 100));
      painter.drawText(rect(), Qt::AlignCenter, "Qt");
   }

};

The reimplemented paintEvent method draws a centered text by using the QPainter class.

Now we replace the main application widget with our customized widget:

#include "MyQPainterWidget.h"

MyQPainterWidget main;
main.show();
QPainter


Compilation eines Qt Projekts mit CMake | | Qt App Menu

Options: