sam-w
sam-w

Reputation: 7687

Qt: Detecting a mousePressEvent outside of a specific QWidget

I have a widget containing multiple child objects, which the user can select by clicking them.

I want to clear the current selection when the user clicks outside the widget and I'm wondering how best to detect these clicks.

Some constraints:

Does that leave me with any other options?

Note: This app will be deployed cross-platform (at the very least Windows and Ubuntu)

Upvotes: 2

Views: 3723

Answers (3)

Martin Gemme
Martin Gemme

Reputation: 345

mousePressEvent()

The default implementation implements the closing of popup widgets when you click outside the window.

This is probably what you meant?

Upvotes: -1

Silas Parker
Silas Parker

Reputation: 8147

The QApplication::focusChanged signal is emitted, when the focused widget changes. You could check if the newly focused widget is not in your set of widgets and then deselect based on that.

You can get the currently focused widget with QApplication::focusWidget.

Upvotes: 2

Chris
Chris

Reputation: 17535

I would be inclined to simply check if your widget loses focus, using QWidget's focusOutEvent

Upvotes: 5

Related Questions