Short summary:
My Qt class crashes at class construction part when calling the parent QWidget constructor. Error message shows: “First-chance exception at 0x0048fca9 in MyApplication.exe: 0xC0000005: Access violation reading location 0x00000000.”
Details:
I’m working on a project to add an overlay user interface to the one client, much like what steam has when you press “tab+shift” while game play. The original application is written in visual studio environment without any UI stuff, and I decide to use Qt to do the job.
Since the project is pretty complex to transform to a Qt project with the .pro file, I want to basically use Qt as another library (including the lib and include folder and use it in the project).
It works fine when I write it in C language. The menu show up but it’s simply not clickable. It seems like in order to make the Qt event loop to work, I have to declare my custom widget as a class rather than group of C functions.
I added my Qt class into the project, modify the include and lib folder, generate the Moc file, including it in the project. Everything compiles but when I run the program, it shows the error message. Tracking into the code shows that the constructor fails when it tried to construct QWidget.
I have searched online but most tutorial tells you how to work in Qt Creator or with Visual Studio Addin from scratch.
Solved thanks to Smi:
The problem was that I initialize QApplication later than QWidget gets initialized. Adding QApplication before calling constructor fixed the problem.
One possible reason for
QWidgetconstructor failing is that you have forgot to initialize theQApplicationobject. The Qt documentation says:And also: