0

How can one get Ctrl+Left mouse click event in Qt widget. I am able to get key event from QObject::keyPressEvent() and mouse click from QObject::mousePressEvent(). But I need to capture both in the same function. Can someone give some pointer to right direction. Thanks.

Oli
  • 1,313
  • 14
  • 31

3 Answers3

2

You can try to use an additional variable, like:

private:
    bool ctrlIsPressed = false;

protected:
    void keyPressEvent(QKeyEvent *event) 
    { 
        if( event->key() == Qt::Key_Control ) 
           ctrlIsPressed = true; 
    }
    void keyReleaseEvent(QKeyEvent *event)
    { 
        if( event->key() == Qt::Key_Control ) 
           ctrlIsPressed = false; 
    }

    void mousePressEvent()
    {
        if( ctrlIsPressed )
            // ... Your code
    }
JoshThunar
  • 452
  • 1
  • 6
  • 15
2

You can call QMouseEvent::modifiers() to check whether it returns the value Qt::ControlModifier.

wedesoft
  • 2,781
  • 28
  • 25
0

Check out this Stackoverflow.com question. I think it is exactly the issue you are having.

How to detect the modifier key on mouse click in Qt

Community
  • 1
  • 1
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79