Important: Please read the Qt Code of Conduct -

Qt5 runtime error (only in Release) "QApplication must be created before QWidget"

  • Hi guys,

    I am porting my build system to Win8 (64bits Visual Studio 12 compiler, note this application runs like a charm in Ubuntu and MacOSX). On windows I can run it without issues (the main window appears) in Debug mode - that is in my CMakeLists.txt I set:

    set(CMAKE_BUILD_TYPE "Debug") 

    However, when I compile in Release, at runtime I get the well known "QApplication must be created before QWidget" error. While this is clearly not the case (considering the app runs perfectly in other OSs and in debug mode in Win) I have noticed that the error is reported within QtCored.dll.

    I believe the issue is that even though I am building in release it is still linking against the debug libraries of Qt!!... But why? Is there a way of telling Qt (through CMake) that it was the Release libraries it was supposed to link against?

  • I found that CMAKE_BUILD_TYPE "Debug" is not sufficient on Windows.

    You must also set Configuration to "Release". I use msbuild.exe, so it looks like this:

    MSBuild.exe MYPROJECT.vcxproj //p:Configuration=Release

    In VS Express 2013, you can set this in Build->Configuration Manager. There switch all Debugs into Releases.

  • I have found the problem. In my system I am using an external library based on Qt called libQGLViewer. I've noticed that for this library there was only the debug version compiled. So the issue was that I was linking against a release version of the Qt libraries, but for this library only a debug qglviewerd.lib file was available.

    Summing it up: in windows, "QApplication must be created before QWidget" can be caused by a mismatch in library version {release,debug}.

Log in to reply