在阅读了有关应如何使用QThread的扩展讨论并通过子类覆盖它的run方法之后,我使用了一个Worker对象来使用QThread,这是不合适的方法.但是,在我打算使用的方法中,我需要传递一个附加的函数参数,该参数在线程启动以及使用moveToThread将工作程序推入线程时不可用.该信息(参数)在按下按钮时可用,并传达有关要移动的对象的信息.
在我的代码的完整版中,有3个单独的控制器,用于3个单独的对象,您可以在下面找到一个最小的工作示例,以说明我尝试传递参数的内容.该代码也可以在pastebin上使用,并且感兴趣的行号是10-28、46-50和133-135.
到目前为止,我已经尝试在连接到worker中实际函数的行中使用lambda构造函数.那行是self.thread.started.connect(self.obj.moveLeftIncrement)然后尝试使用插槽,但是我不太了解它们.此外,尽管使用了QThread,但有时GUI仍挂起,并且在程序退出后仍会出现错误,其中之一如下:
Process finished with exit code -1073740791 (0xC0000409)
我的问题如下:
>如何在运行时传递参数和/或使用插槽?
>如何防止退出时出现程序错误?
>为什么不推荐使用子类化QThread在这种情况下直接起作用?
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
import sys
import time
class Worker(QObject):
finished = Signal(int)
@Slot(str)
def moveLeftIncrement(self,controller_name):
# controller_name = "Controller 1"
print("Controller name is ",controller_name)
if controller_name == "Controller 1":
print("Starting controller 1")
time.sleep(2)
print("Finishing sleep")
elif controller_name == "Controller 2":
print("Starting controller 2")
time.sleep(2)
print("Finishing sleep")
elif controller_name == "Controller 3":
print("Starting controller 3")
time.sleep(2)
print("Finishing sleep")
else:
raise Exception("No such controller found!")
self.finished.emit(0)
class Window(QWidget):
""" Inherits from QWidget """
def closeEvent(self,*args,**kwargs):
print("\nClosing")
def __init__(self):
super().__init__()
self.CONTINUOUS_MOVE_SWITCH = False
self.title = 'Control Controllers'
self.left = 10
self.top = 10
self.width = 320
self.height = 100
self.AxesMapping = [0,1,2,3]
self.initUI()
self.thread = QThread()
self.obj = Worker()
self.obj.moveToThread(self.thread)
self.thread.started.connect(self.obj.moveLeftIncrement)
self.obj.finished.connect(self.thread.quit)
def initUI(self):
""" Initializes the GUI either using the grid layout or the absolute position layout"""
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
Comp1 = self.createGridLayout("Controller 2")
windowLayout = QGridLayout()
windowLayout.addWidget(Comp1,0)
self.setLayout(windowLayout)
self.show()
def createGridLayout(self,controller):
"""Creates a grid layout for the buttons"""
box_size = QSize(640,440)
HGroupBox = QGroupBox(controller)
layout = QGridLayout()
layout.addWidget(self.createButton("left",controller),1)
layout.addWidget(self.createButton("right",3)
layout.addWidget(self.createButton("forward",2)
layout.addWidget(self.createButton("backward",3,2)
HGroupBox.setLayout(layout)
HGroupBox.setFixedSize(box_size)
return HGroupBox
def createButton(self,name,controller):
"""Creates a button with the specified size"""
button_size = QSize(100,40)
icon_size = 40
button = QPushButton()
button.Name = name
button.Controller = controller
button.Moving = 0
button.clicked.connect(lambda: self.buttonPresssed(button))
button.setFixedSize(button_size)
return button
def moveLeftIncrement(self,controller,button):
if controller == "Controller 1":
time.sleep(2)
elif controller == "Controller 2":
time.sleep(2)
elif controller == "Controller 3":
time.sleep(2)
else:
raise Exception("No such controller found!")
def moveRightIncrement(self,button):
if controller == "Controller 1":
time.sleep(2)
elif controller == "Controller 2":
time.sleep(2)
elif controller == "Controller 3":
time.sleep(2)
else:
raise Exception("No such controller found!")
def moveForwardIncrement(self,button):
if controller == "Controller 1":
time.sleep(2)
elif controller == "Controller 2":
time.sleep(2)
elif controller == "Controller 3":
time.sleep(2)
else:
raise Exception("No such controller found!")
def moveBackwardIncrement(self,button):
if controller == "Controller 1":
time.sleep(2)
elif controller == "Controller 2":
time.sleep(2)
elif controller == "Controller 3":
time.sleep(2)
else:
raise Exception("No such controller found!")
def buttonPresssed(self,button):
name = button.Name
if hasattr(button,'Controller'):
controller = button.Controller
print("The controller selected is",controller)
if name == 'left':
self.thread.start()
elif name == 'right':
print("Moved controller right for a single step")
self.moveRightIncrement(controller,button)
elif name == 'forward':
self.moveForwardIncrement(controller,button)
print("Moved controller forward for a single step")
elif name == 'backward':
self.moveBackwardIncrement(controller,button)
print("Moved controller backward for a single step")
elif name == "up":
print("Moving controller up for a single step")
self.moveUpIncrement(controller,button)
elif name == "down":
print("Moving controller down for a single step")
self.moveDownIncrement(controller,button)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec_())