Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How To Detect if the User Closed the window?
Forum Updated to NodeBB v4.3 + New Features

How To Detect if the User Closed the window?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 738 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yousef Alaa Hussain
    wrote on 12 Aug 2024, 18:26 last edited by
    #1

    I tried searching YouTube to find how to detect if the user clicked on the x button to close the window, but didn't manage to find, does anyone know how to do it in qt c++

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Aug 2024, 18:28 last edited by
      #2

      See https://doc.qt.io/qt-6/qwidget.html#closeEvent

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Y 1 Reply Last reply 13 Aug 2024, 13:59
      1
      • C Christian Ehrlicher
        12 Aug 2024, 18:28

        See https://doc.qt.io/qt-6/qwidget.html#closeEvent

        Y Offline
        Y Offline
        Yousef Alaa Hussain
        wrote on 13 Aug 2024, 13:59 last edited by Yousef Alaa Hussain
        #3

        @Christian-Ehrlicher I am a beginner and I didn't understand that much what is written in the doc

        J 1 Reply Last reply 13 Aug 2024, 14:04
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 13 Aug 2024, 14:00 last edited by
          #4

          Override this function and do whatever you want to do in there.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • Y Yousef Alaa Hussain
            13 Aug 2024, 13:59

            @Christian-Ehrlicher I am a beginner and I didn't understand that much what is written in the doc

            J Offline
            J Offline
            JonB
            wrote on 13 Aug 2024, 14:04 last edited by JonB
            #5

            @Yousef-Alaa-Hussain
            Qt does not issue a signal on closing a window/widget. However it does call a closeEvent() method in any widget. If you want to capture/act on that you should subclass whichever QWidget-derived the "window" is so that you can override that (virtual protected) method.

            If you really don't want to subclass you could alternatively install an event filter and look for the QEvent::close event.

            Both of these are illustrated in e.g. https://stackoverflow.com/questions/17480984/how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button. It also shows using aboutToQuit() if all you really want to know is when the whole application is being closed.

            Y 1 Reply Last reply 13 Aug 2024, 14:42
            0
            • J JonB
              13 Aug 2024, 14:04

              @Yousef-Alaa-Hussain
              Qt does not issue a signal on closing a window/widget. However it does call a closeEvent() method in any widget. If you want to capture/act on that you should subclass whichever QWidget-derived the "window" is so that you can override that (virtual protected) method.

              If you really don't want to subclass you could alternatively install an event filter and look for the QEvent::close event.

              Both of these are illustrated in e.g. https://stackoverflow.com/questions/17480984/how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button. It also shows using aboutToQuit() if all you really want to know is when the whole application is being closed.

              Y Offline
              Y Offline
              Yousef Alaa Hussain
              wrote on 13 Aug 2024, 14:42 last edited by
              #6

              @JonB thank you so much for your answer
              and i am really sorry for asking a lot of questions but,
              as you can see here i have a function here that has a simple label and text input field and two buttons save and cancel

              
              void MainWindow::WriteAnyButtonClicked()
              {
                  QWidget* WriteAnyWindow = new QWidget();
              
                  QVBoxLayout* MainLayout = new QVBoxLayout();
              
                  // Top Layout
                  QHBoxLayout* TopLayout = new QHBoxLayout();
              
                  QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: ");
                  EnterAnyLabel->setAlignment(Qt::AlignLeft);
                  EnterAnyLabel->setAlignment(Qt::AlignVCenter);
              
                  TopLayout->addWidget(EnterAnyLabel);
              
              
                  //Middle Layout
                  QHBoxLayout* MiddleLayout = new QHBoxLayout();
              
                  QTextEdit* EnterAnyTextEdit = new QTextEdit();
                  EnterAnyTextEdit->setFocus();
              
                  MiddleLayout->addWidget(EnterAnyTextEdit);
              
                  // Bottom Layout
                  QHBoxLayout* BottomLayout = new QHBoxLayout();
              
                  QPushButton* Save_Button = new QPushButton("Save");
                  Save_Button->setFixedSize(100, 50);
              
                  QPushButton* Exit_Button = new QPushButton("Cancel 🇽");
                  Exit_Button->setFixedSize(50, 50);
              
              
                  BottomLayout->addWidget(Save_Button);
                  BottomLayout->addWidget(Exit_Button);
              
              
                  MainLayout ->addLayout(TopLayout);
                  MainLayout ->addLayout(MiddleLayout);
                  MainLayout ->addLayout(BottomLayout);
              
                  WriteAnyWindow->setLayout(MainLayout);
                  WriteAnyWindow->setWindowTitle("Send By Text");
                  WriteAnyWindow->setFixedSize(400,400);
                  WriteAnyWindow->show();
              
              
                  // Button Connects
                  connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;});
              }
              
              

              and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
              do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:

              void MainWindow::closeEvent (QCloseEvent *event)
              {
                  QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender",
                                                                             tr("Are you sure?\n"),
                                                                             QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                             ,QMessageBox::Cancel);
                  if (resBtn != QMessageBox::Yes) {
                      event->ignore();
                  } else {
                      event->accept();
                  }
              }
              
              

              really?
              and thanks!

              J P 2 Replies Last reply 13 Aug 2024, 14:48
              0
              • Y Yousef Alaa Hussain
                13 Aug 2024, 14:42

                @JonB thank you so much for your answer
                and i am really sorry for asking a lot of questions but,
                as you can see here i have a function here that has a simple label and text input field and two buttons save and cancel

                
                void MainWindow::WriteAnyButtonClicked()
                {
                    QWidget* WriteAnyWindow = new QWidget();
                
                    QVBoxLayout* MainLayout = new QVBoxLayout();
                
                    // Top Layout
                    QHBoxLayout* TopLayout = new QHBoxLayout();
                
                    QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: ");
                    EnterAnyLabel->setAlignment(Qt::AlignLeft);
                    EnterAnyLabel->setAlignment(Qt::AlignVCenter);
                
                    TopLayout->addWidget(EnterAnyLabel);
                
                
                    //Middle Layout
                    QHBoxLayout* MiddleLayout = new QHBoxLayout();
                
                    QTextEdit* EnterAnyTextEdit = new QTextEdit();
                    EnterAnyTextEdit->setFocus();
                
                    MiddleLayout->addWidget(EnterAnyTextEdit);
                
                    // Bottom Layout
                    QHBoxLayout* BottomLayout = new QHBoxLayout();
                
                    QPushButton* Save_Button = new QPushButton("Save");
                    Save_Button->setFixedSize(100, 50);
                
                    QPushButton* Exit_Button = new QPushButton("Cancel 🇽");
                    Exit_Button->setFixedSize(50, 50);
                
                
                    BottomLayout->addWidget(Save_Button);
                    BottomLayout->addWidget(Exit_Button);
                
                
                    MainLayout ->addLayout(TopLayout);
                    MainLayout ->addLayout(MiddleLayout);
                    MainLayout ->addLayout(BottomLayout);
                
                    WriteAnyWindow->setLayout(MainLayout);
                    WriteAnyWindow->setWindowTitle("Send By Text");
                    WriteAnyWindow->setFixedSize(400,400);
                    WriteAnyWindow->show();
                
                
                    // Button Connects
                    connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;});
                }
                
                

                and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
                do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:

                void MainWindow::closeEvent (QCloseEvent *event)
                {
                    QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender",
                                                                               tr("Are you sure?\n"),
                                                                               QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                               ,QMessageBox::Cancel);
                    if (resBtn != QMessageBox::Yes) {
                        event->ignore();
                    } else {
                        event->accept();
                    }
                }
                
                

                really?
                and thanks!

                J Offline
                J Offline
                JonB
                wrote on 13 Aug 2024, 14:48 last edited by
                #7

                @Yousef-Alaa-Hussain
                As I wrote there are two possibilities.

                • Subclass and override closeEvent(). Your code looks reasonable.

                • If you do not want to "rearrange all of this code to an entire sperate class" (I don't see the problem, you seem to have already done it) then you can do it by installing an event filter.

                I already wrote just this and referred you to a link showing both alternatives. I am not sure what you are actually asking now.

                Y 1 Reply Last reply 13 Aug 2024, 16:03
                0
                • J JonB
                  13 Aug 2024, 14:48

                  @Yousef-Alaa-Hussain
                  As I wrote there are two possibilities.

                  • Subclass and override closeEvent(). Your code looks reasonable.

                  • If you do not want to "rearrange all of this code to an entire sperate class" (I don't see the problem, you seem to have already done it) then you can do it by installing an event filter.

                  I already wrote just this and referred you to a link showing both alternatives. I am not sure what you are actually asking now.

                  Y Offline
                  Y Offline
                  Yousef Alaa Hussain
                  wrote on 13 Aug 2024, 16:03 last edited by
                  #8

                  @JonB

                  @JonB said in How To Detect if the User Closed the window?:

                  I don't see the problem, you seem to have already done it

                  actually, this close event is for the entire window (the main window) but
                  if the user clicks a button inside this main window, another window will pop up, and I wanted to detect when the user closes the new window that popped up after the button got clicked,

                  I guess I need to make an entire new class, uh, qt is very hard as a beginner, I hope I get out of the hell of beginner

                  So now I will try installing that I don't know thing you talked about, and I will see if I can adapt with it. If there is a tutorial out there for this thing I need to install, let's hope.

                  Thank you so much, and sorry for wasting your time

                  J 1 Reply Last reply 13 Aug 2024, 17:03
                  0
                  • Y Yousef Alaa Hussain
                    13 Aug 2024, 16:03

                    @JonB

                    @JonB said in How To Detect if the User Closed the window?:

                    I don't see the problem, you seem to have already done it

                    actually, this close event is for the entire window (the main window) but
                    if the user clicks a button inside this main window, another window will pop up, and I wanted to detect when the user closes the new window that popped up after the button got clicked,

                    I guess I need to make an entire new class, uh, qt is very hard as a beginner, I hope I get out of the hell of beginner

                    So now I will try installing that I don't know thing you talked about, and I will see if I can adapt with it. If there is a tutorial out there for this thing I need to install, let's hope.

                    Thank you so much, and sorry for wasting your time

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Aug 2024, 17:03 last edited by
                    #9

                    @Yousef-Alaa-Hussain

                    another window will pop up, and I wanted to detect when the user closes the new window that popped up

                    if the "window which pops up" is a (modal) dialog then you are told when it is closed. If not a modal dialog then you will have to subclass the window/widget or use an event filter.

                    1 Reply Last reply
                    0
                    • Y Yousef Alaa Hussain
                      13 Aug 2024, 14:42

                      @JonB thank you so much for your answer
                      and i am really sorry for asking a lot of questions but,
                      as you can see here i have a function here that has a simple label and text input field and two buttons save and cancel

                      
                      void MainWindow::WriteAnyButtonClicked()
                      {
                          QWidget* WriteAnyWindow = new QWidget();
                      
                          QVBoxLayout* MainLayout = new QVBoxLayout();
                      
                          // Top Layout
                          QHBoxLayout* TopLayout = new QHBoxLayout();
                      
                          QLabel* EnterAnyLabel = new QLabel("Enter What You Want To Share: ");
                          EnterAnyLabel->setAlignment(Qt::AlignLeft);
                          EnterAnyLabel->setAlignment(Qt::AlignVCenter);
                      
                          TopLayout->addWidget(EnterAnyLabel);
                      
                      
                          //Middle Layout
                          QHBoxLayout* MiddleLayout = new QHBoxLayout();
                      
                          QTextEdit* EnterAnyTextEdit = new QTextEdit();
                          EnterAnyTextEdit->setFocus();
                      
                          MiddleLayout->addWidget(EnterAnyTextEdit);
                      
                          // Bottom Layout
                          QHBoxLayout* BottomLayout = new QHBoxLayout();
                      
                          QPushButton* Save_Button = new QPushButton("Save");
                          Save_Button->setFixedSize(100, 50);
                      
                          QPushButton* Exit_Button = new QPushButton("Cancel 🇽");
                          Exit_Button->setFixedSize(50, 50);
                      
                      
                          BottomLayout->addWidget(Save_Button);
                          BottomLayout->addWidget(Exit_Button);
                      
                      
                          MainLayout ->addLayout(TopLayout);
                          MainLayout ->addLayout(MiddleLayout);
                          MainLayout ->addLayout(BottomLayout);
                      
                          WriteAnyWindow->setLayout(MainLayout);
                          WriteAnyWindow->setWindowTitle("Send By Text");
                          WriteAnyWindow->setFixedSize(400,400);
                          WriteAnyWindow->show();
                      
                      
                          // Button Connects
                          connect(Exit_Button,&QPushButton::clicked, this,[&](){delete WriteAnyWindow;});
                      }
                      
                      

                      and i want to check when this widget is closed to automatically save the input that the user entered in the textinput,
                      do you know how to do this? do i really need to rearrange all of this code to an entire sperate class just to get this protected close event funtion and then create a funtion for it like this:

                      void MainWindow::closeEvent (QCloseEvent *event)
                      {
                          QMessageBox::StandardButton resBtn = QMessageBox::question( this, "The Sender",
                                                                                     tr("Are you sure?\n"),
                                                                                     QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                                     ,QMessageBox::Cancel);
                          if (resBtn != QMessageBox::Yes) {
                              event->ignore();
                          } else {
                              event->accept();
                          }
                      }
                      
                      

                      really?
                      and thanks!

                      P Offline
                      P Offline
                      Pl45m4
                      wrote on 13 Aug 2024, 17:13 last edited by Pl45m4
                      #10

                      @Yousef-Alaa-Hussain said in How To Detect if the User Closed the window?:

                      void MainWindow::WriteAnyButtonClicked()
                      

                      This function creates new widgets and layouts every time it's called (on button click, it seems).
                      You know that, right?!
                      It also leaks memory since you are not using the features of the QObject-tree nor deleting the pointers afterwards.
                      (Edit: you delete WriteAnyWindow forcefully, which is not recommended.)

                      However, the closeEvent looks good, as @JonB already mentioned.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 13 Aug 2024, 18:40 last edited by
                        #11

                        Hi,

                        If I have understood things correctly, wouldn't QInputDialog::getText do what you want ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        1/11

                        12 Aug 2024, 18:26

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved