开发者

Qt: Detect Double-Click with Modkey (Shift, CTRL, etc.)

开发者 https://www.devze.com 2022-12-20 12:24 出处:网络
How can I detect whether a double click on a QWidget (QStatusBar, in my case) occured while a modifier key was held down?

How can I detect whether a double click on a QWidget (QStatusBar, in my case) occured while a modifier key was held down?

I can开发者_如何转开发 overload void QWidget::mouseDoubleClickEvent ( QMouseEvent * event ) to get the double click, but how can I sure whether the widget receives the key events when it might not have the focus?


I found the answer:

QMouseEvent is derived from QInputEvent and that has a method called modifiers():

From the Qt documentation:

Returns the keyboard modifier flags that existed immediately before the event occurred.


If you have a SLOT for your (Mouse)Event or Signal, you can test the modifiers there:

Qt::KeyboardModifiers modifiers  = QApplication::queryKeyboardModifiers ();
if(modifiers.testFlag( Qt::ControlModifier )){
  qDebug() << "CTRL was hold when this function was called";
}
else{
  qDebug() << "CTRL wasn't hold";
}

//SHIFT    = Qt::ShiftModifier
//CTRL     = Qt::ControlModifier
//ALT      = Qt::AltModifier 


Just to add More information in your QWidget you only need to override this method

protected:
    void mouseDoubleClickEvent(QMouseEvent *event);

cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消