Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Question regarding main() behavior



  • Hello!..
    I'm almost pretty new to C++, and never seen this before... so I don't know if this is expected...

    The image is self-explanatory:

    my form will show only these two lines are followed one after other...

    loginWindow.show();
    return a.exec();
    

    As you can see in the picture, if I separate the return a.exec() line, then my application will not show any window, is strange to me since it is supposed to automatically exit the if-else context after executing the loginWindow.show() line and execute the next line return a.exec()

    is this expected?.. if so, why does it behaves this way?.. Thanks.

    alt text

    Image source directly : https://imgur.com/rOv7aly



  • @U7Development
    I don't quite know. But whether it's the "next line" or not won't matter. You could use a debugger to step through. The window won't actually show till you hit the event loop called from a.exec(), you probably won't see it immediately after the show(), I think. Put some prints/debugs in to ensure you understand the flow of control: for example, if your CheckResourceFile() returns false then that would explain which show() is being hit and hence your claimed behaviour.



  • @U7Development This has nothing to do with C++, but it is the way Qt works!

    a is the QApplication, which holds the main Thread and the main event loop.

    When calling a.exec(), you are starting the main thread and all messages in the queue (e:g. signals handling) are executed by the event loop.

    Take time to read the documentation: QApplication



  • @KroMignon
    Sorry, I don't see how your answer addresses what the OP is asking about. He is asking where loginWindow.show() must come (according to him) relative to a.exec(). Something in his claim must be wrong, if you look at his code. He claims that after executing line #33 the next line executed is not line #45 (which I would dispute and ask for proof of).



  • @JonB Yes, there are 2 cases for which dialog will not be shoyed:
    a. when DB is null
    b. when filesystemManager.CheckResourceFile() returns false

    That's it.
    If he wan't to have the dialog shown in every case, then show() must be called every time.



  • @KroMignon
    There I agree with you (assuming what he has commented out is still commented out, you never know with posters here!), which is what I tried to say in the first place, and encouraged him to put in trace statements or use a debugger (I simply don't think he's hitting line #33). But I didn't feel that telling him to read up on QApplication would enlighten him on this. No offence intended.



  • Thanks all for your replies, there was a confusion on me, I got it working now, of course the supposed error had no sense... the line 29 was returning nullptr because my file name was incorrect...

    Thanks again and my apologies..



  • @U7Development
    If every issues has been solved, mark this topic as Solved.


Log in to reply