PyQt4 - 关闭一个对话框窗口,exec_()不起作用

Dav*_*wis 0 python dialog pyqt4

尝试使用PyQt4构建用户界面.弹出一个对话框窗口,当按下"确定"时,我想让它做一些关闭然后关闭的东西.不幸的是,我似乎无法让它工作 - 尝试了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()
Run Code Online (Sandbox Code Playgroud)

对话窗口;

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_()
Run Code Online (Sandbox Code Playgroud)

持续发生的主要事情是在Dial()函数中使用"Dialog"是一个未知变量的消息,或者如果我使用self.exec_()或类似的消息,则说exec_()不是已知属性.如果我尝试接受(self,Dialog),并在connect语句中放置self.accept(Dialog),它也会崩溃.

任何和所有的帮助将不胜感激.

Ava*_*ris 8

你做得很错.您不应该修改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
Run Code Online (Sandbox Code Playgroud)

然后将其初始化为:

def begin_grab(self):
    self.GrabIm=MyDialog(self)
    self.GrabIm.exec_()  # exec_() for modal dialog
                           # show() for non-modal dialog
Run Code Online (Sandbox Code Playgroud)

但是看看你的代码,我不会这样做.让对话框返回,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)
Run Code Online (Sandbox Code Playgroud)

和来电者代码:

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"
Run Code Online (Sandbox Code Playgroud)