QDialog-基础讲解

注意:前面都是基础讲解,如果有什么不懂的可以看看,但是如果只是想看实际运用场景,建议从自定义案例开始看


The QDialog class is the base class of dialog windows
QDialog 类是对话框窗口的基类

详细解释
A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value, and they can have default buttons. QDialogs can also have a QSizeGrip in their lower-right corner, using setSizeGripEnabled().Note that QDialog (and any other widget that has type Qt::Dialog) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent’s top-level widget (if it is not top-level itself). It will also share the parent’s taskbar entry.
Use the overload of the QWidget::setParent() function to change the ownership of a QDialog widget. This function allows you to explicitly set the window flags of the reparented widget; using the overloaded function will clear the window flags specifying the window-system properties for the widget (in particular it will reset the Qt::Dialog flag).
Note: The parent relationship of the dialog does not imply that the dialog will always be stacked on top of the parent window. To ensure that the dialog is always on top, make the dialog modal. This also applies for child windows of the dialog itself. To ensure that child windows of the dialog stay on top of the dialog, make the child windows modal as well.

对话框窗口是主要用于短期任务和与用户简短交流的顶层窗口。QDialog可以是模态或非模态的,能够提供返回值并设置默认按钮。通过在右下角启用setSizeGripEnabled(),QDialog还可以包含尺寸调整手柄。需注意QDialog(以及任何具有Qt::Dialog类型的部件)对父部件的处理方式与Qt其他类略有不同:对话框始终是顶层部件,但若设置了父部件,其默认位置将居中显示在父部件的顶层窗口之上(若父部件本身非顶层)。对话框还会共享父部件的任务栏条目。通过QWidget::setParent()函数的重载版本可变更QDialog部件的所有权关系,该函数允许显式设置重新指定父部件后的窗口标志;使用重载函数将清除指定部件窗口系统属性的标志(特别是会重置Qt::Dialog标志)。
注意:对话框的父级关系并不意味着对话框始终堆叠在父窗口之上。要确保对话框始终置顶,需将其设为模态。此规则同样适用于对话框自身的子窗口——要使对话框的子窗口保持置顶状态,也需将这些子窗口设为模态。

标准对话框

即Qt本身包含的简易对话框。基本分为以下几类:

  • QColorDialog: 颜色对话框;
  • QFileDialog: 选择文件或者目录对话框;
  • QFontDialog: 字体对话框;
  • QInputDialog: 允许用户输入一个值,并将其值返回;
  • QMessageBox: 模态对话框,用于显示信息、询问问题等;
  • QPageSetupDialog: 为打印机提供纸张相关的选项对话框;
  • QPrintDialog: 打印机配置对话框;
  • QPrintPreviewDialog:打印预览对话框;
  • QProgressDialog: 显示操作过程对话框;

自定义案例

MaintenanceDialog::MaintenanceDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("个人信息记录");
    setMinimumSize(500, 400);

    QFormLayout *formLayout = new QFormLayout();
    
    m_adaptationParams = new QLineEdit(this);
    m_softwareUpdate = new QLineEdit(this);
    m_calibrationProcess = new QLineEdit(this);
    m_keyComponentTestParams = new QLineEdit(this);
    m_replacementInfo = new QLineEdit(this);

    formLayout->addRow("姓名:", m_adaptationParams);
    formLayout->addRow("年龄:", m_softwareUpdate);
    formLayout->addRow("性别:", m_calibrationProcess);
    formLayout->addRow("爱好:", m_keyComponentTestParams);
    formLayout->addRow("目标:", m_replacementInfo);

    m_saveButton = new QPushButton("保存", this);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(formLayout);
    mainLayout->addWidget(m_saveButton, 0, Qt::AlignRight);

    setLayout(mainLayout);

    connect(m_saveButton, &QPushButton::clicked, this, &MaintenanceDialog::onSaveButtonClicked);
}

请添加图片描述

问题互动

弹出来的界面,被置顶后,原有界面是否还会存在?
请添加图片描述
假设存在,如下图,有什么办法,在没有完成当前界面任务前,不能使之点击其它任务?
揭秘:

void MainWindow::onShowMaintenanceDialog()
{
    m_dialog->exec();
}

基础知识:对话框分为模态对话框和非模态对话框。

  • 模态对话框,就是会阻塞同一应用程序中其它窗口的输入。
    模态对话框很常见,比如“打开文件”功能。你可以尝试一下记事本的打开文件,当打开文件对话框出现时,我们是不能对除此对话框之外的窗口部分进行操作的。
  • 与此相反的是非模态对话框,例如查找对话框,我们可以在显示着查找对话框的同时,继续对记事本的内容进行编辑。

案例互动

qt-弹框提示-界面提醒
案例源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世转神风-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值