【问题标题】:PyQt4 - closing a dialog window, exec_() not workingPyQt4 - 关闭对话框窗口,exec_()不起作用
【发布时间】:2012-07-18 15:22:31
【问题描述】:

尝试使用 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 语句中,它也会崩溃。

我们将不胜感激。

【问题讨论】:

    标签: python dialog pyqt4


    【解决方案1】:

    你做错了。您不应修改 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"
    

    【讨论】:

    • 有什么特别的原因不应该更改 Qt Designer 生成的代码吗?我的意思是,即使我确实更改了它似乎也可以正常工作,那么我是否缺少某些技术原因来说明为什么我不应该这样做?但无论如何,这是一个很好的答案,如果 exec_() 返回 true 或 false,我没有想过要执行这些操作。谢谢!
    • @DaveLewis:您当然可以更改 Designer 生成的代码。如果你不破坏它,它会很好地工作,但它并不意味着手动修改。设计器生成的代码旨在通过setupUi“插入”到您的实际 MainWindow/Dialog/Widget 子类。你看,你的Ui_Dialog 不是QDialog 的子类。这就是为什么你不能做self.exec_() 因为exec_()QDialog 的一个方法。 This link 可能会给你一个使用 Designer 代码的基本思路。
    • 顺便说一句,您也可以忘记 Designer 并手动编写所有 GUI 部分(这是我更喜欢的)。从这个意义上说,Qt 非常友好。
    • 好的,非常感谢!是的,我通常更喜欢手工编码,但我在这个项目上的时间很短,而且对 Qt 相对较新(之前一直在使用 Tkinter。不寒而栗。)所以我大部分时间都使用了设计器。
    【解决方案2】:

    你可以重新实现 closeEvent ,这将帮助你在对话框退出之前做一些处理

    def closeEvent(self,event):
       print "I am here"
       event.accept()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多