【问题标题】:How to specifically connect to overloaded signals?如何专门连接过载信号?
【发布时间】:2017-12-14 14:29:57
【问题描述】:

我正在使用带有 2 个重载的信号

buttonClicked = pyqtSignal([int],[str])

我只想用一个插槽连接一个重载(int)。每当我调用发出另一个重载(str)时,我都不希望发生任何事情。如何做到这一点?

class Example(QWidget):

    buttonClicked = pyqtSignal([int],[str])

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.btn = QPushButton('Button',self)
        self.btn.clicked.connect(self.doAction)
        self.make_conn()
        self.setWindowTitle('Yo')
        self.show()

    def make_conn(self):
        self.buttonClicked.connect(self.showDialog) #How to make specific connection here . Using self.buttonClicked[int].connect(self.showDialog) doesnt work.

    def showDialog(self):
        print('here')

    def doAction(self):
        self.buttonClicked.emit('soru') #should NOT call showDialog
        self.buttonClicked.emit(23) #should call showDialog

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 signals-slots


    【解决方案1】:

    好的,我搜索了网络,不知何故找到了解决方案和一些有趣的东西。

    首先,当使用emit() 时,我必须通过指定类型来指定重载。

    例如,在上面的示例中,如果我想为 str 版本发出信号,我必须调用 self.buttonClicked[str].emit('soru') 。 其次,我必须通过在将信号与插槽连接时告诉它是str 还是int 来指定重载版本详细信息。喜欢 self.buttonClicked[str].connect(showDialog).

    所以如果现在我专门发出 2 个信号:

    self.buttonClicked[str].emit('soru')
    self.buttonClicked[int].emit(23)
    

    那么只有str 版本会调用showDialog。 现在我在连接时没有指定重载版本:

    self.buttonClicked.connect(showDialog)
    

    只有在创建pyqtSignal([int],[str]) 时首先指定的重载版本才会被调用。所以在这里,只有 'int' 版本将连接到插槽。

    来源:source

    【讨论】:

      猜你喜欢
      • 2022-07-04
      • 2012-02-23
      • 1970-01-01
      • 2013-11-30
      • 2011-07-19
      • 2018-09-07
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多