1. 简介
QWizard向导对话框是一个拥有队列界面的特殊对话框,即其父类是QDialog,具有QDialog的特性。在此基础上,向导对话框封装成引导用户一步一步的完成预设流程的对话框。向导常用于安装界面向导、项目界面向导或卸载界面等一些规则性强的场景。
QWizard的每个子界面封装为QWizardPage类。
(1) 4中外观属性:ClassicStyle、ModernStyle、MacStyle和AeroStyle。
(2) 每个向导页都有:标题、子标题、左侧图片和右侧部件集合。
2.向导对话框的简单实现
1> 声明一个向导对话框变量
QWizard wizard;
2> 设置标题
wizard.setWindowTitle("新建向导");
3> 设置外观样式
wizard.setWizardStyle(QWizard::ModernStyle);
4> 设置按钮数量及按钮显示内容
wizard.setButtonText(QWizard::BackButton, "上一步");
wizard.setButtonText(QWizard::NextButton, "下一步");
wizard.setButtonText(QWizard::CancelButton, "取消");
wizard.setButtonText(QWizard::FinishButton, "下一步");
在此处可以设置标题为中文,根据需要显示按钮数量
5> 添加向导页
wizard.addPage(new MyWizardPage("new"));
wizard.addPage(new MyWizardPage("dat"));
此处使用工厂模式,将对话框页做了封装。对话框页左侧为图片,右侧为部件布局区。
this->setTitle("新建工程");
this->setPixmap(QWizard::WatermarkPixmap, QPixmap(":/prefix/images/guide.jpg"));
QLineEdit *line_edit_project_name = new QLineEdit("未命名");
line_edit_project_name->setFixedWidth(180);
line_edit_project_name->setValidator(new QRegularExpressionValidator(QRegularExpression("[^~\\\\/:*,.!?\"<>|。!?]*")));
this->registerField("projectName*", line_edit_project_name);
QHBoxLayout *project_name_layout = new QHBoxLayout;
project_name_layout->addWidget(line_edit_project_name);
project_name_layout->addWidget(new QLabel(".datw"));
QLineEdit *line_edit_project_local = new QLineEdit;
line_edit_project_local->setReadOnly(true);
this->registerField("projectLocal*", line_edit_project_local);
QPushButton *btn_local = new QPushButton("浏览...");
connect(btn_local, &QPushButton::clicked, [=](){
QString dir_path = QFileDialog::getExistingDirectory(this, "选择", QCoreApplication::applicationDirPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
QDir dir(dir_path);
QStringList files = dir.entryList(QStringList("*.datw"), QDir::Files | QDir::Readable, QDir::Name);
if (0 == files.count())
{
line_edit_project_local->setText(dir_path);
}
else
{
MyMessageBox::warn_mes(tr("该文件夹中已存在工程文件"));
}
});
QHBoxLayout *local_layout = new QHBoxLayout;
local_layout->addWidget(line_edit_project_local);
local_layout->addWidget(btn_local);
QVBoxLayout *page_layout = new QVBoxLayout;
page_layout->addWidget(new QLabel("工程名称"));
page_layout->addLayout(project_name_layout);
page_layout->addStretch(1);
page_layout->addWidget(new QLabel("工程位置"));
page_layout->addLayout(local_layout);
page_layout->addStretch(1);
this->setLayout(page_layout);
注意:this->registerField("projectName*", line_edit_project_name);,registerField中第一个参数指定*号时,对应的部件不允许为空,对向导的自由度起到一个很好的约束。
6> 向导对话框的显示
if (QDialog::Rejected == wizard.exec()) return;
以模态对话框的方式进行显示。