微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/119619.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐