QWidget: Must construct a QApplication before a QPaintDevice
Hi all,
I am getting this error on my application, even before main() is executed:
The funny (?) thing is that I only get it when choosing "debug and build" in QDevelop's menus. When compiling for release it works flawlessly. Any ideas?
Here is my main, not even the first line (line number 9 in this code) is printed:
Code:
#include <cstdlib>
#include "mainwindowimpl.h"
#include <QApplication>
using namespace std;
int main(int argc, char ** argv)
{
printf("111111111111");
MainWindowImpl win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}
Here is the generated faulty pro file
Code:
TEMPLATE = app
QT = gui core
CONFIG += qt warn_on console debug_and_release
OBJECTS_DIR = build
MOC_DIR = build
UI_DIR = build
FORMS = ui/mainwindow.ui
HEADERS = src/mainwindowimpl.h
SOURCES = src/mainwindowimpl.cpp src/main.cpp
INCLUDEPATH = d:\source\qwt-5.0.2\src
LIBS = d:\source\qwt-5.0.2\lib\libqwt5.a
Here is the working pro file:
Code:
TEMPLATE = app
QT = gui core
CONFIG += qt warn_on console release
OBJECTS_DIR = build
MOC_DIR = build
UI_DIR = build
FORMS = ui/mainwindow.ui
HEADERS = src/mainwindowimpl.h
SOURCES = src/mainwindowimpl.cpp src/main.cpp
INCLUDEPATH = d:\source\qwt-5.0.2\src
LIBS = d:\source\qwt-5.0.2\lib\libqwt5.a
System is MingW, Xp, Qt 4.3.2, QDevelop 0.25.
Re: QWidget: Must construct a QApplication before a QPaintDevice
Do you have any static QWidgets in your application? Static objects are instantiated before main gets control.
Re: QWidget: Must construct a QApplication before a QPaintDevice
Alternatively, debug and release object files might be getting mixed. Try cleaning and then rebuilding the project.
Re: QWidget: Must construct a QApplication before a QPaintDevice
Cleaning the project and re-building does not help. This means that (probably) the problem is a static widgets.
I am using QWT, can anyone tell me if it contains a static widget? (I am linking statically).
Re: QWidget: Must construct a QApplication before a QPaintDevice
Qwt itself does not contain static widgets.
Re: QWidget: Must construct a QApplication before a QPaintDevice
Perhaps you compiled Qwt in debug mode and application in release mode?
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Originally Posted by
elcuco
I am linking statically
Maybe Qwt uses a different version of Qt libraries?
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Originally Posted by
elcuco
I am linking statically
In qwtconfig.pri
CONFIG += QwtDll this line must be ->
#CONFIG += QwtDll
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Originally Posted by
jpn
Perhaps you compiled Qwt in debug mode and application in release mode?
it is the answer!!
Re: QWidget: Must construct a QApplication before a QPaintDevice
hi
i have the same problem. i use qt-4.5.2 and qwt 5.2 on winXP. i tried all my code in linux and all works fine.
can anybody of you tell me the procedure on how to compile first qt and then qwt , in the right way?? i don't know why i get these problem! please!
thank you in advance
Re: QWidget: Must construct a QApplication before a QPaintDevice
Hi... i also have a similar question.. what if my application doesnot have any main function.. how would i construct QApplication???? Any idea????
I am using firebreath integrated with QT library it doesnt have any main function and i am using QMacCocoaViewContainer during the compilation its giving me same error "QWidget Must construct a QApplication before a QPaintDevice" how should i declare QApplication
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Originally Posted by
Saurabh Singh
what if my application doesnot have any main function..
Then it is not an application.
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Originally Posted by
jacek
Maybe Qwt uses a different version of Qt libraries?
Often, this is the cause for my problems.
In release mode, I must add -lqwt to LIBS, where in debug mode, -lqwtd should be added. Here a snippet of my .pro file:
Code:
#LIBS+= -lqwt #Release
LIBS+= -lqwtd #Debug
Cheers, Bilderbikkel
Re: QWidget: Must construct a QApplication before a QPaintDevice
If you are using a static Widget and not on purpose there is an easy solution. All you have to do is remove the parent class parenthesis.
change to
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Perhaps you compiled "xyz" in debug mode and application in release mode?
Man, this stupid thing just nailed me for two days. When I set up my release mode build in Visual Studio, I copied the list of libraries from the debug settings, then carefully edited all of the "d" suffixes off. On all but two libraries...
For the first day, I couldn't figure out what was happening - the app wouldn't even get into main(). You could see it start in Task Manager, then it immediately exited. No error message, no crash, nothing. Finally thought to link it with debug turned on, and saw the "QPixmap: Must construct a QGuiApplication first" message. Spent a wasted hour looking for QPixmap, and of course there aren't any defined as static in my code. Finally came across this thread and thought, "I wonder if I've been stupid..."
So, for anyone else using Visual Studio or the MSVC compilers to build your Qt projects, you can't mix debug and release libraries or debug libraries and a release executable. If you get the incomprehensible "QPixmap: Must construct..." error, it means that's what you've done.
Re: QWidget: Must construct a QApplication before a QPaintDevice
d_stranz is right - I had all my libs with a 'd' at the end except for Qt5Widgets and it was causing an error in setupUi() that said: Must construct a QGuiApplication before QPixmap. Thanks for the tipoff guys!
Re: QWidget: Must construct a QApplication before a QPaintDevice
Quote:
Thanks for the tipoff guys!
Yes, but I continue to be stupid. This time, I had a bare-bones QApplication I had just written. Added a few lines to the MainWindow constructor to set some things up (pushing std:: wstring constants onto a std:: vector< std:: wstring > >) and it kept crashing with a heap corruption error. After another wasted hour, discovered it was a mismatch of release and debug libraries yet again.
This nasty bug keeps appearing in different disguises. I guess the take-away lesson is that if you have perfectly good code that won't run, it has nothing to do with the code, it's the way you're building it. Hopefully I'll be quicker to realize that next time.