【问题标题】:How can I retrieve a signal parameter with New-Style Syntax?如何使用 New-Style Syntax 检索信号参数?
【发布时间】: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()

【问题讨论】:

    标签: python qt pyqt pyside


    【解决方案1】:

    目前的插槽设计看起来很混乱。乍一看像是一个实例方法,但实际上只是一个带有假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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多