Thread: Auto Calling Derived Fun

  1. #16
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you want the parent QPushButton object to paint itself, I would try calling QPushButton::paintEvent(event) first, then draw your ellipse.

    gg

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When my widget is shown, mBut:: paintEvent() is automatically called.
    That's to be expected. The real question is: are both QPushButton::paintEvent() and QWidget::paintEvent() also automatically called? Suppose you derive from mBut. Are mBut::paintEvent(), QPushButton::paintEvent() and QWidget::paintEvent() all also automatically called for this new class?

    Here is a real-world example of how this is done, and I need to understand how it's done. If you have used Qt before then this code should make sense.
    I do not use Qt, but I note that as a last resort you could always read the Qt source and find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    16
    Quote Originally Posted by Codeplug View Post
    If you want the parent QPushButton object to paint itself, I would try calling QPushButton:aintEvent(event) first, then draw your ellipse.

    gg
    what are you talking about? we are trying to figure out how painEvent() in the derived class gets called automatically.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's what the virtual call mechanism is for. In fact, that's the entire point of the virtual call mechanism - to have the most derived version of a function called automatically.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    we are trying to figure out how painEvent() in the derived class gets called automatically.
    No, we are not. We are trying to find out how paintEvent() in the base classes get called when the derived class' paintEvent() is invoked polymorphically, and yet the derived class' paintEvent() does not call the base classes' paintEvent()s by itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    16
    Quote Originally Posted by laserlight View Post
    That's to be expected. The real question is: are both QPushButton::paintEvent() and QWidget::paintEvent() also automatically called? Suppose you derive from mBut. Are mBut::paintEvent(), QPushButton::paintEvent() and QWidget::paintEvent() all also automatically called for this new class?


    I do not use Qt, but I note that as a last resort you could always read the Qt source and find out.
    I hear you, but to me it doesn't matter if paintEvent() in QPushButton and QWidget also get called. I just want to know how my painEvent() gets called. This to me is more of a C++ question than anything else.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I hear you, but to me it doesn't matter if paintEvent() in QPushButton and QWidget also get called. I just want to know how my painEvent() gets called. This to me is more of a C++ question than anything else.
    It makes all the difference. Refer to my post #9 and #12 in this thread. If your answer was:
    Code:
    FurtherDerived::Fun()
    then everything is easy. It is as we have explained: when you invoke a virtual function from a base class pointer or reference, the actual function called belongs to the derived class, not the base class (unless there is no derived class override of that virtual function). This is normal polymorphic behaviour.

    If paintEvent() in the base classes are also called, then it implies that the most natural reason is that the derived class override of the virtual function calls them: but you confused us by claiming that it did not and yet your answer was:
    Code:
    Base::Fun()
    Derived::Fun()
    FurtherDerived::Fun()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    16
    Quote Originally Posted by laserlight View Post
    No, we are not. We are trying to find out how paintEvent() in the base classes get called when the derived class' paintEvent() is invoked polymorphically, and yet the derived class' paintEvent() does not call the base classes' paintEvent()s by itself.
    I guess you could say it like that too, but would you not agree that MY paintEven() in mBut gets called without me having to call it? All I've done is re reimplement the paintEvent() in my derived class.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I guess you could say it like that too, but would you not agree that MY paintEven() in mBut gets called without me having to call it? All I've done is re reimplement the paintEvent() in my derived class.
    You did not call it directly, but it was invoked (possibly multiple times) by other code somewhere between and including p->show() and app.exec(). They could do this without knowing that they were dealing with an mBut object because they only needed to know that they were dealing with a pointer to an object of some class derived from QWidget, which has paintEvent().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    One you thing you have to keep in mind is that when a class marks a function virtual, it's always virtual in all the derived classes as well. Even if you don't put "virtual" on your painEvent() method - it still is. The base Qt code is calling paintEvent() somewhere in its implementation - this calls your paintEvent() since it's the most derived implementation of the virtual method.

    Assuming Qt simply calls paintEvent() through a base class pointer, yours is the first to be called. So in this case, you can explicitly call QPushButton::paintEvent() to have the button do its painting. None of the base virtuals are called for you.
    (Found an example via Google - http://www.dpi.inpe.br/terralib/html...37.html#l00145)

    gg
    Last edited by Codeplug; 06-02-2008 at 11:04 AM. Reason: remove smiley

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    16
    Ahhh, finally, I get it. Somewhere (my guess is show() ) in the base class there is a call to paintEvent() in the form of 'this->paintEvent()'.

    To my original example, it would look something like this:

    Code:
    include <iostream>
    using namespace std;
    
    class Base{
    protected:
      virtual void Fun(){ cout << " V Void Fun() in Base " << endl;};
    public:
      void Call(){this->Fun();};
    };
    
    class Derived : public Base{
    
    protected:
      void Fun(){ cout << " Fun() in Derived " << endl;};
    };
    
    int main(){
    
    Derived A1;
     A1.Call();  //Derived::Fun() is called.
    
    return 0;
    
    }
    I guess many of you were saying just that, it's juts I was confused. I'm still learning.

    Thanks again for the quick replies.

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Somewhere (my guess is show() )
    Not quite. Qt calls into the native window system (X, Win32 or whatever MacOS X uses) to create a window. In this call it must supply one or more callback functions, which are called in response to various events. One of those is "needs to be redrawn" (WM_PAINT in Win32). When the callback recognizes this event, it then calls QWindow's paintEvent(), which is resolved to your own paintEvent().

    Aren't you glad Qt hides this stuff from you?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, I made an omission in my original example which you appear to have repeated: I forgot to declare the base class virtual. For more information why, read When should my destructor be virtual?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a pure virtual function from a constructor...
    By starcatcher in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 09:12 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM