1

尝试使用 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_()

不断发生的主要事情是在 accept() 函数中显示“Dialog”是未知变量的消息,或者如果我使用 self.exec_() 或类似的,它说 exec_() 不是已知属性。如果我尝试做accept(self, Dialog),并将self.accept(Dialog) 放在connect 语句中,它也会崩溃。

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

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 回答