【问题标题】:Manage keyboard events through class in Qt C++通过 Qt C++ 中的类管理键盘事件
【发布时间】:2014-11-25 19:12:48
【问题描述】:

我正在尝试检索密钥,然后将其传递给窗口类... 我坚持... 这是我的代码:

#include "mainwindow.h"
#include "game.h"
#include <QApplication>
#include <QKeyEvent>
#include <QString>

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

    Game g(4);
    MainWindow w;

    w.doSomething();

    while(QString text = keyPressEvent(QKeyEvent *e)->text()){ // this is not working
        g.action(text);
        w.update(g.getState());
    }

    w.show();

    return a.exec();
}

我应该将 Game 类包装在 w 类中作为私有成员还是什么? 我试过...没有成功。

谢谢。

【问题讨论】:

    标签: c++ qt events object qt4


    【解决方案1】:

    keyPressEvent是一个QWidget的成员函数,所以如果你需要监听按键事件,你应该在你的子类中重写它。 从您提供的代码看来,它应该是 MainWindow。

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 2022-01-22
      • 2017-05-29
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多