Results 1 to 17 of 17

Thread: QWidget: Must construct a QApplication before a QPaintDevice

  1. #1
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QWidget: Must construct a QApplication before a QPaintDevice

    Hi all,

    I am getting this error on my application, even before main() is executed:
    Qt Code:
    1. QWidget: Must construct a QApplication before a QPaintDevice
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. #include <cstdlib>
    2. #include "mainwindowimpl.h"
    3. #include <QApplication>
    4.  
    5. using namespace std;
    6.  
    7. int main(int argc, char ** argv)
    8. {
    9. printf("111111111111");
    10. QApplication app( argc, argv );
    11. MainWindowImpl win;
    12. win.show();
    13. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Here is the generated faulty pro file
    Qt Code:
    1. TEMPLATE = app
    2. QT = gui core
    3. CONFIG += qt warn_on console debug_and_release
    4. OBJECTS_DIR = build
    5. MOC_DIR = build
    6. UI_DIR = build
    7. FORMS = ui/mainwindow.ui
    8. HEADERS = src/mainwindowimpl.h
    9. SOURCES = src/mainwindowimpl.cpp src/main.cpp
    10. INCLUDEPATH = d:\source\qwt-5.0.2\src
    11. LIBS = d:\source\qwt-5.0.2\lib\libqwt5.a
    To copy to clipboard, switch view to plain text mode 

    Here is the working pro file:
    Qt Code:
    1. TEMPLATE = app
    2. QT = gui core
    3. CONFIG += qt warn_on console release
    4. OBJECTS_DIR = build
    5. MOC_DIR = build
    6. UI_DIR = build
    7. FORMS = ui/mainwindow.ui
    8. HEADERS = src/mainwindowimpl.h
    9. SOURCES = src/mainwindowimpl.cpp src/main.cpp
    10. INCLUDEPATH = d:\source\qwt-5.0.2\src
    11. LIBS = d:\source\qwt-5.0.2\lib\libqwt5.a
    To copy to clipboard, switch view to plain text mode 

    System is MingW, Xp, Qt 4.3.2, QDevelop 0.25.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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).

  5. #5
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Qwt itself does not contain static widgets.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Perhaps you compiled Qwt in debug mode and application in release mode?
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Quote Originally Posted by elcuco View Post
    I am linking statically
    Maybe Qwt uses a different version of Qt libraries?

  8. #8
    Join Date
    Feb 2009
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Quote Originally Posted by elcuco View Post
    I am linking statically
    In qwtconfig.pri

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

  9. #9
    Join Date
    Jul 2008
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Quote Originally Posted by jpn View Post
    Perhaps you compiled Qwt in debug mode and application in release mode?
    it is the answer!!

  10. #10
    Join Date
    Aug 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  11. #11
    Join Date
    Nov 2012
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X

    Default 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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Quote Originally Posted by Saurabh Singh View Post
    what if my application doesnot have any main function..
    Then it is not an application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    Quote Originally Posted by jacek View Post
    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:

    Qt Code:
    1. #LIBS+= -lqwt #Release
    2. LIBS+= -lqwtd #Debug
    To copy to clipboard, switch view to plain text mode 

    Cheers, Bilderbikkel

  14. #14
    Join Date
    Jun 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. class MyQtWidget(QtGui.QWidget())
    To copy to clipboard, switch view to plain text mode 

    change to

    Qt Code:
    1. class MyQtWidget(QtGui.QWidget)
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    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.

  16. #16
    Join Date
    Jul 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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!

  17. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice

    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.

Similar Threads

  1. Replies: 15
    Last Post: 21st April 2007, 18:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.