我尝试使用moveToThread()创建QThread,但是不起作用。 如果我使用扩展QThread的Class,它可以工作。 为什么我不能使用moveToThread运行QThread?
我找到了this example,但看不出有什么区别。 还是环境问题?
env:python3.6.6 32bits,赢得10
TestThread.py
from PySide2 import QtCore
# try:
# from PySide2.QtCore import QString
# except:
# QString = str
import sys,time
class TestThread(QtCore.QObject):
def __init__(self, parent=None):
super(TestThread, self).__init__(parent)
@QtCore.Slot()
def longRun(self):
while True:
print('abcxxx')
time.sleep(3)
# class TestThread(QtCore.QThread):
# def __init__(self, parent=None):
# QtCore.QThread.__init__(self, parent)
# def __del__(self):
# self.quit()
# self.wait()
# def run(self):
# while True:
# print('abc')
# time.sleep(3)
TestMain.py
from PySide2 import QtCore, QtGui, QtWidgets
# try:
# from PySide2.QtCore import QString
# except:
# QString = str
from TestUI import TestUI
import sys
from TestThread import TestThread
class TestMain(QtWidgets.QMainWindow, TestUI):
def __init__(self, parent=None):
super(__class__, self).__init__(parent)
self.setupUi(self)
self.testBtn.clicked.connect(self.testclick)
def testclick(self):
print(123)
t = TestThread()
thr = QtCore.QThread(self)
t.moveToThread(thr)
thr.started.connect(t.longRun)
thr.start()
# t = TestThread(self)
# t.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = TestMain()
ui.show()
sys.exit(app.exec_())
TestUI.py
from PySide2 import QtCore, QtGui, QtWidgets
class TestUI(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(100, 100)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.testBtn = QtWidgets.QPushButton(self.centralWidget)
self.testBtn.setObjectName("testBtb")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
def retranslateUi(self, MainWindow):
self.testBtn.setText(QtWidgets.QApplication.translate("MainWindow", "Test", None, -1))