You need to handle a mousePressEvent only if the click occurred on the headerWidget widget, how can this be done without overloading it?
1 answer
Install an event hook:
headerWidget->installEventFilter(headerClickHandler); where headerClickHandler is a class instance variable with a lifetime not less than headerWidget . The interceptor class itself should be inherited from QObject and handle all necessary events inside its eventFilter() :
class HeaderClickHandler : public QObject { // Q_OBJECT не нужен — мы не работаем с сигналами и слотами protected: bool eventFilter(QObject* watched, QEvent* event) { // Так как mousePressEvent() — это лишь тонкая обёртка, вызываемая при // поступлении события QEvent::MouseButtonPress, то мы можем ловить // событие напрямую #if __cplusplus >= 201103L assert(event); #endif if(event->type() == QEvent::MouseButtonPress) { // Неявно предполагаем, что единственный перехватываемый объект — // это headerWidget // ... (производим все необходимые действия) // Позволяем передать сообщение далее, обработчику headerWidget-а return false; } } } - Thanks I got it. I need to do some actions in CsGroupView upon triggering this event. How to do it better? Emit a signal and catch it in this class? - sm4ll_3gg
- @ sm4ll_3gg,
Эмитить сигнал и отлавливать его в этом классе?- what do you mean? If the processing is done in someHeaderClickHandlermethod, what prevents to call this method directly from theeventFilter? - ߊߚߤߘ
|

