Distinguish between single and double click events in Qt

后端 未结 3 1627
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 12:53

    You can find answer in the thread titled Double Click Capturing on QtCentre forum;

    You could have a timer. Start the timer in the releaseEvent handler and make sure the timeout is long enough to handle the double click first. Then, in the double click event handler you can stop the timer and prevent it from firing. If a double click handler is not triggered, the timer will timeout and call a slot of your choice, where you can handle the single click. This is of course a nasty hack, but has a chance to work.

    wysota

    0 讨论(0)
  • 2020-11-30 13:09

    It's a good UI design to make sure your single-clicks and double-clicks are conceptually related:

    Single-Click: select icon
    Double-Click: select icon and open it
    
    Single-Click: select color
    Double-Click: select color and open palette editor
    

    Notice how in these examples the single-click action is actually a subset of the double-click. This means you can go ahead and do your single-click action normally and just do the additional action if the double-click comes in.

    If your user interface does something like:

    Single-Click: select icon
    Double-Click: close window
    

    Then you are setting your users up to fail. Even if they remember what single-clicking does versus double-clicking all the time, it's very easy to accidentally move your mouse too far while double-clicking or wait too long.

    Edit:

    I'm sorry to hear that.

    In that case, I found these two articles useful:

    • Logical consequences of the way Windows converts single-clicks into double-clicks
    • Implementing higher-order clicks
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题