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 dialog without closeEvent
Forum Updated to NodeBB v4.3 + New Features

Close button dialog without closeEvent

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 528 Views 1 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.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by
    #1

    Hi,

    I'm trying to setup a prompt message when the "close" button is pressed. Currently, I'm doing something like this:

    void ShoulderControl::closeEvent(QCloseEvent *event)
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, "App", "You are about to close this window, have you saved the settings?",
                                      QMessageBox::Yes|QMessageBox::No);
        if (reply == QMessageBox::Yes) {
            event->accept();
        }
        else if(reply == QMessageBox::No) {
            event->ignore();
        }
    }
    
    

    My requirement is to show the message only when a user presses the close button, NOT when by window closes on its own. Any suggestions on possible workarounds?

    JonBJ 1 Reply Last reply
    0
    • V viniltc

      Hi,

      I'm trying to setup a prompt message when the "close" button is pressed. Currently, I'm doing something like this:

      void ShoulderControl::closeEvent(QCloseEvent *event)
      {
          QMessageBox::StandardButton reply;
          reply = QMessageBox::question(this, "App", "You are about to close this window, have you saved the settings?",
                                        QMessageBox::Yes|QMessageBox::No);
          if (reply == QMessageBox::Yes) {
              event->accept();
          }
          else if(reply == QMessageBox::No) {
              event->ignore();
          }
      }
      
      

      My requirement is to show the message only when a user presses the close button, NOT when by window closes on its own. Any suggestions on possible workarounds?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @viniltc said in Close button dialog without closeEvent:

      NOT when by window closes on its own.

      What precisely does this mean? Windows do not "close on their own", just because they feel like it :) Do you mean your code may call ShoulderControl::close() explicitly? In that case use the QCloseEvent *event parameter to check the value of event->spontaneous() to distinguish.

      V 1 Reply Last reply
      1
      • JonBJ JonB

        @viniltc said in Close button dialog without closeEvent:

        NOT when by window closes on its own.

        What precisely does this mean? Windows do not "close on their own", just because they feel like it :) Do you mean your code may call ShoulderControl::close() explicitly? In that case use the QCloseEvent *event parameter to check the value of event->spontaneous() to distinguish.

        V Offline
        V Offline
        viniltc
        wrote on last edited by
        #3

        @JonB there are multiple windows in my application, and I sometimes switch between windows by closing one or another. I do not want to show the closeEvent message every time unless the user presses the close button on the window.

        JonBJ 1 Reply Last reply
        0
        • V viniltc

          @JonB there are multiple windows in my application, and I sometimes switch between windows by closing one or another. I do not want to show the closeEvent message every time unless the user presses the close button on the window.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @viniltc said in Close button dialog without closeEvent:

          and I sometimes switch between windows by closing one or another

          I really don't know what you mean. "Switching between windows" does not generate close events, and closing one window does not generate a close event in a different window. You can close a window by clicking its Close button --- QEvent::spontaneous() == true --- or by calling close() from code --- QEvent::spontaneous() == false. Which is what you asked to distinguish.

          1 Reply Last reply
          2
          • V Offline
            V Offline
            viniltc
            wrote on last edited by
            #5

            @JonB thanks a lot , this worked for me!

            void ShoulderControl::closeEvent(QCloseEvent *event)
            {
                if(event->spontaneous()){
                    QMessageBox::StandardButton reply;
                    reply = QMessageBox::question(this, "App", "Are you sure want to close this window?",
                                                  QMessageBox::Yes|QMessageBox::No);
                    if (reply == QMessageBox::Yes) {
                        event->accept();
                    }
                    else if(reply == QMessageBox::No) {
                        event->ignore();
                    }
                }
            }
            
            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