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. close button to call a function
Forum Updated to NodeBB v4.3 + New Features

close button to call a function

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.0k Views 2 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.
  • R Offline
    R Offline
    russjohn834
    wrote on last edited by
    #1

    Hi,

    How do I enable the close button on the widget to do call some functions?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Do you mean the close button on a Window?
      If yes, you can override closeEvent and ask user stuff or what you need.
      Do note you must call event->accept(); to let it close or event->ignore(); to stay open.

      #include <QCloseEvent>
      void MainWindow::closeEvent (QCloseEvent *event)
      {
          QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME,
                                                                      tr("Are you sure?\n"),
                                                                      QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                      QMessageBox::Yes);
          if (resBtn != QMessageBox::Yes) {
              event->ignore();
          } else {
              event->accept();
          }
      }
      
      R 2 Replies Last reply
      4
      • mrjjM mrjj

        Hi
        Do you mean the close button on a Window?
        If yes, you can override closeEvent and ask user stuff or what you need.
        Do note you must call event->accept(); to let it close or event->ignore(); to stay open.

        #include <QCloseEvent>
        void MainWindow::closeEvent (QCloseEvent *event)
        {
            QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME,
                                                                        tr("Are you sure?\n"),
                                                                        QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                        QMessageBox::Yes);
            if (resBtn != QMessageBox::Yes) {
                event->ignore();
            } else {
                event->accept();
            }
        }
        
        R Offline
        R Offline
        russjohn834
        wrote on last edited by
        #3

        @mrjj Thanks a lot! that's exactly what I meant :)

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          Do you mean the close button on a Window?
          If yes, you can override closeEvent and ask user stuff or what you need.
          Do note you must call event->accept(); to let it close or event->ignore(); to stay open.

          #include <QCloseEvent>
          void MainWindow::closeEvent (QCloseEvent *event)
          {
              QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME,
                                                                          tr("Are you sure?\n"),
                                                                          QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                          QMessageBox::Yes);
              if (resBtn != QMessageBox::Yes) {
                  event->ignore();
              } else {
                  event->accept();
              }
          }
          
          R Offline
          R Offline
          russjohn834
          wrote on last edited by russjohn834
          #4

          @mrjj I see strange behaviour,:
          I see close button dialogue even if I press other buttons on the screen. What might be the issue?

          I do as follows:

          private:
              void closeEvent (QCloseEvent *event);
          
          
          #include <QCloseEvent>
          void MainWindow::closeEvent (QCloseEvent *event)
          {
              QMessageBox::StandardButton resBtn = QMessageBox::question( this, "My App",
                                                                          tr("Are you sure what to close the application?\n"),
                                                                          QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                          QMessageBox::Yes);
              if (resBtn != QMessageBox::Yes) {
                  event->ignore();
              } else {
                  event->accept();
              }
          }
          
          mrjjM 1 Reply Last reply
          0
          • R russjohn834

            @mrjj I see strange behaviour,:
            I see close button dialogue even if I press other buttons on the screen. What might be the issue?

            I do as follows:

            private:
                void closeEvent (QCloseEvent *event);
            
            
            #include <QCloseEvent>
            void MainWindow::closeEvent (QCloseEvent *event)
            {
                QMessageBox::StandardButton resBtn = QMessageBox::question( this, "My App",
                                                                            tr("Are you sure what to close the application?\n"),
                                                                            QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                                                                            QMessageBox::Yes);
                if (resBtn != QMessageBox::Yes) {
                    event->ignore();
                } else {
                    event->accept();
                }
            }
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @russjohn834

            Hi
            It will show when ever the MainWindow will be closed no matter if with X or calling close();

            • even if I press other buttons on the screen.

            And what does these buttons do ?

            R 1 Reply Last reply
            2
            • mrjjM mrjj

              @russjohn834

              Hi
              It will show when ever the MainWindow will be closed no matter if with X or calling close();

              • even if I press other buttons on the screen.

              And what does these buttons do ?

              R Offline
              R Offline
              russjohn834
              wrote on last edited by
              #6

              @mrjj Ah I see. Another button closes the current window and opens a new window.

              How do we make the dialogue appear only when the user press the close button?

              mrjjM 1 Reply Last reply
              0
              • R russjohn834

                @mrjj Ah I see. Another button closes the current window and opens a new window.

                How do we make the dialogue appear only when the user press the close button?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @russjohn834
                add a bool AskUser to your class and simply allow to close setting it to false for the
                cases where you don't want to ask and true where you do.

                closeEvent will be called by Qt regardless of X or close() so you have to add some logic by hand to avoid some cases.

                1 Reply Last reply
                1
                • R Offline
                  R Offline
                  russjohn834
                  wrote on last edited by
                  #8

                  @mrjj Thanks for your suggestions :)

                  1 Reply Last reply
                  0

                  • Login

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