Results 1 to 15 of 15

Thread: How to fix error: Must construct a QApplication before a QWidget

  1. #1

    Default How to fix error: Must construct a QApplication before a QWidget

    Hello, how can I fix the "QWidget: Must construct a QApplication before a QWidget" error? I started receiving this error and I do not know what caused it. I tried undoing recent changes and I commented out all of the code from the main() function.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    You fix it by doing exactly what the error message says. You must construct a QApplication instance before you try to construct any QWidget objects. Usually the QApplication is constructed as the very first thing in your main() function.

  3. #3

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    I have a QApplication constructed as the first line of the program. I did not change the QApplication and it has stopped working. The program crashes at the first line in my program: QApplication app(argc, argv);

    I have another program that started doing the same thing without the error message in debug.
    Same problem with a separate installation of Qt.
    Last edited by vanillac; 30th January 2015 at 00:51.

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

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    One problem that will cause this is if you create a static QWidget on the stack before your create QApplication. (For example, your main() function declares a QWidget xyz; before it declares QApplication a; ).

    Something else that will cause this is if you have mixed and matched Release and Debug DLLs on Windows. Check your link parameters to make sure all of your Qt DLLs are the "D" version.

  5. #5

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    The first line of main() initializes the QApplication. I didn't make any changes to any DLLs or anything like that. I didn't change the debug/release version either.

  6. #6
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    This error is a well known symptom indicating a build problem ( usually mixing debug/release ) on Windows.

    Uwe

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Does your program contain any global variable whose initialization may cause a QWidget to be instantiated? Have you tried to run a debugger to go through every object creation before the QApplication itself is instantiated?

  8. #8

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Quote Originally Posted by yeye_olive View Post
    Does your program contain any global variable whose initialization may cause a QWidget to be instantiated? Have you tried to run a debugger to go through every object creation before the QApplication itself is instantiated?
    I tried using the debugger briefly but I have no idea how to use it. I think it might be because I moved the initialization of a QObject-based class from the main() to the inside of another class.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Do a complete clean rebuild of the program in either debug or release mode.

    If the problems persists then look for any QWidget derived class being declared in a file scope anywhere in your code. These will all be initialised before main() is entered.

  10. #10

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Quote Originally Posted by ChrisW67 View Post
    Do a complete clean rebuild of the program in either debug or release mode.

    If the problems persists then look for any QWidget derived class being declared in a file scope anywhere in your code. These will all be initialised before main() is entered.
    Rebuild did not work. I do not have any static / file scope variables.

    I get a crash window stating "program has stopped working. Windows can check online for solution to the problem." Maybe this helps:

    Fault Module Name: .4\mingw491_32\bin\Qt5Cored.dll!__cxa_throw_bad_ar ray_new_lengt

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    rebuild did not work
    Clearly the result of running the program has changed.

    Your program is trying to use a debug library. Is it a debug build? Run the program in your debugger and get the backtrace when it crashes. Read down the backtrace until you find a line of your code and maybe that will give you a clue.

  12. #12

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Quote Originally Posted by ChrisW67 View Post
    Clearly the result of running the program has changed.

    Your program is trying to use a debug library. Is it a debug build? Run the program in your debugger and get the backtrace when it crashes. Read down the backtrace until you find a line of your code and maybe that will give you a clue.
    It's the same error but I didn't include it in my earlier posts. When I run with the debugger, I get this error additionally:

    An exception was triggered.
    Exception at 0x77d7365e5, code:0xc0000139: DLL entry point not found, flags=0x0.
    During startup program exited with code 0xc0000139.

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Your program is trying to load a version of a dll other than the one it was linked against. Alternatively, a Qt based DLL you are linking against is trying to load a library of incompatible type i.e. Your Qt5 program is a release build but is loading Qwt built against Qt4 or debug libraries. We do not have enough information to resolve this for you.

  14. #14
    Join Date
    Aug 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    Hi I am new to Qt and development in general. I am getting the same error. I am working through the tutorial in the documentation. However, the tutorial does not mention anything about a main() function. Is this function part of the test project?

    Here is my test code:

    void TestHelloTest::TestGui()
    {
    QLineEdit line_edit;

    QTest::keyClicks(&line_edit, "hello world");
    QCOMPARE(line_edit.text(), QString("hello world"));
    }

    This is my .pro file

    #-------------------------------------------------
    #
    # Project created by QtCreator 2015-09-01T08:05:09
    #
    #-------------------------------------------------

    QT += widgets testlib

    TARGET = tst_testhellotest
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG += qteslib

    TEMPLATE = app

    SOURCES += tst_testhellotest.cpp
    DEFINES += SRCDIR=\\\"$$PWD/\\\"

    HEADERS +=


    Should I include a main() function in my test. If so, what goes in the main? If not, where does the main come in?

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to fix error: Must construct a QApplication before a QWidget

    You seem to be missing the QTEST_MAIN() line in your unit test code.

    Cheers,
    _

Similar Threads

  1. Replies: 16
    Last Post: 12th December 2014, 20:22
  2. QWidget: Must construct a QApplication before a QWidget
    By Connailo in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2014, 08:02
  3. Replies: 42
    Last Post: 25th July 2012, 21:39
  4. Replies: 2
    Last Post: 25th July 2011, 08:08
  5. Replies: 0
    Last Post: 20th April 2009, 17:45

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.