2

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:

  • It's not really viable to eat the mousePressEvents of surrounding objects through eventFilter.
  • If I can avoid using grabMouse(), I'd like to, because of this warning from the docs:

    Warning: Bugs in mouse-grabbing applications very often lock the terminal. Use this function with extreme caution, and consider using the -nograb command line option while debugging.

Does that leave me with any other options?

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

sam-w
  • 7,478
  • 1
  • 47
  • 77
  • 1
    An event filter doesn't have to "eat" an event. It can intercept it, look at it and then allow it to carry on to the intended target by returning false from the eventFilter() method. – Arnold Spence Nov 18 '11 at 17:12
  • Thanks Arnold, I had thought that returning `false` from `eventFilter` just told the event system to ignore the event and deallocate any resources assigned to it. – sam-w Nov 18 '11 at 17:48

3 Answers3

5

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

Chris
  • 17,119
  • 5
  • 57
  • 60
2

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.

Silas Parker
  • 8,017
  • 1
  • 28
  • 43
-1

mousePressEvent()

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

This is probably what you meant?

sam-w
  • 7,478
  • 1
  • 47
  • 77
Martin Gemme
  • 345
  • 3
  • 17
  • That paragraph in full: *The default implementation implements the closing of **popup widgets** when you click outside the window. **For other widget types it does nothing**.* My widget is not a popup widget, so no, I'm afraid that's not what I was after. Also, I've already accepted a solution above that does everything I need it to. :) – sam-w Nov 20 '11 at 09:26