【发布时间】:2015-03-22 13:01:17
【问题描述】:
在自定义按钮类中,我有一个信号,当将某些东西放入其中时会发出信号。这里是相关方法:
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()))
另一方面,在课堂之外,在主应用程序中,我正在创建一个插槽,以及一种将其连接到信号的方法。
#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()。
【问题讨论】: