threading une classe avec des arguments pyqt5 [duplicate]

Nov 13 2020

Dans le code suivant, j'ai essayé de threader la classe de calcul qui a besoin de x comme argument, le thread fonctionne parfaitement quand il n'y a pas de x. mais une fois que j'ai mis un argument, les choses ont manqué, puis-je savoir pourquoi?

from PyQt5 import QtCore, QtGui, QtWidgets
from mainwindow2 import Ui_Form
import time

class Calculation(QtCore.QThread):
    def __init__(self,x):
        super().__init__()
        self.x = x
    def run(self):
        for i in range(10):
            if i!=5:
                time.sleep(1)
                print(i+self.x)
        
class MainWindow(QtWidgets.QMainWindow, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.pressed.connect(self.threadingc)

    def threadingc(self):
        # create the thread with the main window as a parent, this is possible 
        # since QMainWindow also inherits from QObject, and this also ensures
        # that python will not delete it if you want to start another thread
        self.pushButton.setEnabled(False)
        thread = Calculation(5)
        thread.start()
        

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

mainwindow2

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(150, 110, 93, 28))
        self.pushButton.setObjectName("pushButton")

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

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

Réponses

1 maxwiklund Nov 13 2020 at 01:07

Vous devez fournir la variable parent au thread.

from PyQt5 import QtCore, QtGui, QtWidgets

import time


class Calculation(QtCore.QThread):
    
    def __init__(self, parent, x):
        # I have added the parent variable.
        super().__init__(parent)
        self.x = x
    
    def run(self):
        for i in range(10):
            if i != 5:
                time.sleep(1)
                print(i + self.x)

class MainWindow(QtWidgets.QMainWindow, Ui_Form):
    ...
    
    def threadingc(self):
        # You need to provide a reference to the gui or make the thread a class variable.
        thread = Calculation(self, 5)
        thread.start()
        # If you don't provide a parent or make the thread a class variable the function scope will
        # end here and the thread variable will be terminated.


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())