I want to call QMessageBox in one of the methods in my shared library, but when I'm trying to do that, I get "QWidget: Must construct a QApplication before a QWidget". But I can't construct QApplication in my shared library, so how can I solve this issue?
Asked
Active
Viewed 1,680 times
0
-
Normally, you'd need to instantiate an instance of QApplication and then call exec() to run the GUI UI thread (main event loop). This sounds like a duplicate of something similar to this http://stackoverflow.com/questions/2150488/using-a-qt-based-dll-in-a-non-qt-application . The answer to the question is a bit old (2010) so you'd have to do a bit more research with Qt 5.xx.xx and beyond – Son-Huy Pham Feb 26 '14 at 23:11
-
Why you cant construct QApplication in your library? – Dmitry Sazonov Feb 27 '14 at 09:49
-
@DmitrySazonov: libraries shouldn't (usually) have a QApplication context, as they're usually included into a QApplication context. – Nicholas Smith Feb 27 '14 at 09:56
-
@NicholasSmith it depends on application. – Dmitry Sazonov Feb 27 '14 at 09:58
1 Answers
0
I'd say avoid creating QMessageBox (or any GUI widgets) within a shared library as it dilutes the purpose, what if you used the library with a non-GUI application?
The solution I use frequently with shared libraries is to use the signal/slots system, and then allow the QApplication to manage how it wants to display the result on screen, so emit a signal and connect it to a slot within your application that displays the QMessageBox.

Nicholas Smith
- 11,642
- 6
- 37
- 55