QWidget: Must construct a QApplication before a QWidget
-
Hello! The ctor means constructor. I would suggest to add this code to your
main.cpp
.#ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif
It will work only in the debug mode, so you can properly identify what causes that issue. I had the similar issue but with
"QPixmap: Must construct a QGuiApplication before a QPixmap"
, this was really annoying issue, because I could not identify it properly, but when I have used the code above I have found out what causes it and fixed it. The actual problem with my issue was thatQPixmap/QIcon
was created in theQThread
worker class and added using signals/slots to main thread. I have fixed it by sending only paths for those icons from the worker class and creating the actual icons in the main thread. Also, feel free to check out theQt
source code for this error (the above code will help you find the actual file), it will help you understand what causes it and how to fix it. -
@Cobra91151 thank you for help.
I just set those few lines of code in mymain(...)
func:int main(int argc, char *argv[]) { #ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif qInstallMessageHandler(myMessageOutput); // needed to ensure appropriate OpenGL context is created for VTK rendering. QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); std::wstring pythonHome( L"C:/Anaconda3/envs/MyEnv38" ); Py_SetPythonHome(pythonHome.c_str()); py::scoped_interpreter guard{}; // start the interpreter and keep it alive qDebug() << "Hello"; MSVApplication a(argc, argv); qDebug() << "Bye bye :)"; MainWindow w; w.show(); return a.exec(); }
As I understood from the documentation
qputenv()
sets environment variable but I can't find those two variables (QT_FATAL_WARNINGS and QT_MESSAGE_PATTERN) neither in Qt project settings or Windows environment variables.
Could you explain how to get profit from those lines of code? -
@Please_Help_me_D said in QWidget: Must construct a QApplication before a QWidget:
I can't find those two variables
You have to set them.
"Qt project settings or Windows environment variables" - of course not as those are neither project settings nor Windows environment variables (Windows does not know anything about these two variables). Those are environment variable which you can set (either in QtCreator in "Run environment" or in the terminal where you start your app). -
@jsulm I just set those variables like you can see in the picture:
Now when I launch the app I get app output:
Type: fatal\nProduct Name: \nFile: unknown\nLine: 0\nMethod: unknown\nThreadID: 11772\nThreadPtr: 0x190cb3ac290\nMessage: QWidget: Must construct a QApplication before a QWidgetI think I misunderstood something
-
@Please_Help_me_D
One thing: do you/something have a global/staticQWidget
variable (not pointer to one) anywhere? That is not allowed, as per the error message. -
@JonB all widgets in my app are created as pointers. Also I don't have any
static method()
wich returns non-pointer qwidget.
But probably VTK library has something that returns non-pointer widget. @J-Hilk asked something similar and my answer was here -
Maybe the VTK lib links against another Qt version? Really strange behavior.
-
@Christian-Ehrlicher I use Qt 5.14.2 and the person who started the topic uses Qt 5.15.0
VTK had prerelease version 9.0.0 wich works fine on my PC. Then they released full VTK 9.0.1 (newest release) wich gives me mentionned error.
Both releases were built as shared libs.Probably I should ask this question in VTK forum
-
If I use
class MSVApplication : public QApplication
then is myMSVApplication
(created in main.cpp) able to returnQWidget*
?
Please look at my implementation ofMSVApplication()
is it fine?#ifndef MSVAPPLICATION_H #define MSVAPPLICATION_H #include "core/h5core.h" #include "DockManager.h" #include "DockWidget.h" #include "DockWidgetTab.h" #include "DockAreaWidget.h" #include "DockAreaTitleBar.h" #include "DockAreaTabBar.h" #include "FloatingDockContainer.h" #include "DockComponentsFactory.h" #include <QApplication> class AppSettings; class MSVApplication : public QApplication { Q_OBJECT public: MSVApplication(int &argc, char **argv); ~MSVApplication() override; int findVisDockByName(QString dockName); void setDockManager(ads::CDockManager* dockManager); ads::CDockManager* getDockManager(); // ads::CDockManager* inherited from QWidget* ads::CDockWidget* getCurrentVisDock(); // ads::CDockManager* inherited from QWidget* QList<ads::CDockWidget*> getVisDockList(); h5core::WindowType getCurrentWindowType(); int getCurrentVisDockNum(); int getVisDockListCount(); signals: void currentVisDockChanged(ads::CDockWidget* dockWidget, int num); void visDockAdded(ads::CDockWidget* dockWidget, int num); void visDockRemoved(ads::CDockWidget* dockWidget, int num); private slots: void onFocusedDockChanged( ads::CDockWidget* old, ads::CDockWidget* now); void onDockAdded(ads::CDockWidget* visDock); void onDockAboutToBeRemovedRemoved(ads::CDockWidget* visDock); private: ads::CDockManager* dockManager; int currentVisDockNumber = -1; QList<ads::CDockWidget*> visDockList; AppSettings* appSettings; }; #endif // MSVAPPLICATION_H
-
I'm really sorry that was my mistake.
I've found out that when building newest VTK 9.0.1 my CMAKE cofiguration used to contain all Qt settings (QtDir and other Qt path to Qt cmake files they were automatically set from PATH var and I didn't check them) from python PySide2 Qt. So right after I have set the correct Qt dirs and rebuild VTK 9.0.1 it began to work fine !Thank you for your effort and especially to @Cobra91151 who spent many hours trying to solve my problem