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. Overriding closeEvent without child class

Overriding closeEvent without child class

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 3.2k 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.
  • Q Offline
    Q Offline
    qmad
    wrote on last edited by
    #1

    I saw an application with following override

    void QDockWidget::closeEvent(QCloseEvent* e)
    {
      //handling close event here
    }
    

    How it is not a redefinition? Qt must be defining the QDockWidget::closeEvent() function by itself. How can we redefine that function? I know C++ override, but I thought it is possible only by creating a child class. But there is no child class in this this case. By the way the above code is working perfectly fine !! How?

    raven-worxR 1 Reply Last reply
    0
    • Q qmad

      I saw an application with following override

      void QDockWidget::closeEvent(QCloseEvent* e)
      {
        //handling close event here
      }
      

      How it is not a redefinition? Qt must be defining the QDockWidget::closeEvent() function by itself. How can we redefine that function? I know C++ override, but I thought it is possible only by creating a child class. But there is no child class in this this case. By the way the above code is working perfectly fine !! How?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @qmad said in Overriding closeEvent without child class:

      But there is no child class in this this case.

      i am not quite sure what your question is, but i try it anyway.
      The QWidget base class implements closeEvent(). Additionally it's declared as virtual

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4
      • A Offline
        A Offline
        ambershark
        wrote on last edited by ambershark
        #3

        QDockWidget's parent is QWidget. So it is a child to QWidget. And QWidget::closeEvent() is a protected virtual so it can be overridden.

        Likewise if you wanted to override closeEvent in a class you derive from QDockWidget you would be able to do that as well.

        So the override is legit since QDockWidget is a child of QWidget. ;)

        Edit: I really should have read @raven-worx 's answer before I wrote that since he said exactly what I said.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qmad
          wrote on last edited by
          #4

          I can understand if there is a child class of QDocWidget and closeEvent is overriding in it. In this case simply there is a QDockWidget::closeEvent(QCloseEvent* e){----} code. I was assuming that this definition will be already there in Qt libraries. Do you mean, in QtDocWidget, it is only a declaration and they left definition of us ?

          jsulmJ A 2 Replies Last reply
          0
          • Q qmad

            I can understand if there is a child class of QDocWidget and closeEvent is overriding in it. In this case simply there is a QDockWidget::closeEvent(QCloseEvent* e){----} code. I was assuming that this definition will be already there in Qt libraries. Do you mean, in QtDocWidget, it is only a declaration and they left definition of us ?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @qmad Where is this code from?

            void QDockWidget::closeEvent(QCloseEvent* e)
            {
              //handling close event here
            }
            

            It is just a plain old C++ method definition, which in this case happens to be an inherited virtual method, hence it is overridden in QDockWidget.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Q 1 Reply Last reply
            0
            • Q qmad

              I can understand if there is a child class of QDocWidget and closeEvent is overriding in it. In this case simply there is a QDockWidget::closeEvent(QCloseEvent* e){----} code. I was assuming that this definition will be already there in Qt libraries. Do you mean, in QtDocWidget, it is only a declaration and they left definition of us ?

              A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              @qmad I don't think you get it. The QDockWidget IS the child class where it is overridden. You can of course derive from QDockWidget and override it again yourself.

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @qmad Where is this code from?

                void QDockWidget::closeEvent(QCloseEvent* e)
                {
                  //handling close event here
                }
                

                It is just a plain old C++ method definition, which in this case happens to be an inherited virtual method, hence it is overridden in QDockWidget.

                Q Offline
                Q Offline
                qmad
                wrote on last edited by qmad
                #7

                @jsulm

                Let us say class A is defined in a library as follows

                class A
                {
                  public:
                   virtual int fun();
                };
                
                int A::fun()
                {
                   cout<<"Called from library";
                }
                

                And in my main.cpp can I have below code?

                main()
                {
                  A a;
                  a.fun();
                }
                
                int A::fun()
                {
                  cout<<"called from Main";
                }
                
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  OH! I think I see what you are talking about. The QDockWidget::closeEvent override was in some random cpp file. Not in the Qt files. I thought you were confused on inheritance and virtual functions, but really you're saying someone just randomly redefined a virtual from a class it wasn't part of?

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  Q 1 Reply Last reply
                  0
                  • A ambershark

                    OH! I think I see what you are talking about. The QDockWidget::closeEvent override was in some random cpp file. Not in the Qt files. I thought you were confused on inheritance and virtual functions, but really you're saying someone just randomly redefined a virtual from a class it wasn't part of?

                    Q Offline
                    Q Offline
                    qmad
                    wrote on last edited by qmad
                    #9

                    @ambershark
                    Yes
                    In my opinion, the only way it can work is Qt left the closeEvent() function undefined inside QDocWidget class.
                    Or is there any C++ or Qt magic I am not aware of?

                    A 1 Reply Last reply
                    1
                    • Q qmad

                      @ambershark
                      Yes
                      In my opinion, the only way it can work is Qt left the closeEvent() function undefined inside QDocWidget class.
                      Or is there any C++ or Qt magic I am not aware of?

                      A Offline
                      A Offline
                      ambershark
                      wrote on last edited by
                      #10

                      @qmad Now that I get what you're asking that is indeed weird. The one thing I can think of as well is that it is not defined in Qt itself, so there is no naming conflict when redefining it.

                      Seems like a dangerous game that author was playing regardless though. I'd like to write some test code and play with that a bit more. It doesn't seem valid. :)

                      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                      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