Qt keyPressEvent在按下W/A/S/D键时没有注册

Sex*_*ast 7 c++ qt keypress qkeyevent qevent

我有一个应用程序(与W/A/S/D键可能具有特殊导航含义的任何游戏无关),其中有一个QFrame.我重写了keyPressEvent()以便通过键盘输入文本,同时专注于此QFrame.这是我的代码:

void MyFrame::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "At least came here" << endl;
    QString text = event->text();
    qDebug() << "Text: " << text << endl;
}
Run Code Online (Sandbox Code Playgroud)

当我一次从键盘输入一个字符时,对于所有字符和数字,两个语句都会正确记录.但是对于这四个键,都没有执行日志语句,即事件处理程序甚至没有触发.怎么了?

编辑:在通过示例之后,我试图形成我的bug的最小工作示例.这就是我所拥有的.这里也有同样的问题,通过事件过滤器来完成它.仅对于这四个字符,它不会被记录.

bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{

    if (event->type() == QEvent::KeyPress)
    {
        //this never gets printed
        qDebug() << "Phew!" << endl;
        return true;

    }
    if (qobject_cast<ChildWidget *>(obj) != nullptr)
    {


        ChildWidget *option = qobject_cast<ChildWidget *>(obj);
        if (event->type() == QEvent::Enter || event->type() == QEvent::MouseMove)
        {
            //do stuff
            return true;
        }
        if (event->type() == QEvent::Leave)
        {
            //do stuff
            return true;
        }
        return QWidget::eventFilter(obj, event);
    }
    else
    {
        // pass the event on to the parent class
        return QWidget::eventFilter(obj, event);
    }
}

MyWidget::MyWidget()
{
   //do other initialization
   this->installEventFilter(this);
}

void MyWidget::keyPressEvent(QKeyEvent *event)
{
    qDebug("At least came here");
    QString text = event->text();
    //this prints out whenever I type any character, excpet W/A/S/D
    qDebug() << text;
}
Run Code Online (Sandbox Code Playgroud)

Pie*_*esu 5

QFrame类被设计为一个简单的框架对象。默认情况下,它不适用于任何输入。因此,您必须显式指定一个焦点策略,该策略允许使用QWidget::setFocusPolicy()方法检索键盘输入事件。QFrame默认有Qt::NoFocus策略。尝试将框架的focus policy属性设置为Qt::StrongFocus并再次启动您的程序。


Tar*_*rod 3

不确定我是否误解了某些内容,但以下代码运行良好,并且我看到日志中除键“w”之外的所有键(甚至大写)。

在这里你有:

Edit#1:在 QApplication 上安装事件过滤器以获取过滤事件的对象。

myframe.pro

TEMPLATE = app

QT     += widgets
SOURCES += main.cpp \
           myframe.cpp 

HEADERS += myframe.h 
Run Code Online (Sandbox Code Playgroud)

主程序

#include <QtWidgets/QApplication>
#include <QDebug>

#include "myframe.h"

class QApplicationFilter: public QObject
{
    public:
        QApplicationFilter(): QObject() {};
        ~QApplicationFilter() {};

        bool eventFilter(QObject* obj, QEvent* event)
        {
            qDebug() << "QApplicationFilter: "
                     << obj->objectName()
                     << " - event type: "
                     << event->type();
            return QObject::eventFilter(obj, event);            
        };  
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);    
    a.installEventFilter(new QApplicationFilter());

    MyFrame mf;
    mf.show();
    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

myframe.h

#ifndef MYFRAME_H
#define MYFRAME_H

#include <QtWidgets/QFrame>

class MyFrame : public QFrame
{
    Q_OBJECT

public:
    MyFrame();
    bool eventFilter(QObject *object, QEvent *event);

protected:
    void keyPressEvent(QKeyEvent *event);
};

#endif
Run Code Online (Sandbox Code Playgroud)

myframe.cpp

#include <QDebug>
#include <QKeyEvent>
#include "myframe.h"

MyFrame::MyFrame()
{
   this->installEventFilter(this);
}

bool MyFrame::eventFilter(QObject *object, QEvent *event)
{
    if (object == this && event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_W) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

void MyFrame::keyPressEvent(QKeyEvent *event)
{
    qDebug("At least came here");
    QString text = event->text();
    qDebug() << text;
}
Run Code Online (Sandbox Code Playgroud)