繁体   English   中英

PyQt Qthread自动重启

[英]PyQt Qthread automatic restart

我试图了解线程是如何工作的,而我一直陷于这个问题。 那是我的程序所解释的:我在pyqt中制作了一个简单的GUI,使用QObject作为辅助类。 当我按下botton时,gui从列表中读取一个随机值并将其传递给线程,该线程将打印下一个五个数字。 当线程完成工作时,它将数据传递给gui。 现在,我希望GUI自动重启具有新起始值的新线程。 我可以通过再次按启动键来重新启动线程,但是我需要在没有人工干预的情况下启动它。 有什么办法吗?

提前致谢

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
import sys
import numpy as np



class SomeObject(QObject):

    finished = pyqtSignal(object)
    valore = pyqtSignal(object)
    vector = pyqtSignal(object)

    def __init():
        super(SomeObject, self).__init__()

    def longRunning(self):
        vec = []
        end = self.count + 5 
        while self.count < end:
            time.sleep(1)
            vec.append(self.count)
            self.valore.emit(self.count)
            self.count += 1
        self.finished.emit(vec)
        #self.vector.emit()

    def setCount(self, num):
        self.count = num

class GUI(QDialog):

    def __init__(self, parent = None):
        super(GUI, self).__init__(parent)
        #declare QThread object
        self.objThread = QThread()
        #declare SomeObject type, and move it to thread
        self.obj = SomeObject() 
        self.obj.moveToThread(self.objThread) 
        #connect finished signal to nextVector method
        self.obj.finished.connect(self.nextVector)
        #connect valore to self.prova method
        self.obj.valore.connect(self.prova)
        #self.obj.vector.connect(self.nextVector)
        #Connect thread.start to the method long running
        self.objThread.started.connect(self.obj.longRunning)

        botton = QPushButton("start")
        self.connect(botton, SIGNAL("clicked()"), self.showcount)
        box = QHBoxLayout()
        box.addWidget(botton)        
        self.setLayout(box)

        #a list of random number
        a = np.random.randint(10, size = 5)
        self.iter = iter(a)

    def showcount(self):
        """
        When botton clicked, read the next value from iter, pass it to
        setCount and when start the thread
        """        
        try:
            a = self.iter.next()
            print a
            self.obj.setCount(a)        
            self.objThread.start()
        except StopIteration:
            print "finito"
        #self.obj.setCount(a)        
        #self.objThread.start()
        #print self.objThread.currentThreadId()

    def prova(self, value):
        """
        Connected to signal valore, print the value
        """
        print value

    def nextVector(self, vec):
        """
        Print the whole vector
        """
        print vec
        self.objThread.quit()
        try:
            a = self.iter.next()
            print a
            self.obj.setCount(a)        
            self.objThread.start()
        except StopIteration:
            print "finito"

app = QApplication(sys.argv)
form = GUI()
form.show()
app.exec_()    

您已经设置好了。 线程完成后,它会发出完成信号,该信号将调用nextVector方法,因此只需在nextVector的末尾调用start方法。

def nextVector(self, vec):
    ...
    self.showcount()
# end nextVector

您可能还想更改QPushButton的新信号连接

button.clicked.connect(self.showcount)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM