Pyside setText() 不更新 QLabel [英] Pyside setText() not updating QLabel

查看:71
本文介绍了Pyside setText() 不更新 QLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

 self.testTxt = QtGui.QLabel("0")
 .
 .
 for i in range(10):
        sleep(1)
        self.testTxt.setText(unicode(i))

但 QLabel 直到循环结束才会更新

but the QLabel doesn't get updated till the end of the loop

我试过了:

self.processEvents()
self.testTxt.update()

但无济于事.

有人碰巧知道为什么吗?

Does anyone happen to know why?

提前致谢

推荐答案

文本框不会更新,因为程序被束缚在 for 循环中,因此永远不会回到 Qt 的事件循环来重绘小部件.调用 testTxt.update() 没有帮助,因为 QWidget 文档 指出,update() 只是 安排 下次运行事件循环时的重绘(无论如何,当您调用 setText 时都会处理此问题).

The text box does not update because the program is tied up inside the for loop and thus never gets back to Qt's event loop to redraw the widget. Calling testTxt.update() does not help because, as the QWidget documentation states, update() simply schedules a repaint for the next time the event loop is run (and this is taken care of when you call setText anyway).

可能的解决方案:

  • 使用 QTimer 重复调用更新文本的函数(这是首选解决方案)
  • 在每次调用 setText 后调用 testText.repaint()
  • 在每次调用 setText 后调用 QApplication.processEvents()

这篇关于Pyside setText() 不更新 QLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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