【问题标题】:Qt - Implementing QKeyEventQt - 实现 QKeyEvent
【发布时间】:2016-03-28 16:11:13
【问题描述】:

如何在 Qt 中实现键盘监听?我的以下设置不起作用。我有两个类,gameLogic 和 gameView。 gameView 有一个 gameLogic 实例:

gameView::gameView(QWidget *parent)
    : QWidget(parent)
{
    logic = new gameLogic(6);
    logic->setFocusPolicy(Qt::TabFocus); //in one of the articles I read, this was supposed to fix the issue. It doesn't for me.
    this->resize(1200, 700);
    this->setStyleSheet("background-color: white");
    QString str;
    str.setNum(logic->n);
    connect(logic, SIGNAL(playerStepped(int, int)), this, SLOT(movePlayer(int, int)));    
}

在 gameLogic 中,我按如下方式处理击键:

void gameLogic::keybrdStep( QKeyEvent * keypressed )
{
    if (keypressed->key() == Qt::Key_Q) {
        _message = new QMessageBox;
        _message->setText("Q");
        _message->exec();
    }
}

无论我按 Q 多少次,都没有任何反应。我究竟做错了什么?我错过了哪一部分?我正在使用最新版本的 Qt 使用 Linux Mint。

【问题讨论】:

    标签: c++ qt keyboard-events


    【解决方案1】:
    void gameLogic::keybrdStep( QKeyEvent * keypressed )
    

    您从哪里获得方法名称“keybrdStep”?你觉得应该叫谁?

    获取按键需要重写的方法的名称是QWidget::keyPressEvent()

    【讨论】:

    • 哦!!!我不认为我正在覆盖一个函数,我认为任何带有 QKeyEvent 的函数都可以!愚蠢的我!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2012-10-06
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多