如何顺利显示 QProgressBar? [英] How to show QProgressBar smoothly?

查看:51
本文介绍了如何顺利显示 QProgressBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MacOSX 上学习 Pyside QProgressBar.当我像下面这样使用 QProgressBar 时,它只指示 0% 或 100%.如何顺利制作 QProgressBar?有没有办法做到这一点?

I'm learning Pyside QProgressBar on MacOSX. When I use QProgressBar like following, it only indicate 0% or 100%. How to make a QProgressBar smoothly? Is there any way to do this?

from PySide.QtGui import QApplication, QProgressBar, QWidget
from PySide.QtCore import QTimer
import time

app = QApplication([])
pbar = QProgressBar()
pbar.setMinimum(0)
pbar.setMaximum(100)

pbar.show()

def drawBar():
    global pbar
    pbar.update()

t = QTimer()
t.timeout.connect(drawBar)
t.start(100)

for i in range(1,101):
    time.sleep(0.1)
    pbar.setValue(i)

app.exec_()

推荐答案

去掉这段代码:

for i in range(1,101):   # this won't work, because
    time.sleep(0.1)      # Qt's event loop can't run while
    pbar.setValue(i)     # you are forcing the thread to sleep

相反,添加一个全局变量 p:

and instead, add a global variable p:

p = 0

并在 drawBar() 函数中增加它:

and increment it in your drawBar() function:

def drawBar():
    global pbar
    global p
    p = p + 1
    pbar.setValue(p)
    pbar.update()

这篇关于如何顺利显示 QProgressBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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