Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLWidget says I need a QApplication before QWidget, even if I already have.
Qt 6.12 LTS is out! See what's new in the release blog

QOpenGLWidget says I need a QApplication before QWidget, even if I already have.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.3k Views 1 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.
  • H Offline
    H Offline
    Hyperspider Software
    wrote on last edited by
    #1

    So I'm making a game engine, I tried to make a QOpenGLWidget attached to one of my QMainWindow tabs, but unfortunately I kept getting an error saying I need to construct a QApplication before a QWidget.

    I already did that but unfortunately doesn't work.

    Am I doing something wrong here?

    LevelEditor.cpp:

    HAEDITOR_LEVELEDITOR::HAEDITOR_LEVELEDITOR(QWidget *parent)
    	: QOpenGLWidget(parent)
    {
    	ui.setupUi(this);
    
    	//HATime::renderAPI = HA_RENDER_ENUM::OpenGL;
    
    	// QOpenGLWidget* oGL = new QOpenGLWidget(parent);
    	ui.gridLayout->addWidget(this);
    
    	HAEDITOR_MAINWINDOW* mainWindow = new HAEDITOR_MAINWINDOW();
    	mainWindow->ui.TabNavigation->insertTab(0, this, "Level Editor");
    
    	initializeGL();
    	paintGL();
    	resizeGL(1024, 716);
    }
    

    MainWIndow.cpp

    HAEDITOR_MAINWINDOW::HAEDITOR_MAINWINDOW(QWidget* parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	HAEDITOR_LEVELEDITOR* levelEditor = new HAEDITOR_LEVELEDITOR(parent);
    
    	ui.TabNavigation->insertTab(0, levelEditor, "Level Editor");
    }
    

    Main.cpp:

    int main(int argc, char* argv[])
    {
    	QApplication haEditor(argc, argv);
    
    	std::unique_ptr<HAEDITOR_MAINWINDOW> mainEditor(new HAEDITOR_MAINWINDOW());
    	mainEditor->show();
    
    	return haEditor.exec();
    }
    
    JonBJ 1 Reply Last reply
    0
    • H Hyperspider Software

      So I'm making a game engine, I tried to make a QOpenGLWidget attached to one of my QMainWindow tabs, but unfortunately I kept getting an error saying I need to construct a QApplication before a QWidget.

      I already did that but unfortunately doesn't work.

      Am I doing something wrong here?

      LevelEditor.cpp:

      HAEDITOR_LEVELEDITOR::HAEDITOR_LEVELEDITOR(QWidget *parent)
      	: QOpenGLWidget(parent)
      {
      	ui.setupUi(this);
      
      	//HATime::renderAPI = HA_RENDER_ENUM::OpenGL;
      
      	// QOpenGLWidget* oGL = new QOpenGLWidget(parent);
      	ui.gridLayout->addWidget(this);
      
      	HAEDITOR_MAINWINDOW* mainWindow = new HAEDITOR_MAINWINDOW();
      	mainWindow->ui.TabNavigation->insertTab(0, this, "Level Editor");
      
      	initializeGL();
      	paintGL();
      	resizeGL(1024, 716);
      }
      

      MainWIndow.cpp

      HAEDITOR_MAINWINDOW::HAEDITOR_MAINWINDOW(QWidget* parent)
      	: QMainWindow(parent)
      {
      	ui.setupUi(this);
      
      	HAEDITOR_LEVELEDITOR* levelEditor = new HAEDITOR_LEVELEDITOR(parent);
      
      	ui.TabNavigation->insertTab(0, levelEditor, "Level Editor");
      }
      

      Main.cpp:

      int main(int argc, char* argv[])
      {
      	QApplication haEditor(argc, argv);
      
      	std::unique_ptr<HAEDITOR_MAINWINDOW> mainEditor(new HAEDITOR_MAINWINDOW());
      	mainEditor->show();
      
      	return haEditor.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Hyperspider-Software
      Hello and welcome.

      Run in debugger and look at stack view on error. Usually this mean you have some static QWidget somewhere, at global scope, maybe in another file. You must not do that, as it executes before your main() & QApplication.

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

        Hi,

        @Hyperspider-Software said in QOpenGLWidget says I need a QApplication before QWidget, even if I already have.:

        initializeGL();
        paintGL();
        resizeGL(1024, 716);

        Beside what @JonB suggests, this is also wrong. These methods will be called when appropriate by Qt. Their place is not in the class constructor.

        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
        3
        • H Offline
          H Offline
          Hyperspider Software
          wrote on last edited by
          #4

          I kept seeing this message in the stack menu but couldn't find a solution.
          Any other solutions?

          QtProblem.png

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please show your complete main.cpp - the line number in the stack trace do not match your main.cpp you posted.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Please show your complete main.cpp - the line number in the stack trace do not match your main.cpp you posted.

              H Offline
              H Offline
              Hyperspider Software
              wrote on last edited by
              #6

              @Christian-Ehrlicher here:

              int main(int argc, char *argv[])
              {
              	QApplication haEditor(argc, argv);
              	haEditor.setWindowIcon(QIcon
              	("C:\\Users\\abora\\source\\repos\\HyperArachnid\\HAEditor\\Main\\Icon\\HyperArachnidLogo.png")
              	);
              
              	//std::unique_ptr<HATime> time(new HATime());
              
              	QSplashScreen* splashScreen = new QSplashScreen;
              	splashScreen->setPixmap(QPixmap("C:\\Users\\abora\\source\\repos\\HyperArachnid\\HAEditor\\Main\\SplashImage\\HAEditorSplashIMage.png"));
              	splashScreen->show();
              
              	QTimer::singleShot(2500, splashScreen, SLOT(close()));
              
              	HAEDITOR_MAINWINDOW* mainEditor = new HAEDITOR_MAINWINDOW();
              
              	HAEDITOR_LEVELGRAPH* levelEditor = new HAEDITOR_LEVELGRAPH();
              
              	mainEditor->ui.TabNavigation->addTab(levelEditor->window(), "Level Editor");
              
              	QTimer::singleShot(3500, mainEditor, SLOT(show()));
              
              	return haEditor.exec();
              }
              
              JonBJ 1 Reply Last reply
              0
              • H Hyperspider Software

                @Christian-Ehrlicher here:

                int main(int argc, char *argv[])
                {
                	QApplication haEditor(argc, argv);
                	haEditor.setWindowIcon(QIcon
                	("C:\\Users\\abora\\source\\repos\\HyperArachnid\\HAEditor\\Main\\Icon\\HyperArachnidLogo.png")
                	);
                
                	//std::unique_ptr<HATime> time(new HATime());
                
                	QSplashScreen* splashScreen = new QSplashScreen;
                	splashScreen->setPixmap(QPixmap("C:\\Users\\abora\\source\\repos\\HyperArachnid\\HAEditor\\Main\\SplashImage\\HAEditorSplashIMage.png"));
                	splashScreen->show();
                
                	QTimer::singleShot(2500, splashScreen, SLOT(close()));
                
                	HAEDITOR_MAINWINDOW* mainEditor = new HAEDITOR_MAINWINDOW();
                
                	HAEDITOR_LEVELGRAPH* levelEditor = new HAEDITOR_LEVELGRAPH();
                
                	mainEditor->ui.TabNavigation->addTab(levelEditor->window(), "Level Editor");
                
                	QTimer::singleShot(3500, mainEditor, SLOT(show()));
                
                	return haEditor.exec();
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Hyperspider-Software
                If this were your complete main.cpp it would not compile since it does not have any #include statements, and nor would it have a "Line 29" as shown in the stack trace.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Now please remove all the stuff you don't need, only derive from QOpenGLWidget and remove all the other unneeded stuff to see if it still gives the problem. Once you stripped down your program to less than 50 lines we can try it out here.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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
                  • Unsolved