Distinguish between single and double click events in Qt

后端 未结 3 1630
Happy的楠姐
Happy的楠姐 2020-11-30 12:25

I have a QAbstractItemView that needs to react to single and double click events. The actions are different depending on whether it was single clicked or double

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 13:13

    Using PySide which is the Python binding of Qt 4.8 I saw that single clicks deliver a QEvent.MouseButtonPress event and double clicks deliver a single QEvent.MouseButtonPress event closely followed by a QEvent.MouseButtonDblClick. The delay is approximately about 100ms on Windows. That means you still have a problem if you need to differentiate between single and double clicks.

    The solution needs another QTimer with a slightly higher delay than the inbuilt delay (adding some overhead). If you observe a QEvent.MouseButtonPress event you start your own timer, in case of timeout it is a single click. In case of a QEvent.MouseButtonDblClick it is a double click and you stop the timer to avoid counting as single click.

提交回复
热议问题