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.
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.
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.
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.
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.
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.
This error is a well known symptom indicating a build problem ( usually mixing debug/release ) on Windows.
Uwe
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?
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
Clearly the result of running the program has changed.rebuild did not work
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.
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?
You seem to be missing the QTEST_MAIN() line in your unit test code.
Cheers,
_
Bookmarks