Qt: Detecting a mousePressEvent outside of a specific QWidget

纵饮孤独 提交于 2019-12-24 03:30:21

问题


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)


回答1:


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




回答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.




回答3:


mousePressEvent()

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

This is probably what you meant?



来源:https://stackoverflow.com/questions/8186015/qt-detecting-a-mousepressevent-outside-of-a-specific-qwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!