Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My QOpenGLWidget is being created, but it doesn't work
Forum Update on Monday, May 27th 2025

My QOpenGLWidget is being created, but it doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 296 Views
  • 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.
  • mrjbomM Offline
    mrjbomM Offline
    mrjbom
    wrote on last edited by mrjbom
    #1

    Hello.

    I have a simple form in which I add an OpenGLWidget, and then set my "MyOpenGLWidget" class to the Promoted Class,
    Scr28-01-2021 161103.png
    it is successfully created, but is not displayed.
    Its constructor is called, but the initializeGL, resizeGL, and paintGL functions are not called.
    3443c732-ed35-489b-8866-aa426e641184-image.png
    Project file: HERE

    Here is all my code:
    main.cpp

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

    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;
    }
    

    ui_mainwindow.h

    class Ui_MainWindow
    {
    public:
        QWidget *centralwidget;
        MyOpenGLWidget *openGLWidget;
    
        void setupUi(QMainWindow *MainWindow)
        {
            if (MainWindow->objectName().isEmpty())
                MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
            MainWindow->resize(420, 300);
            centralwidget = new QWidget(MainWindow);
            centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
            openGLWidget = new MyOpenGLWidget(centralwidget);
            openGLWidget->setObjectName(QString::fromUtf8("openGLWidget"));
            openGLWidget->setGeometry(QRect(60, 50, 300, 200));
            MainWindow->setCentralWidget(centralwidget);
    
            retranslateUi(MainWindow);
    
            QMetaObject::connectSlotsByName(MainWindow);
        } // setupUi
    
        void retranslateUi(QMainWindow *MainWindow)
        {
            MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
        } // retranslateUi
    
    };
    
    namespace Ui {
        class MainWindow: public Ui_MainWindow {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    

    myopenglwidget.cpp

    #include "myopenglwidget.h"
    #include "globalvars.h"
    #include "gl/GLU.h"
    
    MyOpenGLWidget::MyOpenGLWidget(QWidget* parent)
    {
        qInfo() << "MyOpenGLWidget called";
    }
    
    MyOpenGLWidget::~MyOpenGLWidget()
    {
        qInfo() << "~MyOpenGLWidget called";
    }
    
    void MyOpenGLWidget::initializeGL()
    {
        qInfo() << "initializeGL called!";
        initializeOpenGLFunctions();
        globalOpenGLContext = this->context();
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(-this->width() / 2, this->width() / 2, -this->height() / 2, this->height() / 2);
        glViewport(0, 0, this->width(), this->height());
    
    }
    
    void MyOpenGLWidget::resizeGL(int w, int h)
    {
        qInfo() << "resizeGL called!";
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(-w / 2, w / 2, -h / 2, h / 2);
        glViewport(0, 0, w, h);
    }
    
    void MyOpenGLWidget::paintGL()
    {
        qInfo() << "paintGL called!";
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glColor3f(1, 1, 1);
        glPushMatrix();
        glBegin(GL_POLYGON);
        glVertex3f(-100, 100, 0);
        glVertex3f(100, 100, 0);
        glVertex3f(100, -100, 0);
        glVertex3f(-100, -100, 0);
        glEnd();
        glPopMatrix();
    
        update();
    }
    

    myopenglwidget.h

    #ifndef MYOPENGLWIDGET_H
    #define MYOPENGLWIDGET_H
    
    #include <QDebug>
    #include <QOpenGLWidget>
    #include <qopenglfunctions_3_3_core.h>
    #include <gl/GLU.h>
    
    class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
    {
        Q_OBJECT
    public:
        MyOpenGLWidget(QWidget *parent = nullptr);
        ~MyOpenGLWidget();
    
    protected:
        void initializeGL();
        void resizeGL(int w, int h);
        void paintGL();
    };
    
    #endif // MYOPENGLWIDGET_H
    
    jsulmJ 1 Reply Last reply
    0
    • mrjbomM mrjbom

      Hello.

      I have a simple form in which I add an OpenGLWidget, and then set my "MyOpenGLWidget" class to the Promoted Class,
      Scr28-01-2021 161103.png
      it is successfully created, but is not displayed.
      Its constructor is called, but the initializeGL, resizeGL, and paintGL functions are not called.
      3443c732-ed35-489b-8866-aa426e641184-image.png
      Project file: HERE

      Here is all my code:
      main.cpp

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

      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;
      }
      

      ui_mainwindow.h

      class Ui_MainWindow
      {
      public:
          QWidget *centralwidget;
          MyOpenGLWidget *openGLWidget;
      
          void setupUi(QMainWindow *MainWindow)
          {
              if (MainWindow->objectName().isEmpty())
                  MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
              MainWindow->resize(420, 300);
              centralwidget = new QWidget(MainWindow);
              centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
              openGLWidget = new MyOpenGLWidget(centralwidget);
              openGLWidget->setObjectName(QString::fromUtf8("openGLWidget"));
              openGLWidget->setGeometry(QRect(60, 50, 300, 200));
              MainWindow->setCentralWidget(centralwidget);
      
              retranslateUi(MainWindow);
      
              QMetaObject::connectSlotsByName(MainWindow);
          } // setupUi
      
          void retranslateUi(QMainWindow *MainWindow)
          {
              MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
          } // retranslateUi
      
      };
      
      namespace Ui {
          class MainWindow: public Ui_MainWindow {};
      } // namespace Ui
      
      QT_END_NAMESPACE
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
      };
      #endif // MAINWINDOW_H
      

      myopenglwidget.cpp

      #include "myopenglwidget.h"
      #include "globalvars.h"
      #include "gl/GLU.h"
      
      MyOpenGLWidget::MyOpenGLWidget(QWidget* parent)
      {
          qInfo() << "MyOpenGLWidget called";
      }
      
      MyOpenGLWidget::~MyOpenGLWidget()
      {
          qInfo() << "~MyOpenGLWidget called";
      }
      
      void MyOpenGLWidget::initializeGL()
      {
          qInfo() << "initializeGL called!";
          initializeOpenGLFunctions();
          globalOpenGLContext = this->context();
          glClearColor(0.0, 0.0, 0.0, 0.0);
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          gluOrtho2D(-this->width() / 2, this->width() / 2, -this->height() / 2, this->height() / 2);
          glViewport(0, 0, this->width(), this->height());
      
      }
      
      void MyOpenGLWidget::resizeGL(int w, int h)
      {
          qInfo() << "resizeGL called!";
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          gluOrtho2D(-w / 2, w / 2, -h / 2, h / 2);
          glViewport(0, 0, w, h);
      }
      
      void MyOpenGLWidget::paintGL()
      {
          qInfo() << "paintGL called!";
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      
          glColor3f(1, 1, 1);
          glPushMatrix();
          glBegin(GL_POLYGON);
          glVertex3f(-100, 100, 0);
          glVertex3f(100, 100, 0);
          glVertex3f(100, -100, 0);
          glVertex3f(-100, -100, 0);
          glEnd();
          glPopMatrix();
      
          update();
      }
      

      myopenglwidget.h

      #ifndef MYOPENGLWIDGET_H
      #define MYOPENGLWIDGET_H
      
      #include <QDebug>
      #include <QOpenGLWidget>
      #include <qopenglfunctions_3_3_core.h>
      #include <gl/GLU.h>
      
      class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
      {
          Q_OBJECT
      public:
          MyOpenGLWidget(QWidget *parent = nullptr);
          ~MyOpenGLWidget();
      
      protected:
          void initializeGL();
          void resizeGL(int w, int h);
          void paintGL();
      };
      
      #endif // MYOPENGLWIDGET_H
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mrjbom said in My QOpenGLWidget is being created, but it doesn't work:

      void initializeGL();
      void resizeGL(int w, int h);
      void paintGL();

      You should change this to

      void initializeGL() override;
      void resizeGL(int w, int h) override;
      void paintGL() override;
      

      to make sure you're really overriding.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mrjbomM 1 Reply Last reply
      0
      • jsulmJ jsulm

        @mrjbom said in My QOpenGLWidget is being created, but it doesn't work:

        void initializeGL();
        void resizeGL(int w, int h);
        void paintGL();

        You should change this to

        void initializeGL() override;
        void resizeGL(int w, int h) override;
        void paintGL() override;
        

        to make sure you're really overriding.

        mrjbomM Offline
        mrjbomM Offline
        mrjbom
        wrote on last edited by
        #3

        @jsulm, I added these changes to myopenglwidget.h, it didn't help.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You should put it in a layout inside the centralwidget.

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

          mrjbomM 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            You should put it in a layout inside the centralwidget.

            mrjbomM Offline
            mrjbomM Offline
            mrjbom
            wrote on last edited by
            #5

            @SGaist @jsulm
            Hi. The problem was that in myopenglwidget.cpp I didn't pass "parent" to the QOpenGLWidget() constructor.
            Thanks.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved