本文介绍了捕获修饰键Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何使用Qt处理各种事件,并发现了一些关键修饰符无法理解的问题,例如 等.我在Qt Creator中做了一个默认的Qt GUI应用程序,扩展了QMainWindow,发现以下示例无法产生可理解的内容结果.

I am trying to understand how to handle various events with Qt and have found an issue I cannot understand with key modifiers e.g. etc. I have made a default Qt GUI Application in Qt Creator extending QMainWindow and have found that the following example does not produce understandable results.

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "Modifier " << event->modifiers().testFlag(Qt::ControlModifier);
    qDebug() << "Key " << event->key();
    qDebug() << "Brute force " << (event->key() == Qt::Key_Control);
}

在暴力破解方法返回正确值的情况下,对事件使用Modifys()函数永远不会成立.

Using the modifiers() function on the event never is true while the brute force method returns the correct value.

我做错了什么?

推荐答案

尝试使用此方法检查班次:

Try using this to check for shift:

if(event->modifiers() & Qt::ShiftModifier){...}

要检查控制权的这一点:

this to check for control:

if(event->modifiers() & Qt::ControlModifier){...}

,依此类推.对我来说很好.

and so on. That works well for me.

要获取wheel事件的修饰符,您需要检查传递给wheelEvent()方法的QWheelEvent对象:

To get the modifiers of a wheel event, you need to check the QWheelEvent object passed to your wheelEvent() method:

void MainWindow::wheelEvent( QWheelEvent *wheelEvent )
{
    if( wheelEvent->modifiers() & Qt::ShiftModifier )
    {
        // do something awesome
    }
    else if( wheelEvent->modifiers() & Qt::ControlModifier )
    {
        // do something even awesomer!
    }
}

这篇关于捕获修饰键Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 11:56