c – Qt keyPressEvent在按下W / A / S / D键时未注册

前端之家收集整理的这篇文章主要介绍了c – Qt keyPressEvent在按下W / A / S / D键时未注册前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序(与任何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;
}

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

编辑:经过这些例子后,我试图形成我的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;
}

解决方法

不确定我是否误解了一些东西,但是下面的代码运行良好,我看到日志中的所有密钥(甚至大写)除了键“w”.

在这里你有:

编辑#1:在QApplication上安装了一个事件过滤器,以获取过滤事件的对象.

myframe.pro

TEMPLATE = app

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

HEADERS += myframe.h

main.cpp中

#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();
}

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

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;
}
原文链接:https://www.f2er.com/c/119619.html

猜你在找的C&C++相关文章