Overriding closeEvent without child class



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


  • Moderators

    @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


  • Moderators

    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.



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


  • Moderators

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


  • Moderators

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



  • @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";
    }
    

  • Moderators

    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?



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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.