When I have to call base class in event functions?
-
@qwe3 said in When I have to call base class in event functions?:
So why he called QWidget::keyPressEvent(e)
Because the base class might do something with this event. So it depends on what you want to achieve.
-
@Christian-Ehrlicher Thank you for reply. But what with Chris Kawa's example? Here I have to call
QWidget::keyPressEvent(e);
? -
@qwe3 said in When I have to call base class in event functions?:
But what with Chris Kawa's example? Here I have to call QWidget::keyPressEvent(e);?
As I said - it depends. When you want that the base class can handle key press events then yes. If you don't then no.
-
@Christian-Ehrlicher And I don't undestand it. For example I have myWidget class and his base class is QWidget. I don' see any reason, why QWidget will be need QMouseEvent - only myWidget need that event, In mousePressEvent of myWidget I will do everything what I need.
And in Chris Kawa's example I don't know why he called base class. What this base class do with this event?
-
@Christian-Ehrlicher And I don't undestand it. For example I have myWidget class and his base class is QWidget. I don' see any reason, why QWidget will be need QMouseEvent - only myWidget need that event, In mousePressEvent of myWidget I will do everything what I need.
And in Chris Kawa's example I don't know why he called base class. What this base class do with this event?
@qwe3 said in When I have to call base class in event functions?:
What this base class do with this event?
-
@Christian-Ehrlicher Hmmm... I still don't undestand it very well.
If you reimplement this handler, it is very important that you call the base class implementation if you do not act upon the key.
What means "act upon the key"?
Chris Kawa did:
keys[e->key()] = true;
So I think, he act upon the key.
EDIT:
Can you write small example, where I will se what means "act upon the key"? -
It's clearly documented what the base class does and therefore what's not going to work when you don't call it:
"The default implementation closes popup widgets if the user presses the key sequence for QKeySequence::Cancel (typically the Escape key). Otherwise the event is ignored, so that the widget's parent can interpret it."
-
@Christian-Ehrlicher And only this? This escape sequence? So when my program doesn't use popup widgets there is no matter if I call base class or no?
EDIT What with paintEvent? in docs
https://doc.qt.io/qt-5/qwidget.html#paintEvent
I don't see any information about things, which will be different, when I don't call base class. -
As already said more than once - if you want to paint by yourself, don't call the base class. If you want to add something, call the base class and then do your painting. I don't see anything Qt specific here - it's the nature of virtual c++ functions.
-
@Christian-Ehrlicher Now I understand call base class in paintEvent:
void myRadio::paintEvent(QPaintEvent *event) { QRadioButton::paintEvent(event); // this is important to draw radio button circle QPainter painter(this); painter.drawLine(0,0,20,20); }
Perfect. Thank you.
But I still don't understand this keyPressEvent. Is there any difference in simple myWidget, which inherits QWidget and when this class don't use popup widgets?
-
@qwe3 said in When I have to call base class in event functions?:
which inherits QWidget and when this class don't use popup widgets?
According the documentation - no.