If you want the parent QPushButton object to paint itself, I would try calling QPushButton::paintEvent(event) first, then draw your ellipse.
gg
If you want the parent QPushButton object to paint itself, I would try calling QPushButton::paintEvent(event) first, then draw your ellipse.
gg
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?When my widget is shown, mBut:: paintEvent() is automatically called.
I do not use Qt, but I note that as a last resort you could always read the Qt source and find out.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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.we are trying to figure out how painEvent() in the derived class gets called automatically.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It makes all the difference. Refer to my post #9 and #12 in this thread. If your answer was: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.
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.Code:FurtherDerived::Fun()
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()
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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().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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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:
I guess many of you were saying just that, it's juts I was confused. I'm still learning.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; }
Thanks again for the quick replies.
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().Somewhere (my guess is show() )
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
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?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)