Flow of control in execution



  • Can someone explain me how does the MainWindow know that its SubWindow or any of its modal dialog is closed?
    How does the control flow from app.exec() to the modal dialog and how does it return back?



  • A modal dialog that you start with exec() starts its own eventloop. That means that the call to exec only returns when that eventloop is exited.



  • If I have two different window,say mainwindow and subwindow with different class name but both derived from QMainWindow,then how does the control work between these two window?
    for example:
    on click of a button in mainwindow,if I have a slot as
    @void mainwindow::on_button_clicked()
    {
    static SubWindow *subwindow = new SubWindow(this);
    subwindow->show();
    subwindow->activateWindow();
    subwindow->raise();
    }@

    The respective forms are designed using Creator.Now when I close the subwindow,how will that be notified to the mainwindow?



  • hi,

    in your case it will not be notified. Why should it?

    you created two completely independent top level windows which have no communication.
    Please explain a bit what you want to achieve and then we can perhaps help you.



  • It won't be notified explicitly, because in this case program flow just continues as usual (you are using show() instead of exec()). That is, after the function you show, control returns to the event loop of QApplication.



  • Ok.Then how can I get control in mainwindow over my subwindow so that I get notified when the user close the window or send any event to some other window say subwindow1.



  • Setup some signals & slots to communicate between them would be one way.

    In your subwindow class, declare a signal windowClosing() and reimplement the closeEvent() like this:

    @
    SubWindow::closeEvent(QCloseEvent * event ) {
    emit windowClosing();
    QMainWindow::closeEvent(event);
    }
    @

    Then, you can connect to the signal windowClosing() from your main window.



  • I tried with the above code snippet.If I don't implement the above then also with signal and slot connection I can close my subwindow and go back to mainwindow.During this how will the control flow?Will it be in exec() loop of my mainwindow only until I close the subwindow with the respective slot?



  • Please rephrase. I don't understand what you're after.



  • sorry.In my subwindow(as in code posted in previous post) if I have a few widgets in it then will my control be w.r.t subwindow? Later how will the control be transferred to the mainwindow?



  • What is not clear, is what you mean with "control", and how that is relevant for you. At first, it seems that you were after the program execution flow, but now, it seems to me that you're after something else.

    If you really want to track execution flow of your program, then simply fire up your debugger, set a breakpoint where you want to start tracking, and step through the code line by line.



  • Hi Revu,

    please, let's step back one step and look at the use case, ok?
    What do you want to achive. Let's make a suggestion:

    • You have a main window with a button
    • if the user clicks on the button, a sub main window opens
    • if the sub window is closed what should happen from the users point of view?
    • is the first main window usable while the second one is open? (is second one modal or non modal?)


  • Hi Revu,

    Could you please explain what are you trying to achieve in general?

    If you want to have several views like in Symbian C++ you check this example:
    http://wiki.forum.nokia.com/index.php/Extending_QStackedWidget_for_sliding_page_animations_in_Qt

    Cheers,
    Leon



  • Sorry guys I posted a wrong link.

    Revu, please have a look at this discussion about multiple QMainWindow at Forum Nokia: http://discussion.forum.nokia.com/forum/showthread.php?195528-Multiple-QMainWindow-but-exit-on-one-window-exits-whole-application-in-Symbian



  • @Andre:Thank you for that tip.

    @Gerolf:

    • You have a main window with a button - Yes
    • if the user clicks on the button, a sub main window opens - Yes
    • if the sub window is closed what should happen from the users point of view? - mainwindow should be visible.
    • is the first main window usable while the second one is open? (is second one modal or non modal?) - I had used modal window.How can I make it non modal?At the same time track the execution control?

    @Leon: When the sub window is closed,how is my mainwindow is notified?To be simple,if there is a message box on button click in my mainwindow then then how will the control be transferred from button to that message box and from message box to mainwindow?

    Edit: fixed formatting; Andre



  • I think we should clarify some general points:

    a modal window means you have a local event loop running while the window is open. This is done by calling exec() on a dialog. For a top level window, I think you would have to do it on your own, by locking at the implementation of QDialog::exec().

    for example:

    @
    MyClass::foo()
    {
    YmDlg dlg;

    if(dlg.exec&#40;&#41;) // <-- this spins a local event loop only processing events for the dialog and sub widgets.
    {
    }
    

    }
    @

    exec() returns when the dialog is closed.

    if you have a main windows that you just show (non modal!) then the user could switch between the windows. If that is really possible depends on the system you are on and whether it supports window switching by the user (like with ALT+Tab on windows, I have no idea of your embedded system).

    If switching by the user is not possible, he can only handle the visible window. when you close it he can use the now visible window.

    Why do you want toi track execution control?
    The user uses the visible windows. If you need modal windows on top, make them modal. A non modal windows which ensures, other windows are not used makes itself model without being it. That makes it much more complicated.



  • [quote author="Revu" date="1302582827"]
    @Leon: When the sub window is closed,how is my mainwindow is notified?To be simple,if there is a message box on button click in my mainwindow then then how will the control be transferred from button to that message box and from message box to mainwindow?

    Edit: fixed formatting; Andre
    [/quote]

    Hi Revu,

    You can handle the button click in your mainwindow and to show the message box.

    If you need more help please share some of your source code.

    [quote author="Gerolf" date="1302593674"]I think we should clarify some general points:
    Why do you want toi track execution control?
    [/quote]

    I am wondering why do want to track execution control, too. With Qt you have full control of all GUI components and you can show,hide,close,etc. them whenever you want.



  • Hi Leon,
    Thank you for your response.I want to track the execution to know how exactly the control is getting transferred from one window to other.Is that the control of a window will be in its loop until it executes its slot?Can it be used while executing another slot of a different class?
    Any idea?



  • [quote author="Revu" date="1302668965"]I want to track the execution to know how exactly the control is getting transferred from one window to other.[/quote]

    If you need to track the execution so badly just follow Andre's advice and use a debugger.



  • I tried the debugger by setting the breakpoints.Thank You All.



  • There is no tracking of that, POINT.

    There is a main event loop inside your main function. Everything is triggered by the event loop. If you have several main windows, all are handled by the same event loop. No separate handling for separate windows.

    Modal windows spin their own modal event loop to block the main event loop during their display.

    signals-slot connections (if they are direct) are direct function calls. Queued connections are synchronised via the event loop.



  • @Geroif:Thank You so much for making a clear understanding.


Log in to reply
 

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