Qt 小部件不接收 keyPressEvent [英] Qt widget does not receive keyPressEvent

查看:97
本文介绍了Qt 小部件不接收 keyPressEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的子小部件没有得到 keyPressEvents,而如果我把相同的小部件作为顶级窗口,它会.我尝试设置它获得焦点,但它对此没有影响.代码如下,显示了我尝试开始工作的内容.

My child widget does not get keyPressEvents, while if I put the same widget as top level window, it does. I try to set it get focus, but it has no effect on this. Code is below, showing what I try to get to work.

#include <QApplication>
#include <QKeyEvent>
#include <QLCDNumber>
#include <QLabel>
#include <QVBoxLayout>

class DigitSummer: public QLCDNumber {
    Q_OBJECT

public:
    DigitSummer(QWidget *parent = nullptr) : QLCDNumber(parent) {
    }

protected:
    void keyPressEvent(QKeyEvent *event) override {
        display(intValue() + event->text().toInt());
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

#if 1 // this version does not work, number does not increase
    QWidget widget;
    widget.setLayout(new QVBoxLayout());
    widget.layout()->addWidget(new QLabel("Press digits!"));
    DigitSummer summer; // in stack: must be after widget to avoid child delete
    widget.layout()->addWidget(&summer);
    widget.setFocusProxy(&summer); // I notice no effect!
    widget.show();

#else // this version works, number grows with keypresseas
    DigitSummer summer;
    summer.show();
#endif

    return a.exec();
}

#include "main.moc"

对于完整的,.pro 文件也是一样的:

And for completenes, .pro file for the same:

QT += core gui widgets
TARGET = QtMCVE
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11
QMAKE_CXXFLAGS += -Wall -Wextra
SOURCES += main.cpp

如何修复小部件以接收关键事件?

How to fix the widget to receive key events?

这个相关问题建议安装事件过滤器,但我不想这样做,必须有一种自包含的方法来修复小部件本身.

This related question suggests installing event filter, but I don't want to do that, there must be a self-contained way to fix the widget itself.

推荐答案

我认为你需要设置 焦点策略 在它接受键盘输入之前.在您的 ctor 中尝试...

I think you need to set the focus policy for the widget before it will accept keyboard input. In your ctor try...

setFocusPolicy(Qt::StrongFocus);

话虽如此,我真的不知道为什么顶级和非顶级小部件的行为会有所不同.

Having said that, I'm really not sure why the behaviour would differ for top-level and non-top-level widgets.

问题代码的工作版本:

#include <QApplication>
#include <QKeyEvent>
#include <QLCDNumber>
#include <QLabel>
#include <QVBoxLayout>

class DigitSummer: public QLCDNumber {
    Q_OBJECT

public:
    DigitSummer(QWidget *parent = nullptr) : QLCDNumber(parent) {
        setFocusPolicy(Qt::StrongFocus);
    }

protected:
    void keyPressEvent(QKeyEvent *event) override {
        display(intValue() + event->text().toInt());
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget widget;
    widget.setLayout(new QVBoxLayout());
    widget.layout()->addWidget(new QLabel("Press digits!"));
    widget.layout()->addWidget(new DigitSummer);
    widget.show();

    return a.exec();
}

#include "main.moc"

这篇关于Qt 小部件不接收 keyPressEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆