其他分享
首页 > 其他分享> > 关于QThread使用锁死的探索

关于QThread使用锁死的探索

作者:互联网

在学习使用QT5的时候,发现要使用多线程处理多任务,按照https://www.cnblogs.com/liming19680104/p/10397052.html等很多网上的方法,测试一下,发现我写的程序运行就崩溃。

不断测试,发现一个现象:

子线程的初始化要在窗口初始的同时进行

程序代码如下:Ui_MainWindow 窗口是qt desinger生成的

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'D:\pywork\qtpy\threadtest\main.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore,  QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(465, 262)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
        self.textEdit.setGeometry(QtCore.QRect(40, 20, 281, 171))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(360, 30, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "start"))

QThread实现类:

# -*- coding: utf-8 -*-
from PyQt5.QtCore import *

class MyQThread(QThread):
    sentText = pyqtSignal(str)
    
    def __init__(self,text):    #text是传递参数
        super(MyQThread, self).__init__()
        self.text = text + "in child thread"
        
    def run(self):
        for i in range(2000000000):
            pass
        # 循环完毕后发出信号
        self.sentText.emit(self.text)

调用的主类:

# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication
from myQThread import MyQThread
from Ui_main import Ui_MainWindow


class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
      #在这里初始化没有问题 self.myqthread = MyQThread("hello") self.myqthread.sentText.connect(self.handleMessage) @pyqtSlot() def on_pushButton_clicked(self): """
      最开始是在这里初始化的
        myqthread = MyQThread("hello")
        myqthread.sentText.connect(self.handleMessage)
        myqthread.start()     
     """
        self.myqthread.start()
    def handleMessage(self, text):
        self.textEdit.setPlainText(self.textEdit.toPlainText()+"\n" + text)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

 

提醒自已注意

 

标签:__,探索,text,self,def,import,锁死,MainWindow,QThread
来源: https://www.cnblogs.com/aocshallo1/p/11066530.html