PDA

View Full Version : How to fix error: Must construct a QApplication before a QWidget



vanillac
29th January 2015, 17:43
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.

ChrisW67
29th January 2015, 19:28
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.

vanillac
29th January 2015, 20:46
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.

d_stranz
30th January 2015, 01:49
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.

vanillac
30th January 2015, 02:10
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.

Uwe
30th January 2015, 08:27
This error is a well known symptom indicating a build problem ( usually mixing debug/release ) on Windows.

Uwe

yeye_olive
30th January 2015, 10:14
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?

vanillac
30th January 2015, 18:14
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.

ChrisW67
30th January 2015, 21:27
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.

vanillac
31st January 2015, 01:02
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

ChrisW67
31st January 2015, 01:49
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.

vanillac
31st January 2015, 02:00
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.

ChrisW67
31st January 2015, 02:20
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.

jolema
2nd September 2015, 15:10
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?

anda_skoa
2nd September 2015, 16:52
You seem to be missing the QTEST_MAIN() line in your unit test code.

Cheers,
_