<p>我想你可能弄错了。你需要在一个单独的线程中完成工作,这样它就不会冻结应用程序。但您也希望能够更新进度条。您可以通过使用<code>QThread</code>创建工作类来实现这一点。qthread能够发出信号,用户界面可以监听这些信号并采取适当的操作。</p>
<p>首先,让我们创建你的工人阶级。</p>
<pre><code>#Inherit from QThread
class Worker(QtCore.QThread):
#This is the signal that will be emitted during the processing.
#By including int as an argument, it lets the signal know to expect
#an integer argument when emitting.
updateProgress = QtCore.Signal(int)
#You can do any extra things in this init you need, but for this example
#nothing else needs to be done expect call the super's init
def __init__(self):
QtCore.QThread.__init__(self)
#A QThread is run by calling it's start() function, which calls this run()
#function in it's own "thread".
def run(self):
#Notice this is the same thing you were doing in your progress() function
for i in range(1, 101):
#Emit the signal so it can be received on the UI side.
self.updateProgress.emit(i)
time.sleep(0.1)
</code></pre>
<p>现在你有了工人阶级,是时候利用它了。您需要在<code>Ui_Dialog</code>类中创建一个新函数来处理发出的信号。</p>
<pre><code>def setProgress(self, progress):
self.progressBar.setValue(progress)
</code></pre>
<p>在那里时,可以删除<code>progress()</code>函数。</p>
<p>在<code>retranslateUi()</code>中,您将希望从</p>
<pre><code>self.pushButton.clicked.connect(self.progress)
</code></pre>
<p>到</p>
<pre><code>self.pushButton.clicked.connect(self.worker.start)
</code></pre>
<p>最后,在<code>setupUI()</code>函数中,需要创建一个worker类的实例,并将其信号连接到<code>setProgress()</code>函数。</p>
<p>在此之前:</p>
<pre><code>self.retranslateUi(Dialog)
</code></pre>
<p>添加以下内容:</p>
<pre><code>self.worker = Worker()
self.worker.updateProgress.connect(self.setProgress)
</code></pre>
<hr/>
<p>以下是最终代码:</p>
<pre><code>from PySide import QtCore, QtGui
import time
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 133)
self.progressBar = QtGui.QProgressBar(Dialog)
self.progressBar.setGeometry(QtCore.QRect(20, 10, 361, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(20, 40, 361, 61))
self.pushButton.setObjectName("pushButton")
self.worker = Worker()
self.worker.updateProgress.connect(self.setProgress)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.progressBar.minimum = 1
self.progressBar.maximum = 100
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Dialog", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
self.progressBar.setValue(0)
self.pushButton.clicked.connect(self.worker.start)
def setProgress(self, progress):
self.progressBar.setValue(progress)
#Inherit from QThread
class Worker(QtCore.QThread):
#This is the signal that will be emitted during the processing.
#By including int as an argument, it lets the signal know to expect
#an integer argument when emitting.
updateProgress = QtCore.Signal(int)
#You can do any extra things in this init you need, but for this example
#nothing else needs to be done expect call the super's init
def __init__(self):
QtCore.QThread.__init__(self)
#A QThread is run by calling it's start() function, which calls this run()
#function in it's own "thread".
def run(self):
#Notice this is the same thing you were doing in your progress() function
for i in range(1, 101):
#Emit the signal so it can be received on the UI side.
self.updateProgress.emit(i)
time.sleep(0.1)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
</code></pre>
<hr/>
<p>QThreads有一些自动发出的内置信号。您可以看到它们,以及有关QThreads<a href="http://srinikom.github.io/pyside-docs/PySide/QtCore/QThread.html">in the documentation</a>的更多信息</p>