Qt,在QkeyPressedEvent

函数是: 当我按下一次键时,调用keyPress,调用QTimer上链接的插槽调用一次,调用KeyReleased,QTimer停止。 当我持续按下键时,按键已按下,QTimer上的插槽每隔2s调用一次, 当我释放键时,KeyReleased调用了,QTimer停止了。 没有QMessageBox,一切都会顺利进行。 问题是: 当我在插槽中添加QMessageBox时,将永远不会调用KeyReleased并且QTimer也将不会停止。

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    connect(&m_keyUpTimer,&QTimer::timeout,this,&MainWindow::slotKeyUpTimerTimeOut);
}

MainWindow::~MainWindow()
{
}

void MainWindow::keyPressEvent( QKeyEvent * e)
{
    if (e->isautorepeat()) {
        return;
    }
    switch (e->key()) {
    case Qt::Key_Up:
    {
        QMessageBox errorMsg;
        errorMsg.setWindowTitle("Press");
        errorMsg.show();


        if (!m_keyUpTimer.isactive() ) {
            m_keyUpTimer.start(2000);
        }
        break;
    }
    default: break;
    }
}

void MainWindow::keyReleaseEvent(QKeyEvent * e)
{
    if (e->isautorepeat()) {
        return;
    }
    switch (e->key()) {
    case Qt::Key_Up:
    {
        m_keyUpTimer.stop();
        QMessageBox errorMsg;
        errorMsg.setWindowTitle("Release");
        errorMsg.show();
        break;
    }

    default: break;
    }
}

void MainWindow::slotKeyUpTimerTimeOut() const
{
    QMessageBox errorMsg;
    errorMsg.setWindowTitle("Slot");
    errorMsg.show();
}

对不起,我的英语不好。

leehomspeed 回答:Qt,在QkeyPressedEvent

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2585261.html

大家都在问