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. How to catch QDockWidget close window event?
Qt 6.11 is out! See what's new in the release blog

How to catch QDockWidget close window event?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 5.9k Views 3 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi!

    of any of those widgets, I can trace this method:

    void QDockWidget::closeEvent(QCloseEvent *event)
    

    That's great but my requirement is different: I need to avoid that the QDockWidget get closed directly and instead, to show a warning dialog.

    Is this possible? Thanks!


    Qt Developer

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

      @xtingray said:
      Hi
      Im not completely sure why

      void MyDockWidget::closeEvent(QCloseEvent *event)
      {
      if (USERCONFIRM()) {
      event->accept();
      } else {
      event->ignore();
      }
      }

      Do not works for Docks as it does for say mainwindow?

      X 1 Reply Last reply
      1
      • mrjjM mrjj

        @xtingray said:
        Hi
        Im not completely sure why

        void MyDockWidget::closeEvent(QCloseEvent *event)
        {
        if (USERCONFIRM()) {
        event->accept();
        } else {
        event->ignore();
        }
        }

        Do not works for Docks as it does for say mainwindow?

        X Offline
        X Offline
        xtingray
        wrote on last edited by
        #3

        @mrjj As far as I understand, the closeEvent() method is called AFTER the QDockWidget is already closed, that could be the reason your code doesn't work (just an idea).


        Qt Developer

        mrjjM 1 Reply Last reply
        0
        • X xtingray

          @mrjj As far as I understand, the closeEvent() method is called AFTER the QDockWidget is already closed, that could be the reason your code doesn't work (just an idea).

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

          @xtingray
          Ok, so that is different from other widgets as there you
          can event->ignore() to avoid it closing completely.
          I have never tried with Dock though.

          X 1 Reply Last reply
          1
          • mrjjM mrjj

            @xtingray
            Ok, so that is different from other widgets as there you
            can event->ignore() to avoid it closing completely.
            I have never tried with Dock though.

            X Offline
            X Offline
            xtingray
            wrote on last edited by xtingray
            #5

            @mrjj I was wrong, you were right!!! I was testing this and it's working!

            void MyDockWidget::closeEvent(QCloseEvent *event)
            {
                event->ignore();
            }
            

            Thanks! :D


            Qt Developer

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

              Oh, so it was like other widgets. Good testing!
              Docks are very special so you never know :)
              super!

              1 Reply Last reply
              0
              • X xtingray

                @mrjj I was wrong, you were right!!! I was testing this and it's working!

                void MyDockWidget::closeEvent(QCloseEvent *event)
                {
                    event->ignore();
                }
                

                Thanks! :D

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @xtingray
                To stop the event, you're supposed to call accept(), meaning you've handled it. Here probably it's not very important, but I'd do it like this:

                void MyDockWidget::closeEvent(QCloseEvent * event)
                {
                    event->accept();
                    QDockWidget::closeEvent(event);
                }
                

                Kind regards.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @kshegunov Actually closeEvent is kinda special in that way and ignoring it is the way to prevent window from closing. It's different from other events but documented.

                  kshegunovK 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    @kshegunov Actually closeEvent is kinda special in that way and ignoring it is the way to prevent window from closing. It's different from other events but documented.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    @Chris-Kawa
                    Oops! Good catch!

                    Read and abide by the Qt Code of Conduct

                    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