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

Qt app doesn't close properly (still visible in task manager)



  • I've written an application, but when I use the close button on the right corner to close the app, the window is invisible, but it's still in my task manager. I am talking about the buttons that Qt provides when you create a window.

    I have an app with 3 windows. You need to go through the first window to get the second and you need to get through the second window to get the third. So I'm calling 2 hides, but I would think that if you'd press the close button it would close the entire app.
    Is there anyone else having the same issues and if so, how can I solve this?

    I've set a breakpoint to return a.exec(), but it doesn't go there.

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Login login;
        login.show();
    
        qApp->setQuitOnLastWindowClosed(false);
    
        return a.exec();
    }
    

    could it be the setQuitOnLastWindowClosed(false) that prevents it from closing?
    I need that line to not let my app crash when I show a QMessageBox.



  • @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    qApp->setQuitOnLastWindowClosed(false)

    does exactly what it says on the tin! Of course it prevents the application from automatically exiting when the last window is closed! So you'll get the behaviour you describe.

    I need that line to not let my app crash when I show a QMessageBox.

    No idea what you mean. I cannot imagine you would want to disable auto-closing on last window close, especially as you're complaining about just that keep-running behaviour.



  • @hobbyProgrammer

    Do you use a parent-child structure or three independent windows?
    Does your program exits properly, if you close all three windows?
    (If a parent window is still running in the background, your program wont exit)

    EDIT:
    @hobbyProgrammer why you removed the qApp->setQuitOnLastWindowClosed(false) from your post?
    This line is not necessary. If it is in your case, your are doing something else wrong.

    I need that line to not let my app crash when I show a QMessageBox.

    Have a look at your three windows, dialogs and messageBoxes again. Try to figure out, what's going wrong there.



  • @Pl45m4

    I don't know what I am doing wrong. I followed the examples from the Qt docs.

            QMessageBox message;                                                                
            message.setText("QMessageBoxText");
            message.setStandardButtons(QMessageBox::Yes | QMessageBox::No);                  
    
            int ret = message.exec();                                                           
    
            if(ret == QMessageBox::Yes)                                                          
            {
                qDebug() << "Yes"
            }
            else if(ret == QMessageBox::No)
            {
                        qDebug() << "No"
            }
    

    for some reason this closes my entire app, but I can't seem to figure out why.



  • @hobbyProgrammer
    It will only close your entire app if you have the default setQuitOnLastWindowClosed(true) and this message box is the last window your app has open. You should have another window open if you don't want your app to exit. I don't know why you are hiding windows as you go. I don't know what you expect to happen after you close the last window, if you intend the app to continue you would need another window open for the user to do something in....



  • @hobbyProgrammer

    Normally a MessageBox is shown above some parent Widget like QDialog, QWindow or QMainWindow. If you hide / close all widgets except your MessageBox and close the MessageBox afterwards, your program exits.

    Maybe you can replace your windows with one or two windows with a widget container like QStackedWidget or you replace widgets on runtime. This would reduce the amount of show / hide processes. But depends on you and your needs, what will work best for you. :)



  • @Pl45m4 said in Qt app doesn't close properly (still visible in task manager):

    Maybe you can replace your windows with one or two windows with a widget container like QStackedWidget or you replace widgets on runtime. This would reduce the amount of show / hide processes. But depends on you and your needs, what will work best for you. :)

    That sounds pretty good, but all the windows have different functions. The first window is to login the second is to select something and the third is a drawing app using qgraphicsview.



  • @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    replace it with the new window

    Widget, not window.

    @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    So with QStackedWidget I don't need to hide my window

    Yes, it works like a book. You can dynamically flip pages / widgets instead of showing a new window with static content.

    @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    The first window is to login the second is to select something and the third is a drawing app using qgraphicsview

    I think two windows are enough. You could use a QDialog to login. After successful login, you show your QMainWindow with your QGraphicsView and your "settings". Either at the same time or you put a QStackedWidget on your QMainWindow and flip after you set everything up, to start drawing



  • @Pl45m4 I understand, but I would (for now) really just like to find a way to make it work with 3 screens. Unless there's an easy way to change the screens to widgets, but I don't have much experience with that.
    I currently have screens with different UI's.
    I use screens with menu bars etc. so how can I convert that to a widget?



  • @hobbyProgrammer

    I've edited my last post. Check it out.

    @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    I use screens with menu bars etc. so how can I convert that to a widget?

    You use Menus in your QMainWindow, right? You can leave them as they are.
    The QStackedWidget would only affect your MainWindows CentralWidget (Content of QMainWindow)

    QWidget is the base class of nearly everything in Qt. Every widget is... well... a QWidget (or inherits it).

    See:
    https://doc.qt.io/qt-5/qwidget.html



  • @Pl45m4 no the menus are different for each screen. However, I think I can apply this to the first two screens. The third one is diferent...



  • @hobbyProgrammer

    I think you are making it more complicated than it really is or I dont understand what you are actually doing / planing to do :)
    From what I've read the last days, you are coding a more or less "simple" drawing app (with clickable and editable polygons / objects, contextMenu on rightclick and further settings in a different window.... and now with a login window at program start) Correct?



  • @Pl45m4 yes that's correct



  • @Pl45m4

    I wouldn't mind it if I had to remake the .ui file, but is there a way to keep the classes as they are?
    I really don't want 1 big class for this as it would probably be 500 lines of code. I would like it to stay organized.



  • @hobbyProgrammer
    Briefly, because you need this whole behaviour sorted out, as per the mess which is developing in your other thread, https://forum.qt.io/topic/108715/check-button-press-of-qmessagebox.

    You have two choices:

    • setQuitOnLastWindowClosed(true): Make sure your code always has a window open, till you/the user chooses to close it and exit the application.

    • setQuitOnLastWindowClosed(false): If you cannot do the above, you must recognise in code when the last of your windows is closed and then exit the application explicitly.



  • @hobbyProgrammer said in Qt app doesn't close properly (still visible in task manager):

    I really don't want 1 big class for this as it would probably be 500 lines of code

    500 LOC are not that much actually :)

    You need to know what you want. If you set setQuitOnLastWindowClosed(false) only because of your MessageBox behavior, there is something wrong / not well structured with your classes.


Log in to reply