qt 我如何检查 2 keypressevent 之间时间戳的差异

qt how i can check difference of timestamp between 2 keypressevent

当 2 个键之间的时间差大于 100 毫秒时,我试图在两次编辑之间改变焦点。如果keyPressed(..)函数是每次输入一个键,我如何记住最后一次输入的键?

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

示例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"