Qt Message Box, QApplication and QPaintDevice

May 29, 2011 at 2:06pm
For my event log, I wanted to display an error message box. I avoided WinAPI (in which it is really easy...) for portability.

After downloading and building Qt, I used the following code to try to show a message box:
return QMessageBox::warning(0, Caption.c_str(), Message.c_str());

But when I build it in debug configuration it says "QWidget: Must construct a QApplication before a QPaintDevice" (and in release config it just dies silently).

I don't really know much about Qt yet, but does this mean I'm going to have to create a whole Qt Application object just to display a message box?
Or is there some other fix?

Is it any easier with GTK, wxWidgets, etc?
May 29, 2011 at 7:51pm
Xander314 wrote:
I don't really know much about Qt yet, but does this mean I'm going to have to create a whole Qt Application object just to display a message box?


1
2
3
4
5
6
7
int main(int argc, char** argv)
{

    QApplication app(argc, argv);
    QMessageBox::warning(0, QString("Hello"), QString("World"));

}
May 29, 2011 at 8:07pm
I know I can do that, but this is supposed to be a standalone component in a static library, and I don't want the user having to create the Qt app.

Can I give the application to the log as a member, or am I not allowed multiple QApplications at once? Also, don't I have to call app.quit() at the end.
May 29, 2011 at 8:30pm
Well, to have anything GUI related happen in QT you need a QApplication object.

From QT documentation wrote:
Since the QApplication object does so much initialization, it must be created before any other objects related to the user interface are created.


Can I give the application to the log as a member, or am I not allowed multiple QApplications at once?
From QT documentation wrote:
For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time.


Also, don't I have to call app.quit() at the end.
---- Possibly not in our simple example ----
From QT documentaion wrote:
Generally, no user interaction can take place before calling QApplication::exec(). As a special case, modal widgets like QMessageBox can be used before calling exec(), because modal widgets call exec() to start a local event loop.


Jun 1, 2011 at 8:25am
Thanks for your help, and sorry it's taken me a few days to reply.

For now, I only need Qt support for an error message box, but I suppose I might use it elsewhere later. Thus, shall I just have a global scope QApplication as part of the library? It would seem silly to require the user to create it.
Topic archived. No new replies allowed.