在子小部件处于活动状态时阻止 QMa​​inWindow , pyqt [英] Block QMainWindow while child widget is alive , pyqt

查看:68
本文介绍了在子小部件处于活动状态时阻止 QMa​​inWindow , pyqt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当用户按下按钮时,在 MainWindow 被阻止等待表单填写后会出现一个表单

I want when the user press the button a form will appear after MainWindow is blocked pending form filling

推荐答案

您需要使用 QDialog 并使用 exec 显示它,这将阻止应用程序的其余部分,直到它关闭.exec 的返回值还告诉您表单是否在未提交更改的情况下关闭(即取消).

You need to use a QDialog and show it using exec, which will block the rest of the application until it is closed. The return value of exec also tells you whether the form was closed without committing changes (i.e. cancelled).

这是一个简单的演示脚本,展示了如何使用 QDialog:

Here is a simple demo script that shows how to use a QDialog:

from PyQt4 import QtCore, QtGui

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.checkbox1 = QtGui.QCheckBox('Option one', self)
        self.checkbox2 = QtGui.QCheckBox('Option two', self)
        self.buttonOk = QtGui.QPushButton('Ok', self)
        self.buttonOk.clicked.connect(self.accept)
        self.buttonCancel = QtGui.QPushButton('Cancel', self)
        self.buttonCancel.clicked.connect(self.reject)
        layout = QtGui.QGridLayout(self)
        layout.addWidget(self.checkbox1, 0, 0, 1, 2)
        layout.addWidget(self.checkbox2, 1, 0, 1, 2)
        layout.addWidget(self.buttonOk, 2, 0)
        layout.addWidget(self.buttonCancel, 2, 1)

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        widget = QtGui.QWidget(self)
        layout = QtGui.QVBoxLayout(widget)
        self.button = QtGui.QPushButton('Show Dialog', self)
        self.button.clicked.connect(self.handleButton)
        layout.addWidget(self.button)
        self.setCentralWidget(widget)

    def handleButton(self):
        dialog = Dialog(self)
        if dialog.exec_() == QtGui.QDialog.Accepted:
            print('Option one: %s' % dialog.checkbox1.isChecked())
            print('Option two: %s' % dialog.checkbox2.isChecked())
        else:
            print('Cancelled')
        dialog.deleteLater()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 200, 100)
    window.show()
    sys.exit(app.exec_())

这篇关于在子小部件处于活动状态时阻止 QMa​​inWindow , pyqt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆