Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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!


  • Lifetime Qt Champion

    Hi,

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



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

    bool closedByUser;



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



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



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


  • Lifetime Qt Champion

    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



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


  • Moderators

    @Alvein

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

    it isn't

    If you can avoid it, avoid it


Log in to reply