RT,我使用重写QThread的run方法,创建子线程。打印主、子线程号
import sys
from PySide6.QtCore import QObject, QThread, Signal, Slot
from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
# #################################################################################################
# @copyright 2003-2022
# @file thread_signals.py
# @date 2022/1/15 22:28
# @author qiao wei
# @version 1.0
# @brief Create a basic window with a layout and a button
# @history
# #################################################################################################
class MainForm(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Form")
self.layout = QVBoxLayout()
self.button = QPushButton("Click me!")
self.button.clicked.connect(self.start_thread)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
def start_thread(self):
"""
Instantiate and start a new thread
Returns:
"""
# Print thread id
print(QThread.currentThread())
instanced_thread: QThread = WorkerThread(self)
instanced_thread.start()
# Create the Slots that will receive signals
@Slot(str)
def update_str_field(self, message):
print(message)
@Slot(int)
def update_int_field(self, value):
print(value)
# #################################################################################################
# @copyright 2003-2022
# @file thread_signals.py
# @date 2022/1/15 22:34
# @author qiao wei
# @version 1.0
# @brief Signals must inherit QObject
# @history
# #################################################################################################
class MySignals(QObject):
signal_str = Signal(str)
signal_int = Signal(int)
# #################################################################################################
# @copyright 2003-2022
# @file thread_signals.py
# @date 2022/1/15 22:35
# @author qiao wei
# @version 1.0
# @brief Create the Worker Thread. Override run method
# @history
# #################################################################################################
class WorkerThread(QThread):
def __init__(self, parent=None):
QThread.__init__(self, parent)
# Instantiate signals and connect signals to the slots
self.signals = MySignals()
self.signals.signal_str.connect(parent.update_str_field)
self.signals.signal_int.connect(parent.update_int_field)
def run(self):
# Print thread id
print(QThread.currentThread())
# Do something on the worker thread
a: int = 1 + 1
# Emit signals whenever you want
self.signals.signal_int.emit(a)
self.signals.signal_str.emit("This text comes to Main thread from our Worker thread.")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainForm()
window.show()
sys.exit(app.exec())
打印的结果
<PySide6.QtCore.QThread(0x1bda79ee6c0) at 0x000001BDA828DF40>
<__main__.WorkerThread(0x1bda5ba7ad0) at 0x000001BDA828DF40>
使用Qt推荐的moveToThread方法将对象移动到子线程中然后打印
代码:
import sys
from PySide6.QtCore import QObject, QThread, Signal, Slot
from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
# #################################################################################################
# @copyright 2003-2022
# @file thread_signals.py
# @date 2022/1/15 22:28
# @author qiao wei
# @version 1.0
# @brief Create a basic window with a layout and a button
# @history
# #################################################################################################
class MainForm(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Form")
self.layout = QVBoxLayout()
self.button = QPushButton("Click me!")
self.button.clicked.connect(self.start_sub_thread)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
def start_sub_thread(self):
print(QThread.currentThread())
self.sig = MySignals()
self.sl = WorkThread01()
self.sig.signal_int.connect(self.sl.run_in_sub_thread)
# Move to sub thread
self.thread = QThread()
self.sl.moveToThread(self.thread)
self.thread.start()
self.sig.signal_int.emit(2)
# #################################################################################################
# @copyright 2003-2022
# @file thread_signals.py
# @date 2022/1/15 22:34
# @author qiao wei
# @version 1.0
# @brief Signals must inherit QObject
# @history
# #################################################################################################
class MySignals(QObject):
signal_str = Signal(str)
signal_int = Signal(int)
class WorkThread01(QObject):
def __init__(self):
super().__init__()
@Slot(int)
def run_in_sub_thread(self, value):
print(value)
print(QThread.currentThread())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainForm()
window.show()
sys.exit(app.exec())
两者结果
<PySide6.QtCore.QThread(0x2507d855950) at 0x000002507D7EDD00>
2
<PySide6.QtCore.QThread(0x2507d8e4190) at 0x000002507D7EE280>
而且退出时
Process finished with exit code -1073740791 (0xC0000409)
请问两个方法,我都成功启动子线程了么?第二种方法为什么返回会有 -1073740791 情况。该如何解决?