簡體   English   中英

PyQt更新gui

[英]PyQt update gui

我正在嘗試通過PyQt中的QThread更新Qt GUI對象中的文本,但我只是得到錯誤QPixmap: It is not safe to use pixmaps outside the GUI thread ,然后它崩潰了。 我真的很感激任何幫助,謝謝。

class MainWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent = None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.output = Output()

    def __del__ (self):
        self.ui = None

    @pyqtSignature("")
    def on_goBtn_released(self):
        threadnum = 1
        #start threads
        for x in xrange(threadnum):
            thread = TheThread() 
            thread.start()


class Output(QWidget, Ui_Output):

    def __init__(self, parent = None):
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.ui = Ui_Output
        self.show()

    def main(self):
        self.textBrowser.append("sdgsdgsgsg dsgdsg dsgds gsdf")



class TheThread(QtCore.QThread):

    trigger = pyqtSignal()

    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        self.trigger.connect(Output().main())
        self.trigger.emit()
self.trigger.connect(Output().main())

這條線存在問題。 您正在線程中實例化一個類似於小部件的類。 這是錯的。 您不應該在不同的線程中使用GUI元素。 所有與GUI相關的代碼都應該與事件循環在同一個線程中運行。

以上這條線在設計上也是錯誤的。 您從線程發出自定義信號,這是一個好方法。 但處理此信號的對象應該是擁有/創建線程的對象,即您的MainWindow

您也不會保留對線程實例的引用。 您可以在方法中創建它,但它是本地的。 所以它將被垃圾收集,你可能會看到它在完成之前被刪除的警告。

這是一個最小的工作示例:

import sys
from PyQt4 import QtGui, QtCore
import time
import random


class MyThread(QtCore.QThread):
    trigger = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)

    def setup(self, thread_no):
        self.thread_no = thread_no

    def run(self):
        time.sleep(random.random()*5)  # random sleep to imitate working
        self.trigger.emit(self.thread_no)


class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.text_area = QtGui.QTextBrowser()
        self.thread_button = QtGui.QPushButton('Start threads')
        self.thread_button.clicked.connect(self.start_threads)

        central_widget = QtGui.QWidget()
        central_layout = QtGui.QHBoxLayout()
        central_layout.addWidget(self.text_area)
        central_layout.addWidget(self.thread_button)
        central_widget.setLayout(central_layout)
        self.setCentralWidget(central_widget)

    def start_threads(self):
        self.threads = []              # this will keep a reference to threads
        for i in range(10):
            thread = MyThread(self)    # create a thread
            thread.trigger.connect(self.update_text)  # connect to it's signal
            thread.setup(i)            # just setting up a parameter
            thread.start()             # start the thread
            self.threads.append(thread) # keep a reference

    def update_text(self, thread_no):
        self.text_area.append('thread # %d finished' % thread_no)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    mainwindow = Main()
    mainwindow.show()

    sys.exit(app.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM