Question

I press the Ctrl key. My first function works fine:

void MainWindow::keyPressEvent(QKeyEvent *event){
    if(event->modifiers()==Qt::CTRL){               //RETURNS TRUE
        ui->widget->groupmaking=true;
    } }

In the second, i have troubles with checking what kind of key was released. (It was Ctrl)

void MainWindow::keyReleaseEvent(QKeyEvent *event){
    if(event->modifiers()==Qt::CTRL){               //RETURNS FALSE. WHY?
        ui->widget->groupmaking=false;
        ui->widget->groupexist=true;
    }
}

So, how to catch Ctrl release correctly ?

Was it helpful?

Solution

The reason for such behavior is that there is no key modifier at the moment, when you get the key release event, because the Ctrl key is already released. The right way to perform such a check would be:

void keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Control) {
        [..]
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top