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. Is it possible to add GUI for QOpenGLWindow? The button should be in the center of the OpenGL canvas.
Forum Updated to NodeBB v4.3 + New Features

Is it possible to add GUI for QOpenGLWindow? The button should be in the center of the OpenGL canvas.

Scheduled Pinned Locked Moved Solved General and Desktop
40 Posts 4 Posters 6.7k Views 2 Watching
  • 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.
  • 8 8Observer8
    30 Mar 2023, 16:26

    @JoeCFD what do you mean? Can I create a side panel with GUI? How?

    J Offline
    J Offline
    JoeCFD
    wrote on 30 Mar 2023, 16:31 last edited by
    #5

    @8Observer8 you can create a QWidget with a layout and put your OpenGL Window in it.
    Then use QWidget as parent to create for example a button. Place the button(not into the layout) where you want. It can be on top of your OpenGL Window.

    8 1 Reply Last reply 30 Mar 2023, 16:38
    1
    • J JoeCFD
      30 Mar 2023, 16:31

      @8Observer8 you can create a QWidget with a layout and put your OpenGL Window in it.
      Then use QWidget as parent to create for example a button. Place the button(not into the layout) where you want. It can be on top of your OpenGL Window.

      8 Offline
      8 Offline
      8Observer8
      wrote on 30 Mar 2023, 16:38 last edited by 8Observer8
      #6

      @JoeCFD But actually my main goal is to create a game GUI. For example, create a button on top of the graphics. Is it possible?

      For example, a button:

      4af2c5e8-7fbd-4629-b99b-1fd12b2f6a88-image.png

      main.cpp

      /* .pro file:
      QT += core gui openglwidgets widgets
      win32: LIBS += -lopengl32
      SOURCES += main.cpp
      TARGET = app
      -------------------------------
      Build and run commands for CMD:
      > qmake -makefile
      > mingw32-make
      > "release/app"
      */
      
      #ifdef _WIN32
      #include <windows.h>
      extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
      extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
      #endif
      
      #include <iostream>
      
      #include <QtCore/QSize>
      #include <QtGui/QOpenGLFunctions>
      #include <QtOpenGLWidgets/QOpenGLWidget>
      #include <QtWidgets/QApplication>
      #include <QtWidgets/QPushButton>
      #include <QtWidgets/QVBoxLayout>
      
      class OpenGLWidget : public QOpenGLWidget, QOpenGLFunctions
      {
          Q_OBJECT
      
      public:
      
          OpenGLWidget()
          {
              setFixedSize(QSize(350, 350));
              setWindowTitle("OpenGL21 Qt6 C++");
      
              QVBoxLayout *vbox = new QVBoxLayout();
              QPushButton *startButton = new QPushButton("Start Game");
              startButton->setFixedSize(QSize(120, 50));
              QHBoxLayout *startButtonLayout = new QHBoxLayout();
              startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
              startButtonLayout->addWidget(startButton);
              vbox->addLayout(startButtonLayout);
              setLayout(vbox);
              // Set font for the start button
              QFont font = startButton->font();
              font.setPointSize(14);
              startButton->setFont(font);
              // Set style for the start button
              startButton->setObjectName("startButton");
              setStyleSheet(
                  "QPushButton#startButton {"
                  "    background-color: yellow;"
                  "    border-radius: 10px;"
                  "}"
                  "QPushButton#startButton:pressed {"
                  "    background-color: red;"
                  "    border-radius: 10px;"
                  "}");
              connect(startButton, &QPushButton::clicked,
                  this, &OpenGLWidget::startButtonClick);
          }
      
      private slots:
      
          void startButtonClick()
          {
              std::cout << "start button click" << std::endl;
          }
      
      private:
      
          void initializeGL() override
          {
              initializeOpenGLFunctions();
              glClearColor(0.1f, 0.3f, 0.2f, 1.f);
          }
      
          void resizeGL(int w, int h) override
          {
              glViewport(0, 0, w, h);
          }
      
          void paintGL() override
          {
              glClear(GL_COLOR_BUFFER_BIT);
          }
      };
      
      #include "main.moc"
      
      int main(int argc, char *argv[])
      {
      #ifdef _WIN32
          if (AttachConsole(ATTACH_PARENT_PROCESS))
          {
              freopen("CONOUT$", "w", stdout);
              freopen("CONOUT$", "w", stderr);
          }
      #endif
          std::cout << std::endl;
      
          QApplication app(argc, argv);
          OpenGLWidget w;
          w.show();
          return app.exec();
      }
      
      ? J 2 Replies Last reply 30 Mar 2023, 16:46
      0
      • 8 8Observer8
        30 Mar 2023, 16:38

        @JoeCFD But actually my main goal is to create a game GUI. For example, create a button on top of the graphics. Is it possible?

        For example, a button:

        4af2c5e8-7fbd-4629-b99b-1fd12b2f6a88-image.png

        main.cpp

        /* .pro file:
        QT += core gui openglwidgets widgets
        win32: LIBS += -lopengl32
        SOURCES += main.cpp
        TARGET = app
        -------------------------------
        Build and run commands for CMD:
        > qmake -makefile
        > mingw32-make
        > "release/app"
        */
        
        #ifdef _WIN32
        #include <windows.h>
        extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
        extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
        #endif
        
        #include <iostream>
        
        #include <QtCore/QSize>
        #include <QtGui/QOpenGLFunctions>
        #include <QtOpenGLWidgets/QOpenGLWidget>
        #include <QtWidgets/QApplication>
        #include <QtWidgets/QPushButton>
        #include <QtWidgets/QVBoxLayout>
        
        class OpenGLWidget : public QOpenGLWidget, QOpenGLFunctions
        {
            Q_OBJECT
        
        public:
        
            OpenGLWidget()
            {
                setFixedSize(QSize(350, 350));
                setWindowTitle("OpenGL21 Qt6 C++");
        
                QVBoxLayout *vbox = new QVBoxLayout();
                QPushButton *startButton = new QPushButton("Start Game");
                startButton->setFixedSize(QSize(120, 50));
                QHBoxLayout *startButtonLayout = new QHBoxLayout();
                startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                startButtonLayout->addWidget(startButton);
                vbox->addLayout(startButtonLayout);
                setLayout(vbox);
                // Set font for the start button
                QFont font = startButton->font();
                font.setPointSize(14);
                startButton->setFont(font);
                // Set style for the start button
                startButton->setObjectName("startButton");
                setStyleSheet(
                    "QPushButton#startButton {"
                    "    background-color: yellow;"
                    "    border-radius: 10px;"
                    "}"
                    "QPushButton#startButton:pressed {"
                    "    background-color: red;"
                    "    border-radius: 10px;"
                    "}");
                connect(startButton, &QPushButton::clicked,
                    this, &OpenGLWidget::startButtonClick);
            }
        
        private slots:
        
            void startButtonClick()
            {
                std::cout << "start button click" << std::endl;
            }
        
        private:
        
            void initializeGL() override
            {
                initializeOpenGLFunctions();
                glClearColor(0.1f, 0.3f, 0.2f, 1.f);
            }
        
            void resizeGL(int w, int h) override
            {
                glViewport(0, 0, w, h);
            }
        
            void paintGL() override
            {
                glClear(GL_COLOR_BUFFER_BIT);
            }
        };
        
        #include "main.moc"
        
        int main(int argc, char *argv[])
        {
        #ifdef _WIN32
            if (AttachConsole(ATTACH_PARENT_PROCESS))
            {
                freopen("CONOUT$", "w", stdout);
                freopen("CONOUT$", "w", stderr);
            }
        #endif
            std::cout << std::endl;
        
            QApplication app(argc, argv);
            OpenGLWidget w;
            w.show();
            return app.exec();
        }
        
        ? Offline
        ? Offline
        A Former User
        wrote on 30 Mar 2023, 16:46 last edited by
        #7

        @8Observer8 I never tried integrating OpenGL into Qt GUI but my first feeling is that this is what QOpenGLWindow is intended for.

        8 1 Reply Last reply 30 Mar 2023, 17:36
        0
        • 8 8Observer8
          30 Mar 2023, 16:38

          @JoeCFD But actually my main goal is to create a game GUI. For example, create a button on top of the graphics. Is it possible?

          For example, a button:

          4af2c5e8-7fbd-4629-b99b-1fd12b2f6a88-image.png

          main.cpp

          /* .pro file:
          QT += core gui openglwidgets widgets
          win32: LIBS += -lopengl32
          SOURCES += main.cpp
          TARGET = app
          -------------------------------
          Build and run commands for CMD:
          > qmake -makefile
          > mingw32-make
          > "release/app"
          */
          
          #ifdef _WIN32
          #include <windows.h>
          extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
          extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
          #endif
          
          #include <iostream>
          
          #include <QtCore/QSize>
          #include <QtGui/QOpenGLFunctions>
          #include <QtOpenGLWidgets/QOpenGLWidget>
          #include <QtWidgets/QApplication>
          #include <QtWidgets/QPushButton>
          #include <QtWidgets/QVBoxLayout>
          
          class OpenGLWidget : public QOpenGLWidget, QOpenGLFunctions
          {
              Q_OBJECT
          
          public:
          
              OpenGLWidget()
              {
                  setFixedSize(QSize(350, 350));
                  setWindowTitle("OpenGL21 Qt6 C++");
          
                  QVBoxLayout *vbox = new QVBoxLayout();
                  QPushButton *startButton = new QPushButton("Start Game");
                  startButton->setFixedSize(QSize(120, 50));
                  QHBoxLayout *startButtonLayout = new QHBoxLayout();
                  startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                  startButtonLayout->addWidget(startButton);
                  vbox->addLayout(startButtonLayout);
                  setLayout(vbox);
                  // Set font for the start button
                  QFont font = startButton->font();
                  font.setPointSize(14);
                  startButton->setFont(font);
                  // Set style for the start button
                  startButton->setObjectName("startButton");
                  setStyleSheet(
                      "QPushButton#startButton {"
                      "    background-color: yellow;"
                      "    border-radius: 10px;"
                      "}"
                      "QPushButton#startButton:pressed {"
                      "    background-color: red;"
                      "    border-radius: 10px;"
                      "}");
                  connect(startButton, &QPushButton::clicked,
                      this, &OpenGLWidget::startButtonClick);
              }
          
          private slots:
          
              void startButtonClick()
              {
                  std::cout << "start button click" << std::endl;
              }
          
          private:
          
              void initializeGL() override
              {
                  initializeOpenGLFunctions();
                  glClearColor(0.1f, 0.3f, 0.2f, 1.f);
              }
          
              void resizeGL(int w, int h) override
              {
                  glViewport(0, 0, w, h);
              }
          
              void paintGL() override
              {
                  glClear(GL_COLOR_BUFFER_BIT);
              }
          };
          
          #include "main.moc"
          
          int main(int argc, char *argv[])
          {
          #ifdef _WIN32
              if (AttachConsole(ATTACH_PARENT_PROCESS))
              {
                  freopen("CONOUT$", "w", stdout);
                  freopen("CONOUT$", "w", stderr);
              }
          #endif
              std::cout << std::endl;
          
              QApplication app(argc, argv);
              OpenGLWidget w;
              w.show();
              return app.exec();
          }
          
          J Offline
          J Offline
          JoeCFD
          wrote on 30 Mar 2023, 16:55 last edited by JoeCFD
          #8

          @8Observer8 Yes, you can. But I would not add button to the OpenGLWidget. Instead, create a QWidget, for example;

          class DisplayWidget : public QWidget
          {
              explicit DisplayWidget( QWidget * parent=nullptr ) : QWidget( parent ) {
                      make a layout and add m_openGLWidget;
                      m_startGameButton = new QPushButton( QString( "Start Game" ), this );
                     /* do not add it to the layout */ 
                     m_startGameButton->move( center);
              }
          
          private:
                OpenGLWidget * m_openGLWidget{};
                QPushButton * m_startGameButton{}; 
          }
          

          After button click, either hide it(for reuse) or destroy it(not needed anymore)
          separate all controls from OpenGLWidget and make OpenGLWidget handling drawing only. Otherwise, the code looks a bit messy.

          8 2 Replies Last reply 30 Mar 2023, 18:24
          0
          • ? A Former User
            30 Mar 2023, 16:46

            @8Observer8 I never tried integrating OpenGL into Qt GUI but my first feeling is that this is what QOpenGLWindow is intended for.

            8 Offline
            8 Offline
            8Observer8
            wrote on 30 Mar 2023, 17:36 last edited by
            #9

            @ankou29666 said in Is it possible to add GUI for QOpenGLWindow?:

            I never tried integrating OpenGL into Qt GUI but my first feeling is that this is what QOpenGLWindow is intended for.

            But the developers removed the dependency on the widgets module for better performance: https://doc.qt.io/qt-6/qopenglwindow.html

            QOpenGLWindow is an enhanced QWindow that allows easily creating windows that perform OpenGL rendering using an API that is compatible with QOpenGLWidget Unlike QOpenGLWidget, QOpenGLWindow has no dependency on the widgets module and offers better performance.

            For example, QOpenGLWindow doesn't have the setLayout method.

            ? 1 Reply Last reply 30 Mar 2023, 17:53
            0
            • 8 8Observer8
              30 Mar 2023, 17:36

              @ankou29666 said in Is it possible to add GUI for QOpenGLWindow?:

              I never tried integrating OpenGL into Qt GUI but my first feeling is that this is what QOpenGLWindow is intended for.

              But the developers removed the dependency on the widgets module for better performance: https://doc.qt.io/qt-6/qopenglwindow.html

              QOpenGLWindow is an enhanced QWindow that allows easily creating windows that perform OpenGL rendering using an API that is compatible with QOpenGLWidget Unlike QOpenGLWidget, QOpenGLWindow has no dependency on the widgets module and offers better performance.

              For example, QOpenGLWindow doesn't have the setLayout method.

              ? Offline
              ? Offline
              A Former User
              wrote on 30 Mar 2023, 17:53 last edited by A Former User
              #10

              @8Observer8 QOpenGLWindow depends on QWindow, which as far as I understand is rather intended for QtQuick, but not for QtWidget.
              QOpenGLWindow depends on QWindow and QWindow is not QWidget.
              If you want a QtWidget app, you need QOpenGLWidget instead (which is based on QWidget and thus has setLayout).

              8 1 Reply Last reply 30 Mar 2023, 18:00
              0
              • ? A Former User
                30 Mar 2023, 17:53

                @8Observer8 QOpenGLWindow depends on QWindow, which as far as I understand is rather intended for QtQuick, but not for QtWidget.
                QOpenGLWindow depends on QWindow and QWindow is not QWidget.
                If you want a QtWidget app, you need QOpenGLWidget instead (which is based on QWidget and thus has setLayout).

                8 Offline
                8 Offline
                8Observer8
                wrote on 30 Mar 2023, 18:00 last edited by 8Observer8
                #11

                @ankou29666 said in Is it possible to add GUI for QOpenGLWindow?:

                If you want a QtWidget app, you need QOpenGLWidget instead (which is based on QWidget and thus has setLayout).

                QOpenGLWidget has a bad performance for animations, i.e. the animation is not smooth, because there is no synchronization for updating frames on the monitor. To solve this problem, QOpengLWindow was created with the frameSwapped signal: https://doc.qt.io/qt-6/qopenglwindow.html#frameSwapped

                This signal is emitted after the potentially blocking buffer swap has been done. Applications that wish to continuously repaint synchronized to the vertical refresh, should issue an update() upon this signal. This allows for a much smoother experience compared to the traditional usage of timers.

                ? 1 Reply Last reply 30 Mar 2023, 18:17
                0
                • 8 8Observer8
                  30 Mar 2023, 18:00

                  @ankou29666 said in Is it possible to add GUI for QOpenGLWindow?:

                  If you want a QtWidget app, you need QOpenGLWidget instead (which is based on QWidget and thus has setLayout).

                  QOpenGLWidget has a bad performance for animations, i.e. the animation is not smooth, because there is no synchronization for updating frames on the monitor. To solve this problem, QOpengLWindow was created with the frameSwapped signal: https://doc.qt.io/qt-6/qopenglwindow.html#frameSwapped

                  This signal is emitted after the potentially blocking buffer swap has been done. Applications that wish to continuously repaint synchronized to the vertical refresh, should issue an update() upon this signal. This allows for a much smoother experience compared to the traditional usage of timers.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 30 Mar 2023, 18:17 last edited by A Former User
                  #12

                  @8Observer8 yes this is issue to consider indeed. I hadn't read properly your second doc quote ...

                  QOpenGLWindow depends on QWindow and so does QQuickWindow ...

                  What QWindow's doc says

                  QWindow::QWindow(QWindow *parent)

                  Creates a window as a child of the given parent window.

                  The window will be embedded inside the parent window, its coordinates relative to the parent.

                  The only option I see then would be something like nesting QOpenGLWindow inside a QQuickWindow and do your ui in QtQuick and QML instead.

                  8 1 Reply Last reply 30 Mar 2023, 18:32
                  1
                  • J JoeCFD
                    30 Mar 2023, 16:55

                    @8Observer8 Yes, you can. But I would not add button to the OpenGLWidget. Instead, create a QWidget, for example;

                    class DisplayWidget : public QWidget
                    {
                        explicit DisplayWidget( QWidget * parent=nullptr ) : QWidget( parent ) {
                                make a layout and add m_openGLWidget;
                                m_startGameButton = new QPushButton( QString( "Start Game" ), this );
                               /* do not add it to the layout */ 
                               m_startGameButton->move( center);
                        }
                    
                    private:
                          OpenGLWidget * m_openGLWidget{};
                          QPushButton * m_startGameButton{}; 
                    }
                    

                    After button click, either hide it(for reuse) or destroy it(not needed anymore)
                    separate all controls from OpenGLWidget and make OpenGLWidget handling drawing only. Otherwise, the code looks a bit messy.

                    8 Offline
                    8 Offline
                    8Observer8
                    wrote on 30 Mar 2023, 18:24 last edited by 8Observer8
                    #13

                    @JoeCFD I am trying to compile your example. What the center variable here:

                    main_window.h

                    #ifndef MAIN_WINDOW_H
                    #define MAIN_WINDOW_H
                    
                    #include <QtCore/QString>
                    #include <QtWidgets/QPushButton>
                    #include <QtWidgets/QWidget>
                    
                    #include "opengl_window.h"
                    
                    class MainWindow : public QWidget
                    {
                    public:
                        explicit MainWindow(QWidget * parent=nullptr) : QWidget(parent)
                        {
                            m_pStartGameButton = new QPushButton(QString("Start Game"), this);
                            /* do not add it to the layout */ 
                            m_pStartGameButton->move(center);
                        }
                    
                    private:
                        OpenGLWindow *m_pOpenGLWindow {};
                        QPushButton *m_pStartGameButton {};
                    };
                    
                    #endif // MAIN_WINDOW_H
                    
                    J 1 Reply Last reply 30 Mar 2023, 18:36
                    0
                    • ? A Former User
                      30 Mar 2023, 18:17

                      @8Observer8 yes this is issue to consider indeed. I hadn't read properly your second doc quote ...

                      QOpenGLWindow depends on QWindow and so does QQuickWindow ...

                      What QWindow's doc says

                      QWindow::QWindow(QWindow *parent)

                      Creates a window as a child of the given parent window.

                      The window will be embedded inside the parent window, its coordinates relative to the parent.

                      The only option I see then would be something like nesting QOpenGLWindow inside a QQuickWindow and do your ui in QtQuick and QML instead.

                      8 Offline
                      8 Offline
                      8Observer8
                      wrote on 30 Mar 2023, 18:32 last edited by 8Observer8
                      #14

                      @ankou29666 said in Is it possible to add GUI for QOpenGLWindow?:

                      The only option I see then would be something like nesting QOpenGLWindow inside a QQuickWindow and do your ui in QtQuick and QML instead.

                      I will first try placing a QPushButton on top of a QOpenGLWindow as suggested by JoeCFD and then try your way.

                      1 Reply Last reply
                      0
                      • 8 8Observer8
                        30 Mar 2023, 18:24

                        @JoeCFD I am trying to compile your example. What the center variable here:

                        main_window.h

                        #ifndef MAIN_WINDOW_H
                        #define MAIN_WINDOW_H
                        
                        #include <QtCore/QString>
                        #include <QtWidgets/QPushButton>
                        #include <QtWidgets/QWidget>
                        
                        #include "opengl_window.h"
                        
                        class MainWindow : public QWidget
                        {
                        public:
                            explicit MainWindow(QWidget * parent=nullptr) : QWidget(parent)
                            {
                                m_pStartGameButton = new QPushButton(QString("Start Game"), this);
                                /* do not add it to the layout */ 
                                m_pStartGameButton->move(center);
                            }
                        
                        private:
                            OpenGLWindow *m_pOpenGLWindow {};
                            QPushButton *m_pStartGameButton {};
                        };
                        
                        #endif // MAIN_WINDOW_H
                        
                        J Offline
                        J Offline
                        JoeCFD
                        wrote on 30 Mar 2023, 18:36 last edited by
                        #15

                        @8Observer8 it is pseudocode. I do not know the center coordinates of your widget. You need to know where it is.

                        1 Reply Last reply
                        0
                        • J JoeCFD
                          30 Mar 2023, 16:55

                          @8Observer8 Yes, you can. But I would not add button to the OpenGLWidget. Instead, create a QWidget, for example;

                          class DisplayWidget : public QWidget
                          {
                              explicit DisplayWidget( QWidget * parent=nullptr ) : QWidget( parent ) {
                                      make a layout and add m_openGLWidget;
                                      m_startGameButton = new QPushButton( QString( "Start Game" ), this );
                                     /* do not add it to the layout */ 
                                     m_startGameButton->move( center);
                              }
                          
                          private:
                                OpenGLWidget * m_openGLWidget{};
                                QPushButton * m_startGameButton{}; 
                          }
                          

                          After button click, either hide it(for reuse) or destroy it(not needed anymore)
                          separate all controls from OpenGLWidget and make OpenGLWidget handling drawing only. Otherwise, the code looks a bit messy.

                          8 Offline
                          8 Offline
                          8Observer8
                          wrote on 30 Mar 2023, 20:05 last edited by 8Observer8
                          #16

                          @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                          make a layout and add m_openGLWidget;

                          How to add it to layout?

                                  QHBoxLayout *hbox = new QHBoxLayout();
                                  hbox->addWidget(m_pOpenGLWindow);
                          
                          error: no matching function for call to 'QHBoxLayout::addWidget(OpenGLWindow*&)'
                                   vbox->addWidget(m_pOpenGLWindow);
                          

                          main_window.h

                          #ifndef MAIN_WINDOW_H
                          #define MAIN_WINDOW_H
                          
                          #include <QtCore/QSize>
                          #include <QtCore/QString>
                          #include <QtWidgets/QHBoxLayout>
                          #include <QtWidgets/QPushButton>
                          #include <QtWidgets/QWidget>
                          
                          #include "opengl_window.h"
                          
                          class MainWindow : public QWidget
                          {
                          public:
                              explicit MainWindow(QWidget *parent=nullptr) : QWidget(parent)
                              {
                                  setFixedSize(QSize(300, 300));
                                  m_pStartGameButton = new QPushButton(QString("Start Game"), this);
                                  // m_pStartGameButton->move(center);
                          
                                  QHBoxLayout *hbox = new QHBoxLayout();
                                  m_pOpenGLWindow = new OpenGLWindow();
                                  hbox->addWidget(m_pOpenGLWindow);
                          
                              }
                          
                              ~MainWindow()
                              {
                                  delete m_pOpenGLWindow;
                              }
                          
                          private:
                              OpenGLWindow *m_pOpenGLWindow {};
                              QPushButton *m_pStartGameButton {};
                          };
                          
                          #endif // MAIN_WINDOW_H
                          

                          opengl_window.h

                          #ifndef OPENGL_WINDOW_H
                          #define OPENGL_WINDOW_H
                          
                          #include <QtCore/QSize>
                          #include <QtGui/QOpenGLFunctions>
                          #include <QtGui/QSurfaceFormat>
                          #include <QtOpenGL/QOpenGLWindow>
                          
                          #include <iostream>
                          
                          class OpenGLWindow : public QOpenGLWindow, private QOpenGLFunctions
                          {
                          public:
                              OpenGLWindow()
                              {
                                  resize(QSize(300, 300));
                                  setTitle("OpenGL 2.1, Qt6, C++");
                          
                                  // Set format
                                  QSurfaceFormat format;
                                  format.setSamples(4);
                                  format.setSwapInterval(1);
                                  setFormat(format);
                                  connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
                              }
                          
                          private:
                              void initializeGL() override
                              {
                                  initializeOpenGLFunctions();
                                  glClearColor(0.2, 0.2, 0.2, 1);
                              }
                          
                              void paintGL() override
                              {
                                  glClear(GL_COLOR_BUFFER_BIT);
                                  // std::cout << "paintGL" << std::endl;
                              }
                          };
                          
                          #endif // OPENGL_WINDOW_H
                          

                          main.cpp

                          /*
                          .pro file:
                          QT += core gui opengl widgets
                          win32: LIBS += -lopengl32
                          SOURCES += main.cpp
                          TARGET = app
                          -------------------------------
                          Build and run commands for CMD:
                          > qmake -makefile
                          > mingw32-make
                          > "release/app"
                          */
                          
                          #ifdef _WIN32
                          #include <windows.h>
                          extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
                          extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
                          #endif
                          
                          #include <QtWidgets/QApplication>
                          #include <iostream>
                          #include "main_window.h"
                          
                          int main(int argc, char *argv[])
                          {
                          #ifdef _WIN32
                              if (AttachConsole(ATTACH_PARENT_PROCESS))
                              {
                                  freopen("CONOUT$", "w", stdout);
                                  freopen("CONOUT$", "w", stderr);
                              }
                          #endif
                              std::cout << std::endl;
                          
                              QApplication app(argc, argv);
                              MainWindow w;
                              w.show();
                              return app.exec();
                          }
                          
                          J 8 2 Replies Last reply 30 Mar 2023, 20:11
                          0
                          • 8 8Observer8
                            30 Mar 2023, 20:05

                            @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                            make a layout and add m_openGLWidget;

                            How to add it to layout?

                                    QHBoxLayout *hbox = new QHBoxLayout();
                                    hbox->addWidget(m_pOpenGLWindow);
                            
                            error: no matching function for call to 'QHBoxLayout::addWidget(OpenGLWindow*&)'
                                     vbox->addWidget(m_pOpenGLWindow);
                            

                            main_window.h

                            #ifndef MAIN_WINDOW_H
                            #define MAIN_WINDOW_H
                            
                            #include <QtCore/QSize>
                            #include <QtCore/QString>
                            #include <QtWidgets/QHBoxLayout>
                            #include <QtWidgets/QPushButton>
                            #include <QtWidgets/QWidget>
                            
                            #include "opengl_window.h"
                            
                            class MainWindow : public QWidget
                            {
                            public:
                                explicit MainWindow(QWidget *parent=nullptr) : QWidget(parent)
                                {
                                    setFixedSize(QSize(300, 300));
                                    m_pStartGameButton = new QPushButton(QString("Start Game"), this);
                                    // m_pStartGameButton->move(center);
                            
                                    QHBoxLayout *hbox = new QHBoxLayout();
                                    m_pOpenGLWindow = new OpenGLWindow();
                                    hbox->addWidget(m_pOpenGLWindow);
                            
                                }
                            
                                ~MainWindow()
                                {
                                    delete m_pOpenGLWindow;
                                }
                            
                            private:
                                OpenGLWindow *m_pOpenGLWindow {};
                                QPushButton *m_pStartGameButton {};
                            };
                            
                            #endif // MAIN_WINDOW_H
                            

                            opengl_window.h

                            #ifndef OPENGL_WINDOW_H
                            #define OPENGL_WINDOW_H
                            
                            #include <QtCore/QSize>
                            #include <QtGui/QOpenGLFunctions>
                            #include <QtGui/QSurfaceFormat>
                            #include <QtOpenGL/QOpenGLWindow>
                            
                            #include <iostream>
                            
                            class OpenGLWindow : public QOpenGLWindow, private QOpenGLFunctions
                            {
                            public:
                                OpenGLWindow()
                                {
                                    resize(QSize(300, 300));
                                    setTitle("OpenGL 2.1, Qt6, C++");
                            
                                    // Set format
                                    QSurfaceFormat format;
                                    format.setSamples(4);
                                    format.setSwapInterval(1);
                                    setFormat(format);
                                    connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
                                }
                            
                            private:
                                void initializeGL() override
                                {
                                    initializeOpenGLFunctions();
                                    glClearColor(0.2, 0.2, 0.2, 1);
                                }
                            
                                void paintGL() override
                                {
                                    glClear(GL_COLOR_BUFFER_BIT);
                                    // std::cout << "paintGL" << std::endl;
                                }
                            };
                            
                            #endif // OPENGL_WINDOW_H
                            

                            main.cpp

                            /*
                            .pro file:
                            QT += core gui opengl widgets
                            win32: LIBS += -lopengl32
                            SOURCES += main.cpp
                            TARGET = app
                            -------------------------------
                            Build and run commands for CMD:
                            > qmake -makefile
                            > mingw32-make
                            > "release/app"
                            */
                            
                            #ifdef _WIN32
                            #include <windows.h>
                            extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
                            extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
                            #endif
                            
                            #include <QtWidgets/QApplication>
                            #include <iostream>
                            #include "main_window.h"
                            
                            int main(int argc, char *argv[])
                            {
                            #ifdef _WIN32
                                if (AttachConsole(ATTACH_PARENT_PROCESS))
                                {
                                    freopen("CONOUT$", "w", stdout);
                                    freopen("CONOUT$", "w", stderr);
                                }
                            #endif
                                std::cout << std::endl;
                            
                                QApplication app(argc, argv);
                                MainWindow w;
                                w.show();
                                return app.exec();
                            }
                            
                            J Offline
                            J Offline
                            JoeCFD
                            wrote on 30 Mar 2023, 20:11 last edited by
                            #17

                            @8Observer8 said in Is it possible to add GUI for QOpenGLWindow?:

                            m_pOpenGLWindow

                            m_pOpenGLWindow is not created. Is its header included? QOpenGLWidget is a widget. Sure you can add it to the layout.

                            8 1 Reply Last reply 30 Mar 2023, 20:12
                            0
                            • 8 8Observer8
                              30 Mar 2023, 20:05

                              @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                              make a layout and add m_openGLWidget;

                              How to add it to layout?

                                      QHBoxLayout *hbox = new QHBoxLayout();
                                      hbox->addWidget(m_pOpenGLWindow);
                              
                              error: no matching function for call to 'QHBoxLayout::addWidget(OpenGLWindow*&)'
                                       vbox->addWidget(m_pOpenGLWindow);
                              

                              main_window.h

                              #ifndef MAIN_WINDOW_H
                              #define MAIN_WINDOW_H
                              
                              #include <QtCore/QSize>
                              #include <QtCore/QString>
                              #include <QtWidgets/QHBoxLayout>
                              #include <QtWidgets/QPushButton>
                              #include <QtWidgets/QWidget>
                              
                              #include "opengl_window.h"
                              
                              class MainWindow : public QWidget
                              {
                              public:
                                  explicit MainWindow(QWidget *parent=nullptr) : QWidget(parent)
                                  {
                                      setFixedSize(QSize(300, 300));
                                      m_pStartGameButton = new QPushButton(QString("Start Game"), this);
                                      // m_pStartGameButton->move(center);
                              
                                      QHBoxLayout *hbox = new QHBoxLayout();
                                      m_pOpenGLWindow = new OpenGLWindow();
                                      hbox->addWidget(m_pOpenGLWindow);
                              
                                  }
                              
                                  ~MainWindow()
                                  {
                                      delete m_pOpenGLWindow;
                                  }
                              
                              private:
                                  OpenGLWindow *m_pOpenGLWindow {};
                                  QPushButton *m_pStartGameButton {};
                              };
                              
                              #endif // MAIN_WINDOW_H
                              

                              opengl_window.h

                              #ifndef OPENGL_WINDOW_H
                              #define OPENGL_WINDOW_H
                              
                              #include <QtCore/QSize>
                              #include <QtGui/QOpenGLFunctions>
                              #include <QtGui/QSurfaceFormat>
                              #include <QtOpenGL/QOpenGLWindow>
                              
                              #include <iostream>
                              
                              class OpenGLWindow : public QOpenGLWindow, private QOpenGLFunctions
                              {
                              public:
                                  OpenGLWindow()
                                  {
                                      resize(QSize(300, 300));
                                      setTitle("OpenGL 2.1, Qt6, C++");
                              
                                      // Set format
                                      QSurfaceFormat format;
                                      format.setSamples(4);
                                      format.setSwapInterval(1);
                                      setFormat(format);
                                      connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
                                  }
                              
                              private:
                                  void initializeGL() override
                                  {
                                      initializeOpenGLFunctions();
                                      glClearColor(0.2, 0.2, 0.2, 1);
                                  }
                              
                                  void paintGL() override
                                  {
                                      glClear(GL_COLOR_BUFFER_BIT);
                                      // std::cout << "paintGL" << std::endl;
                                  }
                              };
                              
                              #endif // OPENGL_WINDOW_H
                              

                              main.cpp

                              /*
                              .pro file:
                              QT += core gui opengl widgets
                              win32: LIBS += -lopengl32
                              SOURCES += main.cpp
                              TARGET = app
                              -------------------------------
                              Build and run commands for CMD:
                              > qmake -makefile
                              > mingw32-make
                              > "release/app"
                              */
                              
                              #ifdef _WIN32
                              #include <windows.h>
                              extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
                              extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
                              #endif
                              
                              #include <QtWidgets/QApplication>
                              #include <iostream>
                              #include "main_window.h"
                              
                              int main(int argc, char *argv[])
                              {
                              #ifdef _WIN32
                                  if (AttachConsole(ATTACH_PARENT_PROCESS))
                                  {
                                      freopen("CONOUT$", "w", stdout);
                                      freopen("CONOUT$", "w", stderr);
                                  }
                              #endif
                                  std::cout << std::endl;
                              
                                  QApplication app(argc, argv);
                                  MainWindow w;
                                  w.show();
                                  return app.exec();
                              }
                              
                              8 Offline
                              8 Offline
                              8Observer8
                              wrote on 30 Mar 2023, 20:11 last edited by 8Observer8
                              #18

                              I posted all the example code above.

                              1 Reply Last reply
                              0
                              • J JoeCFD
                                30 Mar 2023, 20:11

                                @8Observer8 said in Is it possible to add GUI for QOpenGLWindow?:

                                m_pOpenGLWindow

                                m_pOpenGLWindow is not created. Is its header included? QOpenGLWidget is a widget. Sure you can add it to the layout.

                                8 Offline
                                8 Offline
                                8Observer8
                                wrote on 30 Mar 2023, 20:12 last edited by 8Observer8
                                #19

                                @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                                QOpenGLWidget is a widget. Sure you can add it to the layout.

                                I just replaced QOpenGLWidget to QOpenGLWindow in your example. I don't need QOpenGLWidget.

                                J 1 Reply Last reply 30 Mar 2023, 20:18
                                0
                                • 8 8Observer8
                                  30 Mar 2023, 20:12

                                  @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                                  QOpenGLWidget is a widget. Sure you can add it to the layout.

                                  I just replaced QOpenGLWidget to QOpenGLWindow in your example. I don't need QOpenGLWidget.

                                  J Offline
                                  J Offline
                                  JoeCFD
                                  wrote on 30 Mar 2023, 20:18 last edited by JoeCFD
                                  #20

                                  @8Observer8 You can not add it to the layout. I use QOpenGLWidget. You can overlay your button to any widget which holds QOpenGLWindow.

                                  https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow

                                  8 1 Reply Last reply 30 Mar 2023, 21:08
                                  0
                                  • J JoeCFD
                                    30 Mar 2023, 20:18

                                    @8Observer8 You can not add it to the layout. I use QOpenGLWidget. You can overlay your button to any widget which holds QOpenGLWindow.

                                    https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow

                                    8 Offline
                                    8 Offline
                                    8Observer8
                                    wrote on 30 Mar 2023, 21:08 last edited by
                                    #21

                                    @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                                    https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow

                                    This is the only information I found on my topic through Google. But this question on StackOverflow doesn't have a solution, so I forgot about it. Now I noticed QWidget::createWindowContainer. But the question remains open. I'm trying to position a button in the center of an OpenGL window:

                                    d125f17c-b958-4196-be6d-35ce157c7dd4-image.png

                                    main_window.h

                                    #ifndef MAIN_WINDOW_H
                                    #define MAIN_WINDOW_H
                                    
                                    #include <QtCore/QSize>
                                    #include <QtCore/QString>
                                    #include <QtWidgets/QHBoxLayout>
                                    #include <QtWidgets/QVBoxLayout>
                                    #include <QtWidgets/QPushButton>
                                    #include <QtWidgets/QWidget>
                                    
                                    #include "opengl_window.h"
                                    
                                    class MainWindow : public QWidget
                                    {
                                    public:
                                        explicit MainWindow(QWidget *parent=nullptr) : QWidget(parent)
                                        {
                                            setFixedSize(QSize(300, 300));
                                            setWindowTitle("OpenGL 2.1, Qt6, C++");
                                    
                                            // m_pStartGameButton->move(center);
                                    
                                            QVBoxLayout *rootLayout = new QVBoxLayout();
                                            setLayout(rootLayout);
                                    
                                            m_pOpenGLWindow = new OpenGLWindow();
                                            QWidget *container = QWidget::createWindowContainer(m_pOpenGLWindow);
                                            rootLayout->addWidget(container);
                                    
                                            m_pStartGameButton = new QPushButton(QString("Start Game"));
                                            QHBoxLayout *startButtonLayout = new QHBoxLayout();
                                            startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                                            startButtonLayout->addWidget(m_pStartGameButton);
                                            rootLayout->addLayout(startButtonLayout);
                                        }
                                    
                                        ~MainWindow()
                                        {
                                            delete m_pOpenGLWindow;
                                        }
                                    
                                    private:
                                        OpenGLWindow *m_pOpenGLWindow {};
                                        QPushButton *m_pStartGameButton {};
                                    };
                                    
                                    #endif // MAIN_WINDOW_H
                                    
                                    8 1 Reply Last reply 30 Mar 2023, 21:13
                                    0
                                    • 8 8Observer8
                                      30 Mar 2023, 21:08

                                      @JoeCFD said in Is it possible to add GUI for QOpenGLWindow?:

                                      https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow

                                      This is the only information I found on my topic through Google. But this question on StackOverflow doesn't have a solution, so I forgot about it. Now I noticed QWidget::createWindowContainer. But the question remains open. I'm trying to position a button in the center of an OpenGL window:

                                      d125f17c-b958-4196-be6d-35ce157c7dd4-image.png

                                      main_window.h

                                      #ifndef MAIN_WINDOW_H
                                      #define MAIN_WINDOW_H
                                      
                                      #include <QtCore/QSize>
                                      #include <QtCore/QString>
                                      #include <QtWidgets/QHBoxLayout>
                                      #include <QtWidgets/QVBoxLayout>
                                      #include <QtWidgets/QPushButton>
                                      #include <QtWidgets/QWidget>
                                      
                                      #include "opengl_window.h"
                                      
                                      class MainWindow : public QWidget
                                      {
                                      public:
                                          explicit MainWindow(QWidget *parent=nullptr) : QWidget(parent)
                                          {
                                              setFixedSize(QSize(300, 300));
                                              setWindowTitle("OpenGL 2.1, Qt6, C++");
                                      
                                              // m_pStartGameButton->move(center);
                                      
                                              QVBoxLayout *rootLayout = new QVBoxLayout();
                                              setLayout(rootLayout);
                                      
                                              m_pOpenGLWindow = new OpenGLWindow();
                                              QWidget *container = QWidget::createWindowContainer(m_pOpenGLWindow);
                                              rootLayout->addWidget(container);
                                      
                                              m_pStartGameButton = new QPushButton(QString("Start Game"));
                                              QHBoxLayout *startButtonLayout = new QHBoxLayout();
                                              startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                                              startButtonLayout->addWidget(m_pStartGameButton);
                                              rootLayout->addLayout(startButtonLayout);
                                          }
                                      
                                          ~MainWindow()
                                          {
                                              delete m_pOpenGLWindow;
                                          }
                                      
                                      private:
                                          OpenGLWindow *m_pOpenGLWindow {};
                                          QPushButton *m_pStartGameButton {};
                                      };
                                      
                                      #endif // MAIN_WINDOW_H
                                      
                                      8 Offline
                                      8 Offline
                                      8Observer8
                                      wrote on 30 Mar 2023, 21:13 last edited by 8Observer8
                                      #22

                                      I found my mistake:

                                              // rootLayout->addLayout(startButtonLayout);
                                              container->setLayout(startButtonLayout);
                                      

                                      But it looks like the button is behind the OpenGL canvas like in the topic: https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow
                                      Is it a bug?

                                      f950a4d2-120c-4c81-a990-3297b809f74b-image.png

                                      8 1 Reply Last reply 30 Mar 2023, 21:24
                                      0
                                      • 8 8Observer8
                                        30 Mar 2023, 21:13

                                        I found my mistake:

                                                // rootLayout->addLayout(startButtonLayout);
                                                container->setLayout(startButtonLayout);
                                        

                                        But it looks like the button is behind the OpenGL canvas like in the topic: https://stackoverflow.com/questions/32593428/add-qwidget-on-top-of-qopenglwindow
                                        Is it a bug?

                                        f950a4d2-120c-4c81-a990-3297b809f74b-image.png

                                        8 Offline
                                        8 Offline
                                        8Observer8
                                        wrote on 30 Mar 2023, 21:24 last edited by
                                        #23

                                        Current code:

                                                QVBoxLayout *rootLayout = new QVBoxLayout();
                                                setLayout(rootLayout);
                                        
                                                m_pOpenGLWindow = new OpenGLWindow();
                                                QWidget *container = QWidget::createWindowContainer(m_pOpenGLWindow);
                                                rootLayout->addWidget(container);
                                        
                                                m_pStartGameButton = new QPushButton(QString("Start Game"));
                                                QHBoxLayout *startButtonLayout = new QHBoxLayout();
                                                startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                                                startButtonLayout->addWidget(m_pStartGameButton);
                                                // rootLayout->addLayout(startButtonLayout);
                                                container->setLayout(startButtonLayout);
                                        
                                        J 1 Reply Last reply 30 Mar 2023, 21:37
                                        0
                                        • 8 8Observer8
                                          30 Mar 2023, 21:24

                                          Current code:

                                                  QVBoxLayout *rootLayout = new QVBoxLayout();
                                                  setLayout(rootLayout);
                                          
                                                  m_pOpenGLWindow = new OpenGLWindow();
                                                  QWidget *container = QWidget::createWindowContainer(m_pOpenGLWindow);
                                                  rootLayout->addWidget(container);
                                          
                                                  m_pStartGameButton = new QPushButton(QString("Start Game"));
                                                  QHBoxLayout *startButtonLayout = new QHBoxLayout();
                                                  startButtonLayout->setAlignment(Qt::AlignmentFlag::AlignHCenter);
                                                  startButtonLayout->addWidget(m_pStartGameButton);
                                                  // rootLayout->addLayout(startButtonLayout);
                                                  container->setLayout(startButtonLayout);
                                          
                                          J Offline
                                          J Offline
                                          JoeCFD
                                          wrote on 30 Mar 2023, 21:37 last edited by
                                          #24

                                          @8Observer8
                                          Do not put it in the layout.

                                                 m_pStartGameButton = new QPushButton(QString("Start Game"), this );
                                                 m_pStartGameButton->move( 150, 150 );
                                          
                                          8 2 Replies Last reply 30 Mar 2023, 21:48
                                          0

                                          14/40

                                          30 Mar 2023, 18:32

                                          • Login

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