QOpenGLWidget says I need a QApplication before QWidget, even if I already have.
-
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(); } -
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(); }@Hyperspider-Software
Hello and welcome.Run in debugger and look at stack view on error. Usually this mean you have some static
QWidgetsomewhere, at global scope, maybe in another file. You must not do that, as it executes before yourmain()&QApplication. -
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.
-
I kept seeing this message in the stack menu but couldn't find a solution.
Any other solutions?
-
Please show your complete main.cpp - the line number in the stack trace do not match your main.cpp you posted.
-
Please show your complete main.cpp - the line number in the stack trace do not match your main.cpp you posted.
@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(); } -
@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(); }@Hyperspider-Software
If this were your completemain.cppit would not compile since it does not have any#includestatements, and nor would it have a "Line 29" as shown in the stack trace. -
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.