使用pyqt执行此操作的正确方法是什么?

时间:2014-08-04 12:58:49

标签: multithreading pyqt qdialog qprogressbar

下面的代码给出了错误信息: " QObject :: startTimer:无法从另一个线程启动计时器。" 我真的没理由。主要是因为我几乎得到了这个线程问题和信号和插槽机制。我如何通过" int(百分比)"变量到对话框或主GUI的对话框,以实时刷新进度条对象?

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import urllib.request

class Main(QWidget):
    def __init__(self, parent = None):
        super(Main, self).__init__()
        self.label = QLabel("TheMainGUI")
        self.pushbutton = QPushButton("Download")

        layout = QHBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.pushbutton)
        self.setLayout(layout)

        self.pushbutton.clicked.connect(self.download)

    def download(self):
        self.filedownloadthread = FileDownloadThread()
        self.filedownloadthread.start()

class Dialog(QDialog):
    def __init__(self, parent = None):
        super(Dialog, self).__init__()
        self.progbar = QProgressBar()
        layout = QVBoxLayout()
        layout.addWidget(self.progbar)
        self.setLayout(layout)

class FileDownloadThread(QThread):
    def __init__(self):
        super(FileDownloadThread, self).__init__()
        self.dialog = Dialog()
        self.dialog.show()

    def run(self):
        url = "http://mysource.net//myfile"
        outputfile = "d://file//path//etc//myfile"

        def reporthook(blocknum, blocksize, totalsize):
            readsofar = blocknum * blocksize
            if totalsize > 0:
                percent = readsofar * 1e2 / totalsize

                self.dialog.progbar.setValue(int(percent))
                s = "\r%5.1f%% %*d / %d" % (
                    percent, len(str(totalsize)), readsofar, totalsize)
                sys.stderr.write(s)
                if readsofar >= totalsize:
                    sys.stderr.write("\n")
            else:
                sys.stderr.write("read %d\n" % (readsofar,))

        proxy = urllib.request.ProxyHandler({'http': "myproxy"})
        opener = urllib.request.build_opener(proxy)
        urllib.request.install_opener(opener)
        urllib.request.urlretrieve(url, outputfile, reporthook)

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

1 个答案:

答案 0 :(得分:1)

使用旧信号和插槽机制可以正常工作。

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import urllib.request

class Main(QWidget):
    def __init__(self, parent = None):
        super(Main, self).__init__()
        self.label = QLabel("TheMainGUI")
        self.pushbutton = QPushButton("Download")

        layout = QHBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.pushbutton)
        self.setLayout(layout)

        self.pushbutton.clicked.connect(self.download)

    def download(self):
        self.dialog = Dialog()
        self.dialog.show()

class Dialog(QDialog):
    def __init__(self, parent = None):
        super(Dialog, self).__init__()
        self.progbar = QProgressBar()
        layout = QVBoxLayout()
        layout.addWidget(self.progbar)
        self.setLayout(layout)

        self.filedownloadthread = FileDownloadThread()
        self.connect(self.filedownloadthread, SIGNAL('signal'), self.update)
        self.filedownloadthread.start()

    def update(self, percent):
        self.progbar.setValue(percent)

class FileDownloadThread(QThread):
    def __init__(self):
        super(FileDownloadThread, self).__init__()

    def run(self):
        url = "http://mysource.net//myfile"
        outputfile = "d://file//path//etc//myfile"

        def reporthook(blocknum, blocksize, totalsize):
            readsofar = blocknum * blocksize
            if totalsize > 0:
                percent = readsofar * 1e2 / totalsize

                self.dialog.progbar.setValue(int(percent))
                s = "\r%5.1f%% %*d / %d" % (
                    percent, len(str(totalsize)), readsofar, totalsize)
                sys.stderr.write(s)
                if readsofar >= totalsize:
                    sys.stderr.write("\n")
            else:
                sys.stderr.write("read %d\n" % (readsofar,))
            self.emit(SIGNAL('signal'), int(percent))

        proxy = urllib.request.ProxyHandler({'http': "myproxy"})
        opener = urllib.request.build_opener(proxy)
        urllib.request.install_opener(opener)
        urllib.request.urlretrieve(url, outputfile, reporthook)

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