如何在仍在 __init__ 语句中(或紧随其后)时停止执行 QDialog?

发布于 2024-08-24 04:26:30 字数 1296 浏览 8 评论 0原文

我想知道如果 __init__ 语句中满足某些条件,如何阻止对话框打开。

下面的代码尝试调用“self.close()”函数并且确实如此,但是(我假设)由于对话框尚未启动其事件循环,因此它不会触发关闭事件?那么是否有另一种方法可以关闭和/或阻止对话框打开而不触发事件?

示例代码:

from PyQt4 import QtCore, QtGui

class dlg_closeInit(QtGui.QDialog):
    '''
    Close the dialog if a certain condition is met in the __init__ statement
    '''
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.txt_mytext = QtGui.QLineEdit('some text')
        self.btn_accept = QtGui.QPushButton('Accept')

        self.myLayout = QtGui.QVBoxLayout(self)
        self.myLayout.addWidget(self.txt_mytext)
        self.myLayout.addWidget(self.btn_accept)        

        self.setLayout(self.myLayout)
        # Connect the button
        self.connect(self.btn_accept,QtCore.SIGNAL('clicked()'), self.on_accept)
        self.close()

    def on_accept(self):
        # Get the data...
        self.mydata = self.txt_mytext.text()
        self.accept() 

    def get_data(self):
            return self.mydata

    def closeEvent(self, event):
        print 'Closing...'


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    dialog = dlg_closeInit()
    if dialog.exec_():
        print dialog.get_data()
    else:
        print "Failed"

I am wondering how I can go about stopping a dialog from opening if certain conditions are met in its __init__ statement.

The following code tries to call the 'self.close()' function and it does, but (I'm assuming) since the dialog has not yet started its event loop, that it doesn't trigger the close event? So is there another way to close and/or stop the dialog from opening without triggering an event?

Example code:

from PyQt4 import QtCore, QtGui

class dlg_closeInit(QtGui.QDialog):
    '''
    Close the dialog if a certain condition is met in the __init__ statement
    '''
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.txt_mytext = QtGui.QLineEdit('some text')
        self.btn_accept = QtGui.QPushButton('Accept')

        self.myLayout = QtGui.QVBoxLayout(self)
        self.myLayout.addWidget(self.txt_mytext)
        self.myLayout.addWidget(self.btn_accept)        

        self.setLayout(self.myLayout)
        # Connect the button
        self.connect(self.btn_accept,QtCore.SIGNAL('clicked()'), self.on_accept)
        self.close()

    def on_accept(self):
        # Get the data...
        self.mydata = self.txt_mytext.text()
        self.accept() 

    def get_data(self):
            return self.mydata

    def closeEvent(self, event):
        print 'Closing...'


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    dialog = dlg_closeInit()
    if dialog.exec_():
        print dialog.get_data()
    else:
        print "Failed"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-08-31 04:26:30

仅当调用 exec_ 方法时才会运行该对话框。因此,您应该检查 exec_ 方法中的条件,如果满足,则从 QDialog 运行 exec_。

其他方法是在构造函数内部引发异常(尽管我不确定,这是一个很好的做法;在其他语言中,您通常不应该允许在构造函数内部发生此类行为)并在外部捕获它。如果捕获异常,则不要运行 exec_ 方法。

请记住,除非运行 exec_,否则不需要关闭窗口。对话框已构建,但尚未显示。

The dialog will be run only if exec_ method is called. You should therefore check conditions in the exec_ method and if they are met, run exec_ from QDialog.

Other method is to raise an exception inside the constructor (though I am not sure, it is a good practice; in other languages you generally shouldn't allow such behaviour inside constructor) and catch it outside. If you catch an exception, simply don't run exec_ method.

Remember, that unless you run exec_, you don't need to close the window. The dialog is constructed, but not shown yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文