[SOLVED] Closing QMainWIndow



  • I have two QMainWindow instances, one of which is parent to the other. The problem I'm having is that if I close the child QMainWindow using the window close button, the window closes and focus returns to the parent, BUT, if I close it via a menu item (connected to the window's close slot), both windows close and the application terminates.

    Two questions:

    1. Is it acceptable to instantiate two QMainWindow classes, with one being parent to the other, or should I use a different class for the child QMainWindow? I really need two windows with toolbars, status bar and whatnot, though.

    2. If 1) above is not an issue, how can I prevent closing the child QMainWindow from shutting down the application?



  • I don't know if there is a problem using two QMainWindow classes. There might be but if it works then I don't see an issue.

    You can override the protected closeEvent() method. You can prevent the window from closing with something like this in a subclass:

    @

    void MyWindow::closeEvent(
    QCloseEvent *Event)
    {
    if(cannot_close == true)
    {
    Event->ignore();
    return;
    }

    Event->accept();
    }
    @

    The problem where one window closes both and terminates makes sense if the second window is a child of the first. It should work this way.

    If you create a third common window and have the two QMainWindow classes children of this it should prevent this problem. Both of the children will be independent. The only downside I can think of is that you will have a third window.

    Maybe creating both QMainWindow widgets inside of main() and don't make one a child of the other? I never tried this but it might work.

    @
    int main(int argc, char *argv[])
    {
    Q_INIT_RESOURCE(appname);
    QApplication Application(argc, argv);

    MyWindow  *win1 = new MyWindow;
    MyWindow  *win2 = new MyWindow;
    
    win1->show(); 
    win2->show(); 
    
    return Application.exec();
    

    }
    @


  • Lifetime Qt Champion

    Hi,

    There's no problem with that however it's unusual. Why two QMainWindow ?

    So you have a QAction in your second window which triggers that window close slot ? Correct ?

    Where do you setup that connection ?

    By the way, what version of Qt and OS are you running ?



  • I have nested main windows in a few applications with no problem.

    You problem is mostly likely related either to auto - or manual connection of the menu slots to appropriate slots of both windows instead of one.
    But if I misunderstood and you want both windows exist independently you should not make one child of another.



  • [quote author="SGaist" date="1423606212"]There's no problem with that however it's unusual. Why two QMainWindow ?[/quote]

    I think my first question should have been worded differently. The issue at hand is that there must be two windows each sporting a toolbar and a menu and status bars. So then the question is should I use a QMainWindow for the main window and another QMainWindow for the second one (child to main) or should I use a different class for the second window, if one exists that supports toolbars and menu/status bars? The first window already has a stacked widget showing different "pages".

    [quote author="SGaist" date="1423606212"]So you have a QAction in your second window which triggers that window close slot ? Correct ?[/quote]

    Yes, the action is setup on the menu/toolbar's close action being triggered and invokes the window's close() method; this was done in Qt Creator's designer.

    [quote author="SGaist" date="1423606212"]By the way, what version of Qt and OS are you running ?[/quote]

    Using Linux and Qt's latest stable. Speaking of which, is it advisable to develop for Qt5? I don't develop in Python but I heard many awful things said of Python 3, with many developers opting to stay with Python 2.6 instead (I think). I take it the same sort of thing does not apply to Qt5 vs Qt4?



  • [quote author="alex_malyu" date="1423618036"]I have nested main windows in a few applications with no problem.[/quote]

    [quote author="alex_malyu" date="1423618036"]You problem is mostly likely related either to auto - or manual connection of the menu slots to appropriate slots of both windows instead of one. But if I misunderstood and you want both windows exist independently you should not make one child of another.[/quote]

    Well, the child window is shown by triggering an action in the main window, which means it cannot exist without the main window. I never thought that closing the child window would automatically terminate the application. The funny thing is that closing the child window via the window's own close button -- so not via the menu/toolbar action -- works as intended.

    -EDIT This is bugging me. Anyone knows what method is invoked internally by QT when the window's close button is clicked on or the user hits ALT-F4/CTRL-W ?-

    -EDIT2 Making the child window parentless still results in the application terminating when the close() method is invoked.-



  • Ok. Problem solved. It was my mistake all along as I was deleting the child window immediately upon receiving its closed signal, which, I found out just now with some searches, is a no-no; using deleteLater() instead works as intended!

    Would still appreciate some answers to the questions above, though!


  • Lifetime Qt Champion

    Since there are severals and some that are stroke through, which questions exactly ?



  • [quote author="SGaist" date="1423699961"]Since there are severals and some that are stroke through, which questions exactly ?[/quote]

    These two, please:

    1. Should I use a QMainWindow for the main window and another QMainWindow for the second one (child to main) or should I use a different class for the second window, if one exists that supports toolbars and menu/status bars? The first window already has a stacked widget showing different “pages”.

    2. Is it advisable to develop for Qt5? I don’t develop in Python but I heard many awful things said of Python 3, with many developers opting to stay with Python 2.6 instead (I think). I take it the same sort of thing does not apply to Qt5 vs Qt4?


  • Lifetime Qt Champion

    1. You can use QMainWindow. However you an also build your own widget with these features.

    2. All development forces are going in Qt 5, Qt 4 will likely get only one last release. As for python 3 VS 2, I can't tell, until now I've only used 2. You should rather try yourself to see which one suits your needs.


Log in to reply
 

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