2016-10-25 14 views
1

私はPython 2.7でQtDesignerとPyQT 4.11でアプリケーションを作成しようとしています。接続された信号は、コールバックを実行しません

クリックしたときにQSpinBoxの値を変更する2つのQPushButtonが必要です。私は信号スロットのメカニックの問題に関するStackOverflow答えのいくつかのページを読んだが、私の問題への答えを見つけることができませんでした。ここでは、Ui_W_Setup.pyを生成するrellevant一部をpyuic4を使用して

イム:

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をロードし、アプリケーションを実行するために、メインのPythonファイルを持っている:

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

例外がスローされず、プリントもコンソールに表示されません。

答えて

0

作成したMotorClassのインスタンスへの参照を保持していないため、信号接続が機能しません。ガベージコレクションされると、信号接続は自動的に削除されます。

この問題は、MotorClassのその他の問題(たとえば、window変数へのグローバル参照)と同様に、以下のコードで修正されています。

class MainWindow(QtGui.QMainWindow, Ui_W_Setup): 
    def __init__(self): 
    QtGui.QMainWindow.__init__(self) 
    self.setupUi(self) 
    self.motor = MotorClass(self) 
    self.B_ZuNehm.clicked.connect(self.motor.zuNehm) 
    self.B_ZuTast.clicked.connect(self.motor.zuTast) 

class MotorClass(QtCore.QObject): 
    def __init__(self, parent): 
    super(MotorClass, self).__init__(parent) 
    self.confirm = QtGui.QMessageBox() 
    self.confirm.setText('Verfahrweg frei?') 
    self.confirm.setStandardButtons(QtGui.QMessageBox.No | QtGui.QMessageBox.Yes) 
    self.confirm.setDefaultButton(QtGui.QMessageBox.Yes) 

    def zuNehm(self): 
    print("TestNehm") 
    response = self.confirm.exec_() 
    if response == QtGui.QMessageBox.Yes: 
     self.parent().SB_Position.setValue(float(1)) 

    def zuTast(self): 
    print("TestTast") 
    response = self.confirm.exec_() 
    if response == QtGui.QMessageBox.Yes: 
     self.parent().SB_Position.setValue(float(2)) 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    window = MainWindow() 
    window.show() 
    sys.exit(app.exec_()) 
0

たとえば、この行>>

self.B_FileOpen.clicked.connect(fileSelect) 

あなたはあなたの関数「fileSelect」はまだ存在しません。この接続を行います。

すべてを宣言した後に「接続」を試みてください。

"fileSelect"でない場合は、特に他のファイルの一部です。しかし、この動作は通常、まだ宣言されていないものを接続しようとするときに発生します。

+0

答えていただきありがとうございます。しかし、クラスのインスタンスを読み込んだ後にメソッドが存在しないのはなぜですか?私はPythonに新しいので、まだすべての力学を知っていると私には意味がある方法は、その時点で存在する必要があります。 –

関連する問題