When I have to call base class in event functions?
-
Hi,
I have for example mousePressEvent(QMouseEvent *event) function. And I don't know when I have to call base class. In docs:
https://doc.qt.io/qt-5/eventsandfilters.html
We can read two things:
For example, QPaintEvent is delivered by calling QWidget::paintEvent(). This virtual function is responsible for reacting appropriately, normally by repainting the widget. If you do not perform all the necessary work in your implementation of the virtual function, you may need to call the base class's implementation.
If you want to replace the base class's function, you must implement everything yourself. However, if you only want to extend the base class's functionality, then you implement what you want and call the base class to obtain the default behavior for any cases you do not want to handle.
I don't know if I did everything what I need to and if I have to call base class.
For example in docs we have:
void MyCheckBox::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { // handle left mouse button here } else { // pass on other buttons to base class QCheckBox::mousePressEvent(event); } }
But in this topic:
https://forum.qt.io/topic/28327/big-issue-with-qt-key-inputs-for-gaming/4
Chris Kawa use implementation:void keyPressEvent(QKeyEvent *e) { keys[e->key()] = true; QWidget::keyPressEvent(e); }
The only goal in keyPressEvent was to set e->key() in QMap to true, so Chris Kawa did everything, what he want. So why he called QWidget::keyPressEvent(e) ? In example mousePressEvent in if-section "if (event->button() == Qt::LeftButton)" we don't call
QCheckBox::mousePressEvent(event);
, so I will just write:void keyPressEvent(QKeyEvent *e) { keys[e->key()] = true; }
-
@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.
-
@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.