Вопрос

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