PDA

View Full Version : Why do *Event functions declared in QWidget as protected ?



burkav84
12th February 2007, 13:57
Why do *Event functions declared in QWidget as protected ? What's the reason ?

Gopala Krishna
12th February 2007, 14:13
Why do *Event functions declared in QWidget as protected ? What's the reason ?

*Event functions are mainly provided to override default response of a widget for an event and hence for a user its only useful when he overrides the class. This means only QWidget and classes derived from it are the accessors of *Event members and hence they are protected.
That too making them public would mean a possible misuse of these functions by user - say for example when a user "calls" QWidget::mouseMoveEvent() instead of QCoreApplication::sendEvent() which is what the user would have probably wanted to do.

jpn
12th February 2007, 14:16
Why do *Event functions declared in QWidget as protected ? What's the reason ?
Because they are called "by the framework" (*). One is not not supposed to call them outside by hand (this is why they are not public). One can re-implement a virtual event handler in a subclass (this is why they are not private).

(*) Notably QWidget::event() does the job of identifying different kind of events and delivering them into specialized event handlers if appropriate, like QWidget::mousePressEvent(), QWidget::keyPressEvent() and so on..

burkav84
12th February 2007, 16:00
One can re-implement a virtual event handler in a subclass (this is why they are not private).

But if it declared as private, it is possible to reimplement it in subclass :confused:
Why not to implement them as private ? It may be caused that in reimplemented event handler it may be need to call QWidget::*Event() of the base class. Correct ?

jacek
12th February 2007, 16:03
But if it declared as private, it is possible to reimplement it in subclass
If they were private, they wouldn't be accessible in subclasses.

burkav84
12th February 2007, 16:07
One can re-implement a virtual event handler in a subclass (this is why they are not private).

But if it declared as private, it is possible to reimplement it in subclass :confused:
Why not to implement them as private ? It may be caused that in reimplemented event handler it may be need to call QWidget::*Event() of the base class. Correct ?

Gopala Krishna
12th February 2007, 16:15
But if it declared as private, it is possible to reimplement it in subclass :confused:
Why not to implement them as private ? It may be caused that in reimplemented event handler it may be need to call QWidget::*Event() of the base class. Correct ?

You can implement but you can't call base method in sub classes.

Gopala Krishna
12th February 2007, 16:19
Check this link http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5

jpn
12th February 2007, 16:20
As already mentioned, you cannot access anything private (unless declared as a friend).

What's the difference between public, private, and protected? (http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5)

Edit: Gopala Krishna, did you modify the post of yours? It looks different than by the time I started replying.. :p

burkav84
12th February 2007, 17:24
Thank's Gopala Krishna and jpn for answers and links.

Gopala Krishna
12th February 2007, 17:30
Edit: Gopala Krishna, did you modify the post of yours? It looks different than by the time I started replying.. :p

No I posted 2 replies one after other. You should have started to reply after the 1st reply . Just a sheer coincidence !! ;)