3

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?

fawick
  • 590
  • 5
  • 20

3 Answers3

2

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.

Linville
  • 3,613
  • 1
  • 27
  • 41
fawick
  • 590
  • 5
  • 20
  • did you actually test this approach, Qt Doc states aswell that these are not 100% trustable. – drahnr Feb 12 '10 at 18:20
  • Yes, I have an event filter installed on a `QWidget` which casts the event into `QMouseEvent *` and tests two modifier keys in case the event is of type `QEvent::MouseButtonDblClick. Works like a charm... – fawick Feb 15 '10 at 10:50
1

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 
lx222
  • 248
  • 1
  • 16
0

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

protected:
    void mouseDoubleClickEvent(QMouseEvent *event);

cheers

fredcrs
  • 3,558
  • 7
  • 33
  • 55