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

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 668 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.
  • AlveinA Offline
    AlveinA Offline
    Alvein
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      AlveinA 1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on 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
        • SGaistS SGaist

          Hi,

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

          AlveinA Offline
          AlveinA Offline
          Alvein
          wrote on 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
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by Kent-Dorfman
            #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.

            AlveinA 1 Reply Last reply
            1
            • Kent-DorfmanK Kent-Dorfman

              @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.

              AlveinA Offline
              AlveinA Offline
              Alvein
              wrote on 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
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                AlveinA 1 Reply Last reply
                3
                • Christian EhrlicherC Christian Ehrlicher

                  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

                  AlveinA Offline
                  AlveinA Offline
                  Alvein
                  wrote on 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.HilkJ 1 Reply Last reply
                  0
                  • AlveinA Alvein

                    @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.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on 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

                    • Login

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