我正在开发GUI应用程序。从主窗口我打开新的弹出窗口Dialog = myDialog() (所以两者现在都是开放的)。如果我按下新弹出窗口上的按钮,并使用以下代码捕获信号:
class Ui_DialogCalibration(object):
def setupUi(self, DialogCalibration):
DialogCalibration.setObjectName(_fromUtf8("DialogCalibration"))
DialogCalibration.resize(888, 378)
self.widget = QtGui.QWidget(DialogCalibration)
self.widget.setGeometry(QtCore.QRect(10, 150, 641, 144))
self.widget.setObjectName(_fromUtf8("widget"))
self.horizontalLayout_5 = QtGui.QHBoxLayout(self.widget)
self.horizontalLayout_5.setMargin(0)
self.horizontalLayout_5.setObjectName(_fromUtf8("horizontalLayout_5"))
self.radioButtonManual = QtGui.QRadioButton(self.widget)
self.radioButtonManual.setObjectName(_fromUtf8("radioButtonManual"))
self.horizontalLayout_5.addWidget(self.radioButtonManual)
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout_4 = QtGui.QHBoxLayout()
self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
self.verticalLayout_4 = QtGui.QVBoxLayout()
self.verticalLayout_4.setObjectName(_fromUtf8("verticalLayout_4"))
self.pushButtonDesnoX = QtGui.QPushButton(self.widget)
self.pushButtonDesnoX.setObjectName(_fromUtf8("pushButtonDesnoX"))
self.verticalLayout_4.addWidget(self.pushButtonDesnoX)
self.lineEdit = QtGui.QLineEdit(self.widget)
self.lineEdit.setText(_fromUtf8(""))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.verticalLayout_4.addWidget(self.lineEdit)
self.pushButtonLijevoX = QtGui.QPushButton(self.widget)
self.pushButtonLijevoX.setObjectName(_fromUtf8("pushButtonLijevoX"))
self.verticalLayout_4.addWidget(self.pushButtonLijevoX)
self.horizontalLayout_4.addLayout(self.verticalLayout_4)
self.verticalLayout_3 = QtGui.QVBoxLayout()
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
self.pushButtonDesnoY = QtGui.QPushButton(self.widget)
self.pushButtonDesnoY.setObjectName(_fromUtf8("pushButtonDesnoY"))
self.verticalLayout_3.addWidget(self.pushButtonDesnoY)
self.lineEdit_2 = QtGui.QLineEdit(self.widget)
self.lineEdit_2.setText(_fromUtf8(""))
self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
self.verticalLayout_3.addWidget(self.lineEdit_2)
self.pushButtonLijevoY = QtGui.QPushButton(self.widget)
self.pushButtonLijevoY.setObjectName(_fromUtf8("pushButtonLijevoY"))
self.verticalLayout_3.addWidget(self.pushButtonLijevoY)
self.horizontalLayout_4.addLayout(self.verticalLayout_3)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.pushButtonDesnoZ = QtGui.QPushButton(self.widget)
self.pushButtonDesnoZ.setObjectName(_fromUtf8("pushButtonDesnoZ"))
self.verticalLayout_2.addWidget(self.pushButtonDesnoZ)
self.lineEdit_3 = QtGui.QLineEdit(self.widget)
self.lineEdit_3.setText(_fromUtf8(""))
self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
self.verticalLayout_2.addWidget(self.lineEdit_3)
self.pushButtoLijevoZ = QtGui.QPushButton(self.widget)
self.pushButtoLijevoZ.setObjectName(_fromUtf8("pushButtoLijevoZ"))
self.verticalLayout_2.addWidget(self.pushButtoLijevoZ)
self.horizontalLayout_4.addLayout(self.verticalLayout_2)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.pushButtonSondaSettings = QtGui.QPushButton(self.widget)
self.pushButtonSondaSettings.setObjectName(_fromUtf8("pushButtonSondaSettings"))
self.verticalLayout.addWidget(self.pushButtonSondaSettings)
self.pushButtonReset = QtGui.QPushButton(self.widget)
self.pushButtonReset.setObjectName(_fromUtf8("pushButtonReset"))
self.verticalLayout.addWidget(self.pushButtonReset)
self.horizontalLayout_5.addLayout(self.verticalLayout)
self.verticalLayout_5 = QtGui.QVBoxLayout()
self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
self.labelPozition = QtGui.QLabel(self.widget)
self.labelPozition.setObjectName(_fromUtf8("labelPozition"))
self.verticalLayout_5.addWidget(self.labelPozition)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.labelpozitionX = QtGui.QLabel(self.widget)
self.labelpozitionX.setObjectName(_fromUtf8("labelpozitionX"))
self.horizontalLayout.addWidget(self.labelpozitionX)
self.lineEditPozitionX = QtGui.QLineEdit(self.widget)
self.lineEditPozitionX.setObjectName(_fromUtf8("lineEditPozitionX"))
self.horizontalLayout.addWidget(self.lineEditPozitionX)
self.verticalLayout_5.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.labelPozitionY = QtGui.QLabel(self.widget)
self.labelPozitionY.setObjectName(_fromUtf8("labelPozitionY"))
self.horizontalLayout_2.addWidget(self.labelPozitionY)
self.lineEditPozitionY = QtGui.QLineEdit(self.widget)
self.lineEditPozitionY.setObjectName(_fromUtf8("lineEditPozitionY"))
self.horizontalLayout_2.addWidget(self.lineEditPozitionY)
self.verticalLayout_5.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
self.labelPozitionZ = QtGui.QLabel(self.widget)
self.labelPozitionZ.setObjectName(_fromUtf8("labelPozitionZ"))
self.horizontalLayout_3.addWidget(self.labelPozitionZ)
self.lineEditPozitionZ = QtGui.QLineEdit(self.widget)
self.lineEditPozitionZ.setObjectName(_fromUtf8("lineEditPozitionZ"))
self.horizontalLayout_3.addWidget(self.lineEditPozitionZ)
self.verticalLayout_5.addLayout(self.horizontalLayout_3)
self.horizontalLayout_5.addLayout(self.verticalLayout_5)
self.widget1 = QtGui.QWidget(DialogCalibration)
self.widget1.setGeometry(QtCore.QRect(20, 40, 631, 91))
self.widget1.setObjectName(_fromUtf8("widget1"))
self.verticalLayout_6 = QtGui.QVBoxLayout(self.widget1)
self.verticalLayout_6.setMargin(0)
self.verticalLayout_6.setObjectName(_fromUtf8("verticalLayout_6"))
self.radioButtonAutomatic = QtGui.QRadioButton(self.widget1)
self.radioButtonAutomatic.setObjectName(_fromUtf8("radioButtonAutomatic"))
self.verticalLayout_6.addWidget(self.radioButtonAutomatic)
self.buttonBox = QtGui.QDialogButtonBox(self.widget1)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.verticalLayout_6.addWidget(self.buttonBox)
self.labelpozitionX.setBuddy(self.lineEditPozitionX)
self.labelPozitionY.setBuddy(self.lineEditPozitionY)
self.labelPozitionZ.setBuddy(self.lineEditPozitionZ)
self.radioButtonManual.toggle()
self.buttonBox.setEnabled(False)
self.pushButtonDesnoX.setEnabled(False)
self.pushButtonDesnoY.setEnabled(False)
self.pushButtonDesnoZ.setEnabled(False)
self.pushButtonLijevoX.setEnabled(False)
self.pushButtonLijevoY.setEnabled(False)
self.pushButtoLijevoZ.setEnabled(False)
self.pushButtonReset.setEnabled(False)
self.lineEdit.setText("1")
self.lineEdit_2.setText("1")
self.lineEdit_3.setText("1")
self.X=None
self.Y=None
self.Z=None
self.retranslateUi(DialogCalibration)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), DialogCalibration.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), DialogCalibration.reject)
QtCore.QMetaObject.connectSlotsByName(DialogCalibration)
class myDialog(QtGui.QDialog,Ui_DialogCalibration):
def __init__(self,sonda, parent=None):
super(myDialog, self).__init__( parent)
self.toupleSonda=sonda
self.setupUi(self)
def on_pushButtonDesnoX_clicked(self):
value=int(self.lineEdit.text())
self.X=self.X+value
print self.X
def retranslateUi(self, DialogCalibration):
DialogCalibration.setWindowTitle(QtGui.QApplication.translate("DialogCalibration", "Calibration", None, QtGui.QApplication.UnicodeUTF8))
self.radioButtonManual.setText(QtGui.QApplication.translate("DialogCalibration", "manual", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonDesnoX.setText(QtGui.QApplication.translate("DialogCalibration", "Desno X", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonLijevoX.setText(QtGui.QApplication.translate("DialogCalibration", "Lijevo X", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonDesnoY.setText(QtGui.QApplication.translate("DialogCalibration", "Naprijed Y", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonLijevoY.setText(QtGui.QApplication.translate("DialogCalibration", "Nazad Y", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonDesnoZ.setText(QtGui.QApplication.translate("DialogCalibration", "Gore Z", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtoLijevoZ.setText(QtGui.QApplication.translate("DialogCalibration", "Dolje Z", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonSondaSettings.setText(QtGui.QApplication.translate("DialogCalibration", "Load", None, QtGui.QApplication.UnicodeUTF8))
self.pushButtonReset.setText(QtGui.QApplication.translate("DialogCalibration", "Reset", None, QtGui.QApplication.UnicodeUTF8))
self.labelPozition.setText(QtGui.QApplication.translate("DialogCalibration", "Current pozition", None, QtGui.QApplication.UnicodeUTF8))
self.labelpozitionX.setText(QtGui.QApplication.translate("DialogCalibration", "X", None, QtGui.QApplication.UnicodeUTF8))
self.labelPozitionY.setText(QtGui.QApplication.translate("DialogCalibration", "Y", None, QtGui.QApplication.UnicodeUTF8))
self.labelPozitionZ.setText(QtGui.QApplication.translate("DialogCalibration", "Z", None, QtGui.QApplication.UnicodeUTF8))
self.radioButtonAutomatic.setText(QtGui.QApplication.translate("DialogCalibration", "automatic", None, QtGui.QApplication.UnicodeUTF8))
我得到了这个输出
1
2
所以,这个方法已经执行了两次。我该如何解决这个问题? 弹出窗口上发出的每个其他信号都会发生相同的情况
答案 0 :(得分:13)
引用docs:
[连接信号的两种变体]也会出现带有可选参数的信号。 Qt的 使用多个信号实现这一点。例如,
QtGui.QAbstractButton
有以下信号:void clicked(bool checked = false);
Qt实现如下:
void clicked(); void clicked(bool checked);
pyqtSlot()
装饰器可用于指定哪个信号 应该连接到插槽。
所以你的问题来自于clicked
的{{1}}信号有两个重载的事实。由于Qt可以将信号连接到参数较少的插槽,QPushButton
可以成功地将两个重载连接到您的插槽,因此每次点击都会调用两次插槽。
您需要通过connectSlotsByName
指定适当的重载:
pyqtSlot