Qt – QTest::keyClick() – 是否与绘制同步?

huangapple go评论68阅读模式
英文:

Qt - QTest::keyClick() - is it synchronized to paint?

问题

我有一个GUI,我想要使用QTest进行测试。

我计划创建一个单独的线程,执行QTest::keyClick()测试步骤,但我想要将我的测试步骤与QWidget::repaint()同步,也就是说,我希望每个QTest::keyClick()都发生在前一个QTest::keyClick()引起的重绘之后(如果有的话)。

  1. QTest::keyClick()
  2. 来自步骤1的重绘
  3. QTest::keyClick()
  4. 来自步骤2的重绘

我不希望出现以下情况:

  1. QTest::keyClick()
  2. QTest::keyClick()
  3. 来自步骤1的重绘
  4. 来自步骤2的重绘

问题
如何确保keyClick()不会在上一个keyClick()的绘图事件之前发生?

英文:

I have a GUI which I want to test with QTest.

I'm planning on having a separate thread which executes QTest::keyClick() test steps but I want to synchronize my test steps with QWidget::repaint() i.e. I want each QTest::keyClick() to happen only after the repaint (if any) caused by the repaint from the previous QTest::keyClick()

  1. QTest::keyClick()
  2. Repaint from step 1
  3. QTest::keyClick()
  4. Repaint from step 2

I do not want

  1. QTest::keyClick()
  2. QTest::keyClick()
  3. Repaint from step 1
  4. Repaint from step 2

QUESTION
How to make sure keyClick()s do not occur before paint event from previous keyClick()?

答案1

得分: 0

不需要同步:每次调用 keyClick() 都会导致调用 qApp->processEvents()

static void simulateEvent(QWindow *window, bool press, int code,
                          Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
{
    QEvent::Type type;
    type = press ? QEvent::KeyPress : QEvent::KeyRelease;
    qt_handleKeyEvent(window, type, code, modifier, text, repeat, delay);
    qApp->processEvents();
}

这将在下一个 QTest.keyClick() 之前触发任何绘图事件。

英文:

No need for synchronization: each call to keyClick() results in a call to qApp->processEvents()

static void simulateEvent(QWindow *window, bool press, int code,
                              Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
    {
        QEvent::Type type;
        type = press ? QEvent::KeyPress : QEvent::KeyRelease;
        qt_handleKeyEvent(window, type, code, modifier, text, repeat, delay);
        qApp->processEvents();
    }

which would trigger any paint events before the next QTest.keyClick().

huangapple
  • 本文由 发表于 2023年2月16日 04:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定