如何使用新式语法检索信号参数?
How can I retrieve a signal parameter with New-Style Syntax?
在自定义按钮内 class 我有一个信号,当有人将东西放入其中时会发出信号。相关方法在这里:
class CustomButton
linked = QtCore.pyqtSignal()
...
def dropEvent(self, e):
print e.source().objectName()
print self.objectName()
# set the drop action as LinkAction
e.setDropAction(QtCore.Qt.LinkAction)
# tell the QDrag we accepted it
e.accept()
#Emit linked signal with the drag object's name as parameter
self.linked.emit( e.source().objectName() )
return QtGui.QPushButton.dropEvent(self, QtGui.QDropEvent(QtCore.QPoint(e.pos().x(), e.pos().y()), e.possibleActions(), e.mimeData(), e.buttons(), e.modifiers()))
另一方面,在 class 之外,我在主应用程序中创建了一个插槽,以及一种将其连接到信号的方法。
#The slot (actually is just a python callable)
def on_link(self):
input = self.sender().objectName()[4:]
print input
#I need to print the name of the other object emitted as str parameter in the signal....
#Instance of custom button
custom_button.linked.connect( lambda: on_link( custom_button ) )
此时我已经知道可以获取信号的sender()
,但是不知道如何获取self.linked.emit( e.source().objectName() )
的参数。我只知道首先我必须先改变这个:linked = QtCore.pyqtSignal(str),但不知道如何编写连接或插槽并检索发射信号中的 e.source().objectName()
。
插槽的当前设计看起来很混乱。乍一看,它看起来像一个实例方法,但实际上它只是一个带有假 self
参数的模块级函数。
我会建议更简单、更明确的建议,例如:
class CustomButton(QtGui.QPushButton):
linked = QtCore.pyqtSignal(str, str)
def dropEvent(self, event):
...
self.linked.emit(self.objectName(), event.source().objectName())
return QtGui.QPushButton.dropEvent(self, event)
def on_link(btn_name, src_name):
print btn_name, src_name
custom_button.linked.connect(on_link)
另一种设计是发送对象而不是它们的名称:
linked = QtCore.pyqtSignal(object, object)
...
self.linked.emit(self, event.source())
def on_link(button, source):
print button.objectName(), source.objectName()
在自定义按钮内 class 我有一个信号,当有人将东西放入其中时会发出信号。相关方法在这里:
class CustomButton
linked = QtCore.pyqtSignal()
...
def dropEvent(self, e):
print e.source().objectName()
print self.objectName()
# set the drop action as LinkAction
e.setDropAction(QtCore.Qt.LinkAction)
# tell the QDrag we accepted it
e.accept()
#Emit linked signal with the drag object's name as parameter
self.linked.emit( e.source().objectName() )
return QtGui.QPushButton.dropEvent(self, QtGui.QDropEvent(QtCore.QPoint(e.pos().x(), e.pos().y()), e.possibleActions(), e.mimeData(), e.buttons(), e.modifiers()))
另一方面,在 class 之外,我在主应用程序中创建了一个插槽,以及一种将其连接到信号的方法。
#The slot (actually is just a python callable)
def on_link(self):
input = self.sender().objectName()[4:]
print input
#I need to print the name of the other object emitted as str parameter in the signal....
#Instance of custom button
custom_button.linked.connect( lambda: on_link( custom_button ) )
此时我已经知道可以获取信号的sender()
,但是不知道如何获取self.linked.emit( e.source().objectName() )
的参数。我只知道首先我必须先改变这个:linked = QtCore.pyqtSignal(str),但不知道如何编写连接或插槽并检索发射信号中的 e.source().objectName()
。
插槽的当前设计看起来很混乱。乍一看,它看起来像一个实例方法,但实际上它只是一个带有假 self
参数的模块级函数。
我会建议更简单、更明确的建议,例如:
class CustomButton(QtGui.QPushButton):
linked = QtCore.pyqtSignal(str, str)
def dropEvent(self, event):
...
self.linked.emit(self.objectName(), event.source().objectName())
return QtGui.QPushButton.dropEvent(self, event)
def on_link(btn_name, src_name):
print btn_name, src_name
custom_button.linked.connect(on_link)
另一种设计是发送对象而不是它们的名称:
linked = QtCore.pyqtSignal(object, object)
...
self.linked.emit(self, event.source())
def on_link(button, source):
print button.objectName(), source.objectName()