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

时间:2012-07-18 15:22:32

标签: 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()

对话窗口;

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

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

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

2 个答案:

答案 0 :(得分: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

然后将其初始化为:

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"

答案 1 :(得分:0)

您可以重新实现closeEvent,它将帮助您在Dialog退出

之前执行某些过程
def closeEvent(self,event):
   print "I am here"
   event.accept()