PyQt-已确认QComboBox连接,但未调用函数

问题描述 投票:2回答:1

我在使用PyQt处理信号/插槽问题时遇到了一些麻烦。我的代码在下面,但是可能值得解释。前两个QObject.connect()返回True,因此我知道已建立连接。但是,在更改comboBox中的选择时,未按预期调用函数getParameters。下面的5个连接用于调试和测试与ComboBox相关的其他信号。这些也不会按预期方式打印日志。

从我阅读过的其他文章中,可以找到更新连接的方法,这可能是问题吗?如果是这样,有人可以给我提供这种格式的示例吗?谢谢!

#interactive GUI connections:
    resultCombo = QObject.connect(self.dlg.ui.comboBox, SIGNAL("currentIndexChanged(const QString & text)"), self.getParameters)
    resultSpin = QObject.connect(self.dlg.ui.spinBox_bands, SIGNAL("valueChanged(int i)"), self.getParameters)
    QMessageBox.information( self.iface.mainWindow(),"Info", "connections: ComboBox = %s SpinBox = %s"%(str(resultCombo), str(resultSpin)) )
    QObject.connect(self.dlg.ui.comboBox, SIGNAL("currentIndexChanged(const QString & text)"), self.log1)
    QObject.connect(self.dlg.ui.comboBox, SIGNAL("currentIndexChanged(int index)"), self.log2)
    QObject.connect(self.dlg.ui.comboBox, SIGNAL("currentTextChanged(const QString & text)"), self.log3)
    QObject.connect(self.dlg.ui.comboBox, SIGNAL("highlighted(const QString & text)"), self.log4)
    QObject.connect(self.dlg.ui.comboBox, SIGNAL("activated(const QString & text)"), self.log5)

def log1(self,  input):
    QgsMessageLog.logMessage("currentIndexChanged string. input = " + str(input),  "Debug",  0)
def log2(self,  input):
    QgsMessageLog.logMessage("currentIndexChanged int. input = " + str(input),  "Debug",  0)
def log3(self,  input):
    QgsMessageLog.logMessage("currentTextChanged string. input = " + str(input),  "Debug",  0)
def log4(self,  input):
    QgsMessageLog.logMessage("highlighted string. input = " + str(input),  "Debug",  0)
def log5(self,  input):
    QgsMessageLog.logMessage("cactivated string. input = " + str(input),  "Debug",  0)
python pyqt qcombobox qt-signals
1个回答
4
投票

我解决了。如我所料,它确实与“新样式”连接语法有关。我不完全确定为什么要连接旧样式,但不调用connected函数,但是现在可以使用以下代码:

    self.dlg.ui.comboBox.currentIndexChanged['QString'].connect(self.getParameters)
    self.dlg.ui.spinBox_bands.valueChanged.connect(self.getParameters)

对于那些不知道(我也没有找到好的文档-链接?)的人,['QString']参数允许您选择过载信号的结果类型。这对我很重要,因为我使用类型来区分发件人。但是,我想我应该更明确一些,并使用

sender = self.sender()

在我的getParameters函数中,但这是可行的。

© www.soinside.com 2019 - 2024. All rights reserved.