跳到主要内容

对话框

对话框是应用程序与用户交互的重要方式,Qt 提供了多种内置对话框和创建自定义对话框的方法。

QMessageBox(消息框)

用于显示信息、警告、错误和询问用户。

简单用法(静态方法)

#include <QMessageBox>

// 信息框
QMessageBox::information(this, "标题", "操作成功完成!");

// 警告框
QMessageBox::warning(this, "警告", "磁盘空间不足!");

// 错误框
QMessageBox::critical(this, "错误", "无法保存文件!");

// 询问框
QMessageBox::StandardButton reply = QMessageBox::question(
this,
"确认",
"确定要删除吗?",
QMessageBox::Yes | QMessageBox::No
);

if (reply == QMessageBox::Yes) {
// 执行删除
}

高级用法

// 自定义按钮
QMessageBox msgBox(this);
msgBox.setWindowTitle("保存更改");
msgBox.setText("文档已被修改,是否保存?");
msgBox.setIcon(QMessageBox::Question);

QPushButton *saveButton = msgBox.addButton("保存", QMessageBox::AcceptRole);
QPushButton *discardButton = msgBox.addButton("不保存", QMessageBox::DestructiveRole);
QPushButton *cancelButton = msgBox.addButton("取消", QMessageBox::RejectRole);

msgBox.setDefaultButton(saveButton);
msgBox.exec();

if (msgBox.clickedButton() == saveButton) {
saveDocument();
} else if (msgBox.clickedButton() == discardButton) {
// 放弃更改
} else {
// 取消操作
}

QFileDialog(文件对话框)

用于选择文件或目录。

#include <QFileDialog>

// 打开单个文件
QString fileName = QFileDialog::getOpenFileName(
this,
"选择文件",
QDir::homePath(),
"文本文件 (*.txt);;图片 (*.png *.jpg);;所有文件 (*.*)"
);

// 打开多个文件
QStringList files = QFileDialog::getOpenFileNames(
this,
"选择多个文件",
QDir::homePath(),
"图片 (*.png *.jpg *.bmp)"
);

// 保存文件
QString fileName = QFileDialog::getSaveFileName(
this,
"保存文件",
QDir::homePath() + "/untitled.txt",
"文本文件 (*.txt)"
);

// 选择目录
QString dir = QFileDialog::getExistingDirectory(
this,
"选择目录",
QDir::homePath(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
);

QInputDialog(输入对话框)

用于获取用户输入。

#include <QInputDialog>

// 文本输入
bool ok;
QString text = QInputDialog::getText(
this,
"输入名称",
"请输入您的姓名:",
QLineEdit::Normal,
"默认值",
&ok
);

// 整数输入
int value = QInputDialog::getInt(
this,
"输入年龄",
"请输入您的年龄:",
25, 0, 150, 1, &ok
);

// 下拉选择
QStringList items;
items << "红色" << "绿色" << "蓝色" << "黄色";
QString item = QInputDialog::getItem(
this, "选择颜色", "请选择喜欢的颜色:",
items, 0, false, &ok
);

QColorDialog(颜色对话框)

#include <QColorDialog>

QColor color = QColorDialog::getColor(Qt::red, this, "选择颜色");

if (color.isValid()) {
QString colorHex = color.name(); // #RRGGBB
}

QFontDialog(字体对话框)

#include <QFontDialog>

bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Microsoft YaHei", 12), this);

if (ok) {
textEdit->setFont(font);
}

创建自定义对话框

// 头文件
#ifndef USERDIALOG_H
#define USERDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QSpinBox>

class UserDialog : public QDialog
{
Q_OBJECT

public:
explicit UserDialog(QWidget *parent = nullptr);

QString name() const;
int age() const;
void setName(const QString &name);
void setAge(int age);

private:
QLineEdit *m_nameEdit;
QSpinBox *m_ageSpin;
};

#endif

// 实现文件
#include "userdialog.h"
#include <QFormLayout>
#include <QPushButton>
#include <QVBoxLayout>

UserDialog::UserDialog(QWidget *parent) : QDialog(parent)
{
setWindowTitle("用户信息");
setFixedSize(300, 150);

// 创建控件
m_nameEdit = new QLineEdit(this);
m_ageSpin = new QSpinBox(this);
m_ageSpin->setRange(0, 150);

// 按钮
QPushButton *okBtn = new QPushButton("确定", this);
QPushButton *cancelBtn = new QPushButton("取消", this);

connect(okBtn, &QPushButton::clicked, this, &QDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject);

// 布局
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow("姓名:", m_nameEdit);
formLayout->addRow("年龄:", m_ageSpin);

QHBoxLayout *btnLayout = new QHBoxLayout();
btnLayout->addStretch();
btnLayout->addWidget(okBtn);
btnLayout->addWidget(cancelBtn);

QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(formLayout);
mainLayout->addLayout(btnLayout);
}

QString UserDialog::name() const
{
return m_nameEdit->text();
}

int UserDialog::age() const
{
return m_ageSpin->value();
}

void UserDialog::setName(const QString &name)
{
m_nameEdit->setText(name);
}

void UserDialog::setAge(int age)
{
m_ageSpin->setValue(age);
}

使用自定义对话框

UserDialog dialog(this);
dialog.setName("张三");
dialog.setAge(25);

if (dialog.exec() == QDialog::Accepted) {
QString name = dialog.name();
int age = dialog.age();
qDebug() << "姓名:" << name << "年龄:" << age;
}

下一步

继续学习 主窗口,掌握菜单栏、工具栏、状态栏的创建和使用。