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 ?

有帮助吗?

解决方案

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) {
        [..]
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top