Qt我如何检查两个按键事件之间的时间戳差异 [英] qt how i can check difference of timestamp between 2 keypressevent

查看:143
本文介绍了Qt我如何检查两个按键事件之间的时间戳差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当两个键之间的时间差大于100毫秒时,我正在尝试在两个编辑之间更改焦点.如果每次输入键时都 keyPressed(..)函数,该如何记忆最后一次输入的键?

I'm trying to change focus between two edits when the time difference between 2 keys is greater than 100ms. How can I memorize the last key input if the keyPressed(..) function is every time a key is inputted ?

推荐答案

我不会为此使用 QTime ,因为它取决于系统时钟.我会使用 QElapsedTimer QTimer .

I wouldn't use QTime for this, as it depends on the system clock. I would use QElapsedTimer or QTimer.

带有 QTimer 的示例:

#include <QtWidgets>

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr) : QWidget(parent)
    {
        setLayout(new QHBoxLayout);
        layout()->addWidget(&line_edit1);
        layout()->addWidget(&line_edit2);
        focus_timer.setInterval(100);
        focus_timer.setSingleShot(true);
        connect(&line_edit1, &QLineEdit::textEdited, &focus_timer, QOverload<>::of(&QTimer::start));
        connect(&line_edit2, &QLineEdit::textEdited, &focus_timer, QOverload<>::of(&QTimer::start));
        connect(&focus_timer, &QTimer::timeout, this, [&]
        {
            line_edit1.hasFocus() ? line_edit2.setFocus() : line_edit1.setFocus();
        });
    }
private:
    QLineEdit line_edit1;
    QLineEdit line_edit2;
    QTimer focus_timer;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

#include "main.moc"

这篇关于Qt我如何检查两个按键事件之间的时间戳差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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