这是我的代码。我试图显示一个进度条,显示加载状态,直到所有小部件都正确加载但进度条不能正确更新。它只在过程结束时更新并显示100%
from PySide.QtGui import *
from PySide.QtCore import *
import time
import sys
class Mainwindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.progress = QProgressBar()
self.progress.setRange(0, 100)
self.worker = Worker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.worker.relay.connect(self.update_progress)
self.thread.start()
self.progress.show()
self.add_tabs()
def add_tabs(self):
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
for i in xrange(10000):
i*i
time.sleep(1)
self.worker.process()
@Slot(int)
def update_progress(self, x):
self.progress.setValue(x * 10)
class Worker(QObject):
relay = Signal(int)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.x = 0
def process(self):
while self.x < 10:
self.x = self.x+1
self.relay.emit(self.x)
print self.x
return True
if __name__=='__main__':
app=QApplication(sys.argv)
win=Mainwindow()
win.show()
app.exec_()
sys.exit(0)
修改1 声称我的问题是how to make QtextEdit can update data realtime and no stuck UI的副本,但我已尝试过程事件,过程事件是循环或我们在前端进行的过程。在我的例子中,你用xrange看到的循环是我真实程序中的后端进程。在这里使用xrange循环有一个理解,我有真正的后端任务运行,我不能使用过程事件。对不起,首先不要说清楚。我需要一个基于Qthread的解决方案。
答案 0 :(得分:2)
如果我理解正确,您希望在单独的线程上执行一些繁重的处理并更新主GUI上的进度。因此,MainWindow.add_tabs
中完成的所有处理实际上都应该在worker中完成。
此外,您似乎想要使用QProgressDialog
(弹出窗口)而不是QProgressBar
。
这是一个解决方案,具有响应式GUI:
from PySide.QtGui import *
from PySide.QtCore import *
import time
import sys
class Mainwindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.worker = Worker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.worker.workRequested.connect(self.thread.start)
self.thread.started.connect(self.worker.add_tabs)
self.worker.finished.connect(self.on_finish)
self.button=QPushButton("Add tabs")
self.button.clicked.connect(self.on_button_click)
self.button_responsive=QPushButton("Still responsive?")
self.button_responsive.clicked.connect(self.responsive)
self.progress = QProgressDialog("Progress","cancel",0,10)
self.progress.setCancelButton(None)
self.worker.relay.connect(self.update_progress)
vbox=QVBoxLayout()
vbox.addWidget(self.button)
vbox.addWidget(self.button_responsive)
self.setLayout(vbox)
def on_button_click(self):
self.button.setEnabled(False)
self.worker.request_work()
self.progress.setValue(0)
def on_finish(self):
self.button.setEnabled(True)
self.thread.quit()
def update_progress(self,value):
self.progress.setValue(value)
def responsive(self):
print("Yes!")
class Worker(QObject):
workRequested=Signal()
finished=Signal()
relay=Signal(int)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.x = 0
def request_work(self):
self.workRequested.emit()
def add_tabs(self):
self.x=0
for i in range(10):
#Do any processing
for i in xrange(10000):
i*i
time.sleep(1)
self.x+=1
self.relay.emit(self.x)
self.finished.emit()
if __name__=='__main__':
app=QApplication(sys.argv)
win=Mainwindow()
win.show()
sys.exit(app.exec_())