QTimer到timeoutmethod的连接在TestCases(Python)中不起作用

2024-09-28 17:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚完成了一个软件,现在想借助python的TestCase对其进行测试。方法test_show_video应该启动一个QTimer。它连接到test_run_iteration,后者每0,1秒将一个索引相加。在调用assert之前,该方法有3秒钟的时间来汇总索引,因此它应该大于零。但事实并非如此。有人有线索吗?实际上,到计时器的超时连接似乎是错误的。在

try:
    app = QtGui.QApplication(sys.argv)
except RuntimeError:
    app = QtCore.QCoreApplication.instance()

class TestProgressPresenter(TestCase):

    def test_show_video(self):

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.test_run_iteration)
        self.timer.start(100)
        self.index = 0

        millis = start_time = int(round(time.time() * 1000))

        while start_time + 3000 > millis:
            millis = int(round(time.time() * 1000))

        assert self.index > 0

    def test_run_iteration(self):
        self.index += 1

Tags: 方法runtestselfindextimevideoshow
1条回答
网友
1楼 · 发布于 2024-09-28 17:19:30

在这里,您将在while循环中阻塞Qt事件循环3秒钟。实际上,计时器的timeout信号在控件返回到事件循环之前是不会被调用的,这是while完成和test_show_video完成的时候。在

如果希望触发计时器,则应确保事件在while循环检查期间得到处理。为此,你可以有一些东西,比如:

while start_time + 3000 > millis:
    millis = int(round(time.time() * 1000))
    QApplication.processEvents(QEventLoop.AllEvents)

使用本地事件循环可以更轻松地等待几秒钟:

^{pr2}$

相关问题 更多 >