how to intercept all events for a QWidget

If you search google for how to capture events, most articles you get will teach you install event filter for child objects, then rewrite the eventFilter function in parent. This way you will intercept all events in the parent’s eventFilter function before they reach the child objects.  But how to get all events for a widget itself? The answer is install event filter for the widget itself and overload the eventFilter function in the widget’s subclass,i.e., in the constructor of MyWidget:

installEventFilter(this);

Rewrite the eventFilter in the definition of MyWidget

bool eventFilter(QObject * watched, QEvent * event)
{

}

Sometimes it is useful to get all events happening on a widget so you can investigate what is happening. For example, if you want to simulate a mouse drag-drop event, you may find just simulating the mousePress, mouseMove, and mouseRelease events is not enough. By using this method, you can know other events that is happening at drag-drop time.

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.