Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved why overriding paintEvent in MainWindow doesn't work

    General and Desktop
    3
    3
    58
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      shiner last edited by

      I just started learning QT. I want overriding the function paintEvent of MainWindow to draw some graphics, But it doesn't work. Thanks for your help.

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QPainterPath>
      #include <QPainter>
      #include <QPaintEvent>
      #include <iostream>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      protected:
          void paintEvent(QPaintEvent *event);
      
      private:
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::paintEvent(QPaintEvent *event)
      {
          auto rect = event->rect();
          QPainter painter(this);
          painter.setPen(QPen(Qt::red,5, Qt::DashDotLine, Qt::RoundCap));
          QFont font;
          font.setPixelSize(140);
          painter.setFont(font);
          painter.drawText(rect,Qt::AlignRight, "display");
      
          std::cout << "here" << std::endl;
      }
      
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
      
          w.setWindowTitle("main");
      
          w.show();
      
          return a.exec();
      }
      
      

      Nothing in the main windows when I run the code.

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Works fine for me - a big red text 'display' is drawn on the mainwindow.

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi and welcome to devnet,

          Since you have a ui file, any chances you have a widget set as central widget of your QMainWindow based class ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • First post
            Last post