一、简介
Qt提供了一套标准的通用对话框,极大的提高了使用者的开发效率。本文收集了大部分标准的对话框,用户需要使用时只须复制相应的部分代码就行,不用再到处查找。
二、运行图
三、详解
1、中文翻译
(1)内置对话框的翻译
- #include <QTextCodec>
- #include <QLibraryInfo>
- #include <QLocale>
- #include <QTranslator>
- #include <QDebug>
- {
- QString translatorFileName = QLatin1String("qt_");
- translatorFileName += QLocale::system().name();
- QTranslator *translator = new QTranslator(&app);
- if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
- app.installTranslator(translator);
- }
- }
- {
- bool bFinded = false;
- QString str = "QTDIR=/usr/share/qt4";
- str = str.mid(6);
- bFinded = translator.load("qt_" + QLocale::system().name(), str.append("/translations/"));
- if(bFinded) {
- qApp->installTranslator(&translator);
- qDebug() << QObject::tr("支持中文的Qt国际化翻译文件!");
- }
- else {
- qDebug() << QObject::tr("没有支持中文的Qt国际化翻译文件!");
- }
- }
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); //Windows下
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); //Unix/Linux下
2、颜色对话框
- {
- QPalette palette = displayTextEdit->palette();
- const QColor& color = QColorDialog::getColor(palette.color(QPalette::Base), this);
- if(color.isValid()) {
- palette.setColor(QPalette::Base, color);
- displayTextEdit->setPalette(palette);
- }
- }
3、错误信息框
- {
- QErrorMessage box(this);
- box.setWindowTitle(tr("错误信息框"));
- box.showMessage(tr("错误信息框实例xx。"));
- box.showMessage(tr("错误信息框实例xx。"));
- box.showMessage(tr("错误信息框实例xx。"));
- box.showMessage(tr("错误信息框实例yy。"));
- box.showMessage(tr("错误信息框实例zz。"));
- box.exec();
- }
勾选“再次显示这个消息”,相同的消息会出现多次。若不勾选,则上述的“错误信息框实例xx。”只显示一次。
4、打开文件信息框
- {
- QString fileName = QFileDialog::getOpenFileName(this,
- tr("打开文件"),
- "/",
- tr("任何文件(*.*)"
- ";;文本文件(*.txt)"
- ";;C++文件(*.cpp)"));
- if (fileName.length() != 0) {
- displayTextEdit->setText(fileName);
- }
- }
QFileDialog提供了很多静态函数,用于获取用户选择的文件。getOpenFileName()函数在Windows和MacOS X平台上提供的是本地的对话框。当然也可以使用QFileDialog类,该类提供的始终是Qt自己绘制的对话框,而不都是调用系统资源API。具体的各个参数的含义可以查看Qt的帮助文档。
- {
- QFileDialog *fileDialog = new QFileDialog(this);
- fileDialog->setWindowTitle(tr("Open Image"));
- fileDialog->setDirectory(".");
- fileDialog->setFilter(tr("Source Files(*.cpp *.c)"));
- if(fileDialog->exec() == QDialog::Accepted) {
- QString path = fileDialog->selectedFiles()[0];
- displayTextEdit->setText(path);
- //QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path);
- } else {
- //QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
- }
- }
5、打开多个文件对话框
比上述打开文件对话框多了个打开多个对话框的功能,其他一样。
- void CBuiltinDlg::MultiOpenFile()
- {
- QStringList files = QFileDialog::getOpenFileNames(
- this, tr("Select one or more files to open"),
- "/",
- tr("All Files (*);;Text Files (*.txt)"));
- if (files.count()) {
- displayTextEdit->setText(QString("[%1]").arg(files.join(", ")));
- }
- }
6、保存文件对话框
与打开文件对话框类似。也提供本地的对话框和Qt自己绘制的对话框。
- void CBuiltinDlg::SaveFileName()
- {
- /*
- //getOpenFileName()函数在Windows和MacOS X平台上提供的是本地的对话框
- QFileDialog::Options options;
- QString selectedFilter;
- options |= QFileDialog::DontUseNativeDialog;
- QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
- displayTextEdit->toPlainText(),
- tr("file (*.cpp *.c *.txt)"),
- &selectedFilter,
- options);
- if (!fileName.isEmpty()) {
- //save fileName
- displayTextEdit->setText("save file:" + fileName);
- }
- */
- //QFileDialog提供的始终是Qt自己绘制的对话框
- QFileDialog* saveFileDialog = new QFileDialog(this);
- saveFileDialog->setWindowTitle(QString("Save File"));
- saveFileDialog->setFileMode(QFileDialog::AnyFile);
- saveFileDialog->setNameFilter(tr("file (*.cpp *.c *.txt)"));
- QString* fileName=new QString();
- if(saveFileDialog->exec())
- *fileName = saveFileDialog->getSaveFileName();
- if(fileName->size() > 0) {
- displayTextEdit->setText("save file:" + *fileName);
- }
- delete fileName;
- fileName = NULL;
- }
7、字体对话框
- {
- bool ok;
- const QFont& font = QFontDialog::getFont(&ok,
- displayTextEdit->font(),
- this,
- tr("字体对话框"));
- if(ok) {// 如果<确定>,设置字体.
- displayTextEdit->setFont(font);
- }
- }
8、页设置对话框
- {
- QPrinter printer;
- QPageSetupDialog dlg(&printer, this);
- dlg.setWindowTitle(tr("页设置对话框"));
- if (dlg.exec() == QDialog::Accepted) {
- // 进行下一步的处理。
- }
- }
9、选择文件夹对话框
可以通过options选项设置对话框显示的文件类型,与打开文件对话框类似。- void CBuiltinDlg::GettExistingDirectory()
- {
- QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
- options |= QFileDialog::DontUseNativeDialog;
- QString directory = QFileDialog::getExistingDirectory(this,
- tr("Open Directory"),
- "/home",
- options);
- if (!directory.isEmpty())
- displayTextEdit->setText(directory);
- }
10、进度对话框
显示文件复制或文件下载的进度条。
- {
- QProgressDialog progress(tr("正在复制文件..."), tr("取消"), 0, 1000, this);
- QFont font("ZYSong18030",10);
- progress.setFont(font);
- progress.setWindowModality(Qt::WindowModal);
- progress.setWindowTitle(tr("进度对话框"));
- progress.show();
- for (int i = 0; i < 1000; i++) {
- progress.setValue(i);
- qApp->processEvents();
- if (progress.wasCanceled())
- break;
- QTest::qWait(1);
- //... 复制文件处理。
- }
- progress.setValue(1000);
- }
11、打印对话框
- {
- QPrinter printer;
- QPrintDialog dlg(&printer, this);
- dlg.setWindowTitle(tr("打印对话框"));
- if (dlg.exec() == QDialog::Accepted) {
- // 进行下一步的处理。
- }
- }
12、输入对话框
输入对话框包括:字符串输入、下拉列表框输入、Int类型输入和Double类型输入。
- void CBuiltinDlg::slotName()
- {
- bool ok;
- QString name = QInputDialog::getText(this,
- tr("用户名"),
- tr("请输入新的用户名:"),
- QLineEdit::Normal,
- "name",
- &ok);
- if(ok && !name.isEmpty()) {
- displayTextEdit->setText(name);
- }
- }
- void CBuiltinDlg::slotSex()
- {
- QStringList list;
- list << tr("男") << tr("女");
- bool ok;
- QString sex = QInputDialog::getItem(this,tr("性别"),
- tr("请选择性别:"),list,0,false,&ok);
- if (ok) {
- displayTextEdit->setText(sex);
- }
- }
- void CBuiltinDlg::slotAge()
- {
- bool ok;
- int age = QInputDialog::getInteger(this,tr("年龄"),
- tr("请输入年龄:"), 20,0,150,1,&ok);
- if(ok) {
- displayTextEdit->setText(QString(tr("%1")).arg(age));
- }
- }
- void CBuiltinDlg::slotStature()
- {
- bool ok;
- double high = QInputDialog::getDouble(this,tr("身高"),
- tr("请输入身高:"),175.00,0,230.00,1,&ok);
- if(ok) {
- displayTextEdit->setText(QString(tr("%1")).arg(high));
- }
- }
13、消息对话框
消息对话框如下:- void CBuiltinDlg::slotQuestion()
- {
- switch(QMessageBox::question(this,tr("询问"),tr("重新开始?"),
- QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))
- {
- case QMessageBox::Ok:
- displayTextEdit->setText(tr("询问按钮 / 确定"));
- break;
- case QMessageBox::Cancel:
- displayTextEdit->setText(tr("询问按钮 / 取消"));
- break;
- default:
- break;
- }
- }
- void CBuiltinDlg::slotInformation()
- {
- QMessageBox::information(this,tr("提示"),tr("提示信息"));
- displayTextEdit->setText(tr("提示信息框"));
- }
- void CBuiltinDlg::slotWarning()
- {
- switch(QMessageBox::warning(this,tr("警告"),tr("是否要保存文档?"),
- QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save))
- {
- case QMessageBox::Save:
- displayTextEdit->setText(tr("警告对话框 / 保存"));
- break;
- case QMessageBox::Discard:
- displayTextEdit->setText(tr("警告对话框 / 不保存"));
- break;
- case QMessageBox::Cancel:
- displayTextEdit->setText(tr("警告对话框 / 取消"));
- break;
- default:
- break;
- }
- }
- void CBuiltinDlg::slotCritical()
- {
- QMessageBox::critical(this, tr("错误"),tr("告诉用户一个严重的错误!"));
- displayTextEdit->setText(tr("错误信息框"));
- }
- void CBuiltinDlg::slotAbout()
- {
- QMessageBox::about(this,tr("关于"),tr("对话框例子!"));
- displayTextEdit->setText(tr("关于信息框"));
- }
- void CBuiltinDlg::slotAboutQt()
- {
- QMessageBox::aboutQt(this, tr("关于Qt"));
- displayTextEdit->setText(tr("关于Qt信息框"));
- }
- void CBuiltinDlg::slotCustom()
- {
- QMessageBox customMsgBox;
- customMsgBox.setWindowTitle(tr("自定义弹出对话框"));
- QPushButton *lockButton = customMsgBox.addButton(tr("加锁"),QMessageBox::ActionRole);
- QPushButton *unlockButton = customMsgBox.addButton(tr("解锁"),QMessageBox::ActionRole);
- QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);
- customMsgBox.setText(tr("这是自定义对话框"));
- customMsgBox.exec();
- if(customMsgBox.clickedButton() == lockButton)
- displayTextEdit->setText(tr("自定义对话框 / 加锁"));
- if(customMsgBox.clickedButton() == unlockButton)
- displayTextEdit->setText(tr("自定义对话框 / 解锁"));
- if(customMsgBox.clickedButton() == cancelButton)
- displayTextEdit->setText(tr("自定义对话框 / 取消"));
- }
14、向导对话框
创建一个向导对话框,可以在页面上添加各种控件。
- void CBuiltinDlg::slotWizard()
- {
- QWizard wizard;
- wizard.addPage(createFirstPage());
- wizard.addPage(createSecondPage());
- wizard.setWindowTitle(tr("向导对话框"));
- wizard.exec();
- }
- QWizardPage* CBuiltinDlg::createFirstPage()
- {
- QWizardPage *page = new QWizardPage;
- page->setTitle(tr("简介"));
- QLabel *label = new QLabel(tr("第一页"));
- label->setWordWrap(true);
- QVBoxLayout *layout = new QVBoxLayout;
- layout->addWidget(label);
- page->setLayout(layout);
- return page;
- }
- QWizardPage* CBuiltinDlg::createSecondPage()
- {
- QWizardPage *page = new QWizardPage;
- page->setTitle(tr("正文"));
- QLabel *label = new QLabel(tr("第二页"));
- label->setWordWrap(true);
- QVBoxLayout *layout = new QVBoxLayout;
- layout->addWidget(label);
- page->setLayout(layout);
- return page;
- }
15、颜色控件框
- void CBuiltinDlg::slotColor(int index)
- {
- QStringList colorList = QColor::colorNames();
- QColor color = QColor(colorList[index]);
- QPalette p = displayTextEdit->palette();
- p.setColor(QPalette::Text, color);
- displayTextEdit->setPalette(p);
- }