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. closeEvent() - Telling apart between user action and program action

closeEvent() - Telling apart between user action and program action

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 577 Views
  • 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.
  • A Offline
    A Offline
    Alvein
    wrote on 2 Feb 2020, 17:39 last edited by
    #1

    Hello,

    What I'm trying to accomplish is knowing when it's the user closing the form, or when it's our own program doing it. This is useful to avoid asking unnecessary confirmation questions.

    I'm currently doing this:

    In the form class header

    protected:
        void closeEvent(QCloseEvent *) override;
    

    In the form class source

    void LoginWindow::closeEvent(QCloseEvent *evnE)
    {
        if(nullptr==QObject::sender())
            if(QMessageBox::No==QMessageBox::question(this,"Confirm","Are you sure",QMessageBox::Yes|QMessageBox::No,QMessageBox::No))
                evnE->ignore();
            else
                evnE->accept();
        else
            evnE->accept();
    }
    

    So, in other words, if there's a sender object, the form is being closed by code. Otherwise, it's the user pressing the X at the form's top-right corner.

    However, I've not found any documentation about this and I'm not sure about the logic behind.

    May someone confirm?

    Thanks!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Feb 2020, 17:51 last edited by
      #2

      Hi,

      Why changes does it mean for your application between the two cases ?

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

      A 1 Reply Last reply 2 Feb 2020, 18:31
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 2 Feb 2020, 17:53 last edited by
        #3

        intercept all the cases where closeEvent() may be called and conditionally set a flag based on what triggered it.

        bool closedByUser;

        1 Reply Last reply
        2
        • S SGaist
          2 Feb 2020, 17:51

          Hi,

          Why changes does it mean for your application between the two cases ?

          A Offline
          A Offline
          Alvein
          wrote on 2 Feb 2020, 18:31 last edited by
          #4

          @SGaist I've used similar things (in other environments) to now if it's the user pressing the X (or whatever button the form has for this action), or it's my own code, or it's a task manager killing the app, or even it's the OS shutting down. But I'm being too picky here.

          So just look at what I posted. Imagine a window that auto-closes depending on some condition. A progress completed, a parent window instructing it to do it, etc. The auto-close operation is, well, automatic, but the users may want to close the window themselves. So, in this specific case, the program asks the users for a confirmation.

          @Kent-Dorfman Where do I set closedByUser to true? The only way (that I know) to intercept the user's manual close action is inside closeEvent() itself.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 2 Feb 2020, 18:33 last edited by Kent-Dorfman 2 Feb 2020, 18:35
            #5

            @Alvein said in closeEvent() - Telling apart between user action and program action:

            Where do I set closedByUser to true? The only way (that I know) to intercept the user's manual close action is inside closeEvent() itself.

            In any user initiated slot that directly or indirectly triggers the closeEvent()

            events are virtuals used by the framework to do the work. signals/slots are the higher level interfaces that spawn the behaviour.

            A 1 Reply Last reply 2 Feb 2020, 18:55
            1
            • K Kent-Dorfman
              2 Feb 2020, 18:33

              @Alvein said in closeEvent() - Telling apart between user action and program action:

              Where do I set closedByUser to true? The only way (that I know) to intercept the user's manual close action is inside closeEvent() itself.

              In any user initiated slot that directly or indirectly triggers the closeEvent()

              events are virtuals used by the framework to do the work. signals/slots are the higher level interfaces that spawn the behaviour.

              A Offline
              A Offline
              Alvein
              wrote on 2 Feb 2020, 18:55 last edited by
              #6

              @Kent-Dorfman The only user-thing triggering the closeEvent() is when they press the form's "X" button. I'm lost in your suggestion.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 2 Feb 2020, 20:05 last edited by
                #7

                So you know when it is triggered from the user or from your program. Set a flag to indicate that it's triggered by your program and test for it in closeEvent

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

                A 1 Reply Last reply 2 Feb 2020, 20:55
                3
                • C Christian Ehrlicher
                  2 Feb 2020, 20:05

                  So you know when it is triggered from the user or from your program. Set a flag to indicate that it's triggered by your program and test for it in closeEvent

                  A Offline
                  A Offline
                  Alvein
                  wrote on 2 Feb 2020, 20:55 last edited by
                  #8

                  @Christian-Ehrlicher Depicted that way, I see it now.
                  Not the most elegant solution but will work for this specific case.
                  Thank you.

                  Still curious about the QObject::sender() approach validity.

                  J 1 Reply Last reply 3 Feb 2020, 06:30
                  0
                  • A Alvein
                    2 Feb 2020, 20:55

                    @Christian-Ehrlicher Depicted that way, I see it now.
                    Not the most elegant solution but will work for this specific case.
                    Thank you.

                    Still curious about the QObject::sender() approach validity.

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 3 Feb 2020, 06:30 last edited by
                    #9

                    @Alvein

                    Still curious about the QObject::sender() approach validity.

                    it isn't

                    If you can avoid it, avoid it


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1

                    2/9

                    2 Feb 2020, 17:51

                    7 unread
                    • Login

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