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.
You can call QMouseEvent::modifiers() to check whether it returns the value Qt::ControlModifier.
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
}
1
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
Use this:
void Rect::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(Qt::ControlModifier == QApplication::keyboardModifiers()){
//code
}
}