PyQt5 模态对话框与非模态对话框的区别

模态对话框与非模态对话框的区别

记录博文

一、模态对话框

  1. 应用程序模态对话框:一旦调用该对话框,它就会成为应用程序唯一能够与用户进行交互的部件。在关闭该对话框之前,用户都不能使用应用程序的其他部件。当然,用户还可以自由使用其他的应用程序。比如,通过单机其他应用程序而使其获得光标。
  2. 窗口模态对话框:与应用程序模态对话框工作方式相似,除了它会阻止与其父窗口、父窗口的父窗口并直至顶层窗口等的交互,当然也会阻止与父窗口同层各兄弟窗口的交互。对于只有一个顶层窗口的那些应用程序老说,在实践上不会存在应用程序模态与窗口模态的区别。

二、非模态对话框

当调用非模态对话框的时候,用户可以与该对话框以及应用程序的其它部分交互。这会对如何设计代码产生影响,因为用户可以既从主窗口又从非模态对话框两边同时修改程序的状态,这就可能造成两者相互影响。

三、show() 与 exec_()的区别

exec() 有返回值,show() 没有返回值。其次这两个方法的作用也不同。调用 show() 的作用仅仅是将 Widget 及其上的内容都显示出来。而调用 exec() 后,调用线程将会被阻塞,直到 Dialog 关闭。

实际的小例子:

    def setPenProperties(self):
        dialog = PenPropertiesDlg(self)
        dialog.widthSpinBox.setValue(self.width)
        dialog.beveledCheckBox.setChecked(self.beveled)
        dialog.styleComboBox.setCurrentIndex(dialog.styleComboBox.findText(self.style))
        if dialog.exec_():
            self.width = dialog.widthSpinBox.value()
            self.beveled = dialog.beveledCheckBox.isChecked()
            self.style = dialog.styleComboBox.currentText()
            self.updateData()

当对话框窗口关闭之后,获取对话框的宽度微调框(widthSpinBox)的值,获取复选框(beveledCheckBox)的选择状态,获取组合框(styleComboBox)的文本字符串,并刷新主窗口。

四、完整的实例代码

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class PenPropertiesDlg(QDialog):

    def __init__(self, parent=None):
        super(PenPropertiesDlg, self).__init__(parent)

        widthLabel = QLabel("&Width:")
        self.widthSpinBox = QSpinBox()
        widthLabel.setBuddy(self.widthSpinBox)
        self.widthSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
        self.widthSpinBox.setRange(0, 24)
        self.beveledCheckBox = QCheckBox("&Beveled edges")
        styleLabel = QLabel("&Style:")
        self.styleComboBox = QComboBox()
        styleLabel.setBuddy(self.styleComboBox)
        self.styleComboBox.addItems(["Solid", "Dashed", "Dotted",
                                     "DashDotted", "DashDotDotted"])
        okButton = QPushButton("&OK")
        cancelButton = QPushButton("Cancel")

        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)
        layout = QGridLayout()
        layout.addWidget(widthLabel, 0, 0)
        layout.addWidget(self.widthSpinBox, 0, 1)
        layout.addWidget(self.beveledCheckBox, 0, 2)
        layout.addWidget(styleLabel, 1, 0)
        layout.addWidget(self.styleComboBox, 1, 1, 1, 2)
        layout.addLayout(buttonLayout, 2, 0, 1, 3)
        self.setLayout(layout)

        okButton.clicked.connect(self.accept)
        cancelButton.clicked.connect(self.reject)
        self.setWindowTitle("Pen Properties")


class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        self.width = 1
        self.beveled = False
        self.style = "Solid"

        penButtonInline = QPushButton("Set Pen... (Dumb &inline)")
        penButton = QPushButton("Set Pen... (Dumb &class)")
        self.label = QLabel("The Pen has not been set")
        self.label.setTextFormat(Qt.RichText)

        layout = QVBoxLayout()
        layout.addWidget(penButtonInline)
        layout.addWidget(penButton)
        layout.addWidget(self.label)
        self.setLayout(layout)

        penButtonInline.clicked.connect(self.setPenInline)
        penButton.clicked.connect(self.setPenProperties)
        self.setWindowTitle("Pen")
        self.updateData()



    def updateData(self):
        bevel = ""
        if self.beveled:
            bevel = "<br>Beveled"
        self.label.setText("Width = {}<br>Style = {}{}".format(
                           self.width, self.style, bevel))


    def setPenInline(self):
        widthLabel = QLabel("&Width:")
        widthSpinBox = QSpinBox()
        widthLabel.setBuddy(widthSpinBox)
        widthSpinBox.setAlignment(Qt.AlignRight)
        widthSpinBox.setRange(0, 24)
        widthSpinBox.setValue(self.width)
        beveledCheckBox = QCheckBox("&Beveled edges")
        beveledCheckBox.setChecked(self.beveled)
        styleLabel = QLabel("&Style:")
        styleComboBox = QComboBox()
        styleLabel.setBuddy(styleComboBox)
        styleComboBox.addItems(["Solid", "Dashed", "Dotted",
                                "DashDotted", "DashDotDotted"])
        styleComboBox.setCurrentIndex(styleComboBox.findText(self.style))
        okButton = QPushButton("&OK")
        cancelButton = QPushButton("Cancel")

        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)
        layout = QGridLayout()
        layout.addWidget(widthLabel, 0, 0)
        layout.addWidget(widthSpinBox, 0, 1)
        layout.addWidget(beveledCheckBox, 0, 2)
        layout.addWidget(styleLabel, 1, 0)
        layout.addWidget(styleComboBox, 1, 1, 1, 2)
        layout.addLayout(buttonLayout, 2, 0, 1, 3)

        form = QDialog()
        form.setLayout(layout)
        okButton.clicked.connect(form.accept)
        cancelButton.clicked.connect(form.reject)
        form.setWindowTitle("Pen Properties")

        if form.exec_():
            self.width = widthSpinBox.value()
            self.beveled = beveledCheckBox.isChecked()
            self.style = styleComboBox.currentText()
            self.updateData()


    def setPenProperties(self):
        dialog = PenPropertiesDlg(self)
        dialog.widthSpinBox.setValue(self.width)
        dialog.beveledCheckBox.setChecked(self.beveled)
        dialog.styleComboBox.setCurrentIndex(dialog.styleComboBox.findText(self.style))
        if dialog.exec_():
            self.width = dialog.widthSpinBox.value()
            self.beveled = dialog.beveledCheckBox.isChecked()
            self.style = dialog.styleComboBox.currentText()
            self.updateData()
        
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = Form()
    form.resize(200, 200)
    form.show()
    app.exec_()

代码转自 《Python Qt Gui 快速编程 —— PyQt 编程指南》

点我回顶部

 
 
 
 
 
 
 
Fin.

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/109150565