1

PyQt4 を使用してユーザー インターフェイスを構築しようとしています。ポップアップするダイアログ ウィンドウを取得しました。'Ok' を押すと、何かを実行してから閉じます。残念ながら、私はそれを機能させることができないようです - Dialog.exec_()、Dialog.close()、self.exec_()、self.close()のあらゆる種類の組み合わせを試し、Dialogに「受け入れられた」信号を発しました.accept など。これまでのところ、何も機能していません。その理由はよくわかりません。そのコードは次のとおりです。

そのように初期化されたダイアログウィンドウ。

def begin_grab(self):
    self.GrabIm=qtg.QDialog(self)
    self.GrabIm.ui=Ui_Dialog()
    self.GrabIm.ui.setupUi(self.GrabIm)
    self.GrabIm.show()

ダイアログ ウィンドウ。

class Ui_Dialog(object):

    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        ...
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), self.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def accept(self):
        if self.radioButton.isChecked()==True: #assume it is true
            #Call continuous grabber
            print "Grabbing continuously"
            Dialog.exec_() #Close it here
        else:
            #Call trigger server
            print "Grabbing triggered"
            self.exec_()

主に発生し続けるのは、「Dialog」が不明な変数であるというメッセージが accept() 関数で表示されるか、self.exec_() などを使用すると、exec_() が既知の属性ではないというメッセージが表示されることです。accept(self, Dialog) を実行しようとして、connect ステートメントに self.accept(Dialog) を入れると、クラッシュします。

ありとあらゆる助けをいただければ幸いです。

4

2 に答える 2

10

あなたはそれをかなり間違っています。Qt Designer で生成されたコード ( Ui_Dialog) を変更しないでください。そこをサブクラス化QDialogして定義する必要acceptがあります。

class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        # use new style signals
        self.ui.buttonBox.accepted.connect(self.accept)
        self.ui.buttonBox.rejected.connect(self.reject)

    def accept(self):
        if self.ui.radioButton.isChecked(): # no need to do ==True
            #Call continuous grabber
            print "Grabbing continuously"
        else:
            #Call trigger server
            print "Grabbing triggered"
        super(MyDialog, self).accept()  # call the accept method of QDialog. 
                                           # super is needed 
                                           # since we just override the accept method

次に、次のように初期化します。

def begin_grab(self):
    self.GrabIm=MyDialog(self)
    self.GrabIm.exec_()  # exec_() for modal dialog
                           # show() for non-modal dialog

しかし、あなたのコードを見ると、私はそのようにはしません。ダイアログを返してaccept/rejectから、呼び出し元 (つまり、メイン ウィンドウ) で条件付きの処理を行います。

class MyDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(MyDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        # use new style signals
        self.ui.buttonBox.accepted.connect(self.accept)
        self.ui.buttonBox.rejected.connect(self.reject)

および呼び出し元コード:

def begin_grab(self):
    self.GrabIm=MyDialog(self)
    if self.GrabIm.exec_():  # this will be True if dialog is 'accept'ed, False otherwise
        if self.GrabIm.ui.radioButton.isChecked():
            #Call continuous grabber
            print "Grabbing continuously"
        else:
            #Call trigger server
            print "Grabbing triggered"
于 2012-07-19T03:21:27.937 に答える
0

ダイアログが終了する前にいくつかのプロセスを実行するのに役立つ closeEvent を再実装できます

def closeEvent(self,event):
   print "I am here"
   event.accept()
于 2012-07-18T18:10:30.793 に答える