即时通讯尝试使用QtDesigner和PyQT 4.11在Python 2.7中创建应用程序。连接信号点击不执行回调
在应用程序中,我希望有两个QPushButton在单击时更改QSpinBox的值。我已经阅读了有关Signal Slot机制的问题的几页StackOverflow答案,但无法找到我的问题的答案。
即时通讯使用pyuic4生成Ui_W_Setup.py,这里的rellevant部分:
class Ui_W_Setup(object):
def setupUi(self, W_Setup):
W_Setup.setObjectName(_fromUtf8("W_Setup"))
self.Motorsteuerung = QtGui.QDockWidget(W_Setup)
self.Motorsteuerung.setEnabled(True)
self.Motorsteuerung.setMinimumSize(QtCore.QSize(140, 365))
self.Motorsteuerung.setFeatures(QtGui.QDockWidget.DockWidgetFloatable|QtGui.QDockWidget.DockWidgetMovable)
self.Motorsteuerung.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea)
self.Motorsteuerung.setObjectName(_fromUtf8("Motorsteuerung"))
self.dockWidgetContents = QtGui.QWidget()
self.dockWidgetContents.setObjectName(_fromUtf8("dockWidgetContents"))
self.B_ZuNehm = QtGui.QPushButton(self.dockWidgetContents)
self.B_ZuNehm.setGeometry(QtCore.QRect(40, 250, 81, 71))
self.B_ZuNehm.setObjectName(_fromUtf8("B_ZuNehm"))
self.B_ZuTast = QtGui.QPushButton(self.dockWidgetContents)
self.B_ZuTast.setGeometry(QtCore.QRect(40, 180, 81, 71))
self.B_ZuTast.setObjectName(_fromUtf8("B_ZuTast"))
self.SB_Position = QtGui.QDoubleSpinBox(self.dockWidgetContents)
self.SB_Position.setGeometry(QtCore.QRect(40, 21, 81, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.SB_Position.setFont(font)
self.SB_Position.setFrame(True)
self.SB_Position.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.SB_Position.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.SB_Position.setSpecialValueText(_fromUtf8(""))
self.SB_Position.setCorrectionMode(QtGui.QAbstractSpinBox.CorrectToNearestValue)
self.SB_Position.setDecimals(1)
self.SB_Position.setMaximum(50.0)
self.SB_Position.setSingleStep(0.2)
self.SB_Position.setProperty("value", 0.0)
self.SB_Position.setObjectName(_fromUtf8("SB_Position"))
然后,我有一个主要的Python文件来加载ui.py和执行应用程序:
import sys
from PyQt4 import QtGui,QtCore
from Setup import Ui_W_Setup
app = QtGui.QApplication(sys.argv)
class MainWindow(QtGui.QMainWindow, Ui_W_Setup):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
motor = MotorClass()
self.B_ZuNehm.clicked.connect(motor.zuNehm)
self.B_ZuTast.clicked.connect(motor.zuTast)
class MotorClass(QtCore.QObject):
def __init__(self):
super(MotorClass, self).__init__()
global window
confirm = QtGui.QMessageBox()
confirm.setText('Verfahrweg frei?')
confirm.setStandardButtons(QtGui.QMessageBox.No | QtGui.QMessageBox.Yes)
confirm.setDefaultButton(QtGui.QMessageBox.Yes)
@QtCore.pyqtSlot() #shouldnt be needed with my current knowledge
def zuNehm(self):
print("TestNehm")
response = self.confirm.exec_()
if response == 16384: #seems to be the return value for "Yes"
window.SB_Position.setValue(float(1))
@QtCore.pyqtSlot() #shouldnt be needed aswell with my current knowledge
def zuTast(self):
print("TestTast")
response = self.confirm.exec_()
if response == 16384:
window.SB_Position.setValue(float(2))
def main():
global window #I dont like declaring it globally , is there a better way
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
没有任何抛出,打印也没有出现在控制台中。
谢谢你的回答,但是为什么在我加载一个类的实例之后该方法不存在?即时通讯新的python ans所以不知道所有的机制,这对我来说是有意义的,该方法应该存在的那一点。 –