PDA

View Full Version : QWidget: Must construct a QApplication before a QPaintDevice



elcuco
25th December 2007, 14:16
Hi all,

I am getting this error on my application, even before main() is executed:

QWidget: Must construct a QApplication before a QPaintDevice

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:

#include <cstdlib>
#include "mainwindowimpl.h"
#include <QApplication>

using namespace std;

int main(int argc, char ** argv)
{
printf("111111111111");
QApplication app( argc, argv );
MainWindowImpl win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}


Here is the generated faulty pro file


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:


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.

marcel
25th December 2007, 14:20
Do you have any static QWidgets in your application? Static objects are instantiated before main gets control.

jpn
25th December 2007, 14:35
Alternatively, debug and release object files might be getting mixed. Try cleaning and then rebuilding the project.

elcuco
25th December 2007, 21:12
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).

e8johan
25th December 2007, 21:26
Qwt itself does not contain static widgets.

jpn
25th December 2007, 21:29
Perhaps you compiled Qwt in debug mode and application in release mode?

jacek
26th December 2007, 02:11
I am linking statically
Maybe Qwt uses a different version of Qt libraries?

burakozturk
5th March 2009, 14:09
I am linking statically

In qwtconfig.pri

CONFIG += QwtDll this line must be ->
#CONFIG += QwtDll

yleesun
6th March 2009, 03:48
Perhaps you compiled Qwt in debug mode and application in release mode?

it is the answer!!

agostain
9th September 2009, 09:37
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

Saurabh Singh
23rd November 2012, 12:19
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

wysota
23rd November 2012, 14:09
what if my application doesnot have any main function..
Then it is not an application.

Bilderbikkel
24th May 2013, 09:53
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:



#LIBS+= -lqwt #Release
LIBS+= -lqwtd #Debug


Cheers, Bilderbikkel

jengel
21st June 2013, 13:49
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.


class MyQtWidget(QtGui.QWidget())

change to


class MyQtWidget(QtGui.QWidget)

d_stranz
28th May 2014, 00:54
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.

lclemens@gmail.com
10th December 2014, 16:27
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!

d_stranz
12th December 2014, 20:22
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.