2016-10-25 1 views
1

QtDesigner 및 PyQT 4.11을 사용하여 Python 2.7에서 응용 프로그램을 만들려고합니다.연결된 신호가 눌려지지 않아

클릭하면 응용 프로그램에서 QSpinBox의 값을 변경하는 QPushButton을 두 개 갖고 싶습니다. 내가 신호 슬롯 정비공과 문제에 관한 StackOverflow 답변의 여러 페이지를 읽었지만, 내 문제에 대한 답변을 찾을 수 없습니다.

인스턴트 메신저 Ui_W_Setup.py, 여기에 rellevant 일부를 생성하는 pyuic4를 사용하여 :

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() 
: 다음

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")) 

나는 응용 프로그램을 ui.py을로드하고 실행하는 주요 파이썬 파일이

예외가 발생하지 않고 콘솔에 인쇄물이 표시되지 않습니다.

답변

0

작성한 MotorClass의 인스턴스에 대한 참조를 유지하지 않아 신호 연결이 작동하지 않습니다. 가비지 수집되면 신호 연결이 자동으로 제거됩니다.

아래의 코드는이 문제뿐만 아니라 MotorClass의 다른 문제 (예 : window 변수에 대한 전역 참조)를 해결합니다.

0

예를 들어,이 라인 >>

self.B_FileOpen.clicked.connect(fileSelect) 

당신이 당신의 기능 "fileSelect는"아직 존재하지 않는이 연결을 확인하십시오.

모두 선언 한 후에 "연결"을 시도하십시오.

특히 "fileSelect"가 아닌 경우 다른 일부입니다. 그러나이 동작은 대개 아직 선언하지 않은 것을 연결하려고 할 때 발생합니다.

+0

답변 해 주셔서 감사합니다.하지만 클래스 인스턴스를로드 한 후에 메소드가 존재하지 않는 이유는 무엇입니까? Im는 python ans에 익숙하지 않습니다. 아직 모든 메커니즘을 알지 못합니다. 그리고 그 방법은 그 시점에 존재해야한다는 것이 나에게 의미가 있습니다. –

관련 문제