#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDialog dialog;
dialog.setWindowTitle("Modal Dialog Example");
QVBoxLayout *layout = new QVBoxLayout(&dialog);
QLabel *label = new QLabel("This is a modal dialog.", &dialog);
layout->addWidget(label);
QPushButton *okButton = new QPushButton("OK", &dialog);
layout->addWidget(okButton);
QPushButton *closeButton = new QPushButton("close", &dialog);
layout->addWidget(closeButton);
QObject::connect(okButton, &QPushButton::clicked, [&dialog]() {
dialog.accept(); // 关闭对话框并返回 QDialog::Accepted
});
QObject::connect(closeButton, &QPushButton::clicked, [&dialog]() {
dialog.reject(); // 关闭对话框并返回 QDialog::Rejected
});
int result = dialog.exec(); // 显示模态对话框
if (result == QDialog::Accepted) {
qInfo() << "User clicked OK.";
} else {
qInfo() << "User closed the dialog.";
}
return app.exec();
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt模态对话框Demo
共2个文件
pro:1个
cpp:1个
0 下载量 101 浏览量
2023-10-14
09:27:53
上传
评论
收藏 1KB ZIP 举报
温馨提示
在Qt中,`QDialog` 是一个用于创建对话框的类。模态对话框(Modal Dialog)是一种阻塞式对话框,当用户与对话框交互时,阻止用户与应用程序的其他部分进行交互。在用户关闭模态对话框之前,主窗口和其他窗口将无法响应用户输入。 以下是如何创建和使用模态对话框的基本步骤: 1. **创建`QDialog`对象:** ```cpp QDialog *dialog = new QDialog(parent); ``` 这里,`parent` 是可选的,表示对话框的父窗口。 2. **设置对话框的标题和内容:** ```cpp dialog->setWindowTitle("Modal Dialog Example"); QLabel *label = new QLabel("This is a modal dialog.", dialog); ``` 3. **添加按钮或其他控件:** ```cpp QPushButton *okButton = new QPushButton("OK", dialog)
资源推荐
资源详情
资源评论
收起资源包目录
6_2.zip (2个子文件)
6_2
6_2.pro 1KB
main.cpp 1KB
共 2 条
- 1
资源评论
三雷科技
- 粉丝: 3w+
- 资源: 100
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功