[SOLVED] Qt based application without QApplication object



  • Hi,
    I'm wondering why my Qt program runs without any problems, although I'm never creating a QApplication object, but I'm using QMessageBoxes and QStrings, QFiles and so on.
    In documentation, it says:
    [quote]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.[/quote]So how is this possible?



  • I am fairly new to Qt so this may be incorrect. One piece of advice: The "and so on..." may be important here.

    Based on what you have said so far however I would say there are two possible answers.

    1. your program is not technically a GUI application. Sure it uses instances of QMessageBox (which requires the Widgets library) but those are self contained objects that aren't meant to be an application in and of themselves.

    2. the QApplication object IS being created in the main method (which is created automatically by the wizard when you start a new project). I suspect that many programmers may ignore this cpp file unless they have reason to change it.



  • [quote]1) your program is not technically a GUI application. Sure it uses instances of QMessageBox (which requires the Widgets library) but those are self contained objects that aren’t meant to be an application in and of themselves.[/quote]Well, how do you explain a "real" GUI application then? I thought of a GUI application as an interface that accepts user input (edits, button clicks etc.), because then I need to run qapplication object (app.exec()), so event loop will start.
    But in my case, there is no event loop, that is why I'm asking myself how I can create and display QMessageBoxes with PushButtons...

    [quote]2) the QApplication object IS being created in the main method (which is created automatically by the wizard when you start a new project). I suspect that many programmers may ignore this cpp file unless they have reason to change it.[/quote]That is not the reason in this case, because I'm not using project files/Qt project, I'm just using a simple text editor and the mingw compiler g++ & moc... I'm doing all the stuff by hand, so there is no way for Qt to create a QApplication instance..

    So I'm still interested in how my program can run without QApplication object..


  • Lifetime Qt Champion

    Hi,

    So your main doesn't contain something like:

    @
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyDialog dialog;
    dialog.exec():
    return 0;
    }@

    ?



  • [quote author="Binary91" date="1414850057"]Well, how do you explain a "real" GUI application then? I thought of a GUI application as an interface that accepts user input (edits, button clicks etc.), because then I need to run qapplication object (app.exec()), so event loop will start.
    But in my case, there is no event loop, that is why I'm asking myself how I can create and display QMessageBoxes with PushButtons...
    [/quote]

    Because you are creating and calling their methods directly. When you show a message box in modal style, control over your application is taken away from your main loop and given directly to the message box. I believe (if it works like other GUI's I am familiar with) it creates its own simple event loop to detect whether or not the button is ever pressed or to redraw the control if it is covered up temporarily by something else. Since it is a modal dialog (a dialog that is meant to be clicked before control is returned to your own program), a QApplication event loop for your own program is not necessary.

    A "real" GUI application is an application that has ownership of its own interface with GUI elements that can be interacted with or at least respond to user input. Therefore it requires its own event loop to handle that interaction.

    I would still take a look at your main loop and compare it to what SGaist posted to make sure #2 doesn't actually apply.



  • Hi again,
    ok this is going to be an interesting thread I think.

    [quote]So your main doesn’t contain something like:...[/quote]Exactly, it doesn't use anything like this..
    That is how my code looks like:
    @ using namespace std;

    int main(int param_count, char** params)
    {
    if(param_count > 1)
    {
    // lot more stuff using console (at first, this program was not planed to have any Qt based stuff, but finally I decided printing output via QMessageBoxes looks better then using the command box...
    }
    else
    {
    QMessageBox::information(0, "Warning", "No arguments were given to the application. Program will exit...", QMessageBox::Ok);
    }
    return 0;
    }@
    As you can see, this program never creates any QApplication stuff, but it calls QMessageBoxes and it seems to work great...

    [quote]Because you are creating and calling their methods directly. When you show a message box in modal style, control over your application is taken away from your main loop and given directly to the message box....
    Since it is a modal dialog (a dialog that is meant to be clicked before control is returned to your own program), a QApplication event loop for your own program is not necessary.[/quote]If I understand you right, it should then also be possible for me to create a whole QDialog without using QApplication? I mean, creating a QDialog with no parent should make it modal style, right? But due to the fact that QDialog is also a QWidget, it should be possible to create a whole complex window without using QApplication, but I don't think that this is possible (can't test it this moment, I'm on the wrong computer right now...)



  • I can't answer that literally because I am not that familiar with Qt yet. However, conceptually your own description makes the distinction that you appear to be ignoring.
    [quote]it should be possible to create a whole complex window without using QApplication[/quote]

    A message box is not a traditional window. Neither is it a "complex" dialog. And it doesn't need to respond to anything but basic events such as redraw and pushing a button/mouse clicks. You are also making the assumption that a QDialog class is all that is needed for QMessageBox functionality. It is possible (though doubtful) that QMessageBox hosts its own watered down QApplication object. In addition, there are other types of event loops and even application classes in Qt than just QApplication. So unless you know exactly how QMessageBox works, you are drawing a conclusion that isn't necessarily so.

    A third thing you need to remember is that Qt ALSO has a whole other layer that hasn't been addressed in this conversation: the OS API. With message boxes, the OS itself probably supplies the event loop because Qt more than likely simply calls the message box methods supplied by the Operating System (Windows/Mac/Linux/whatever).

    I imagine that the message loop class that you use is largely dependent on what kinds of messages your application needs to respond to. Most applications need QApplication to supply that loop but not all. Apparently your application fits in the latter category.


  • Moderators

    Hi,

    That's very interesting! What version of Qt are you using?

    In Qt 4.8 and Qt 5.x, your program will abort if you try to construct a QWidget without a QApplication. See the source code:

    I compiled the following program using Qt 5.3.2, but it refused to run:
    @
    #include <QMessageBox>

    int main(int, char**)
    {
    QMessageBox::information(0, "Warning", "No arguments were given to the application. Program will exit...", QMessageBox::Ok);
    return 0;
    }
    @



  • Oh sorry guys, I made a big mistake! You're right, JKSH, I tried to copy and compiler your code and it also refused to run, so I checked the difference between my own code and there I got the error in reasoning:
    My program ran well, as long as the passed arguments in total were more than one, so QMessageBox was never shown. When I try to run program without arguments and it tries to show QMessageBox, I get the error I aspired to:
    [quote]The application has requested the Runtime to terminate it in an unusual way....[/quote]Well, big mistake of mine, sorry for that. I was just wondering that program can even run without QApplication but with code like "QMessageBox::information()". I can imagine that creating a QMessageBox object instead of calling a static QMessageBox method would also throw this error, even when I'm not showing it...

    Nevertheless, thank's to you guys for your help.


  • Moderators

    You're most welcome. No apologies needed! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.