一、对话框部分常用内容
颜色对话框、文件对话框、字体对话框、输入对话框、消息对话框、进度对话框、错误对话框、向导对话框。
二、代码部分
main.cpp:
ui样子
阅读(0) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
颜色对话框、文件对话框、字体对话框、输入对话框、消息对话框、进度对话框、错误对话框、向导对话框。
二、代码部分
- //widget.h
- #ifndef MYWIDGET_H
- #define MYWIDGET_H
-
- #include <QWidget>
- #include <QWizard>
-
- namespace Ui {
- class MyWidget;
- }
-
- class MyWidget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit MyWidget(QWidget *parent = 0);
- ~MyWidget();
-
- private slots:
- void on_pushButton_clicked();
-
- void on_pushButton_4_clicked();
-
- void on_pushButton_2_clicked();
-
- void on_pushButton_5_clicked();
-
- void on_pushButton_3_clicked();
-
- void on_pushButton_6_clicked();
-
- void on_pushButton_7_clicked();
-
- void on_pushButton_8_clicked();
-
- private:
- Ui::MyWidget *ui;
- QWizardPage *createPage1();
- QWizardPage *createPage2();
- QWizardPage *createPage3();
- };
-
- #endif // MYWIDGET_H
-
-
- //widget.cpp
- #include "mywidget.h"
- #include "ui_mywidget.h"
- #include <QDebug>
- #include <QColorDialog>
- #include <QFileDialog>
- #include <QFontDialog>
- #include <QInputDialog>
- #include <QMessageBox>
- #include <QProgressDialog>
- #include <QErrorMessage>
- #include <QWizard>
-
- MyWidget::MyWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::MyWidget)
- {
- ui->setupUi(this);
- }
-
- MyWidget::~MyWidget()
- {
- delete ui;
- }
-
- //颜色对话框
- void MyWidget::on_pushButton_clicked()
- {
- // QColor color = QColorDialog::getColor(Qt::red, this, tr("颜色对话框"), QColorDialog::ShowAlphaChannel);
- // qDebug()<<"color: "<<color;
- QColorDialog dialog(Qt::red, this);
- dialog.setOption(QColorDialog::ShowAlphaChannel);
- dialog.exec();
- QColor color = dialog.currentColor();
- qDebug()<<"color:"<<color;
-
- }
-
- //文本对话框
- void MyWidget::on_pushButton_4_clicked()
- {
- QString filename = QFileDialog::getOpenFileName(this, tr("文件对话框"), "E:", tr("文本文件(*txt)"));
- qDebug()<<"fileName:"<<filename;
- }
-
- //字体对话框
- void MyWidget::on_pushButton_2_clicked()
- {
- bool ok;
- QFont font = QFontDialog::getFont(&ok, this);
- if(ok) ui->pushButton_2->setFont(font);
- else qDebug()<<tr("没有选择字体!");
- }
-
- //输入对话框
- void MyWidget::on_pushButton_5_clicked()
- {
- bool ok;
- QString string = QInputDialog::getText(this, tr("输入字符串对话框"),
- tr("请输入用户名:"), QLineEdit::Normal, tr("admin"), &ok);
- if(ok) qDebug()<<"string:"<<string;
- //获取整数
- int value1 = QInputDialog::getInt(this, tr("输入整数对话框"),
- tr("请输入-1000到1000之间的数值"), 100, -1000, 1000, 10, &ok);
- if(ok) qDebug()<<"value1:"<<value1;
- //获取浮点数
- double value2 = QInputDialog::getDouble(this, tr("输入浮点数对话框"),
- tr("请输入-1000到1000之间的数值"), 0.00, -1000, 1000, 2, &ok);
- if(ok) qDebug()<<"value2:"<<value2;
- QStringList items;
- items<<tr("条目1")<<tr("条目2");
- //获取条目
- QString item = QInputDialog::getItem(this, tr("输入条目对话框"),
- tr("请选择一个条目"), items, 0, true, &ok);
- if(ok) qDebug()<<"item:"<<item;
- }
-
- //消息对话框
- void MyWidget::on_pushButton_3_clicked()
- {
- //问题对话框
- int ret1 = QMessageBox::question(this, tr("问题对话框"),
- tr("你了解Qt吗?"), QMessageBox::Yes, QMessageBox::No);
- if(ret1 == QMessageBox::Yes) qDebug()<<tr("问题!");
-
- //提示对话框
- int ret2 = QMessageBox::information(this, tr("提示对话框"),
- tr("这是Qt书籍!"), QMessageBox::Ok);
- if(ret2 == QMessageBox::Ok) qDebug()<<tr("提示!");
-
- //警告对话框
- int ret3 = QMessageBox::warning(this, tr("警告对话框"),
- tr("不能提前结束!"), QMessageBox::Abort);
- if(ret3 == QMessageBox::Abort) qDebug()<<tr("警告!");
-
- //错误对话框
- int ret4 = QMessageBox::critical(this, tr("严重错误对话框"),
- tr("发现一个严重错误!现在要关闭所有文件!"), QMessageBox::YesAll);
- if(ret4 == QMessageBox::YesAll) qDebug()<<tr("错误!");
-
- //关于对话框
- QMessageBox::about(this, tr("关于对话框"),
- tr("yafeilinux.com致力于Qt及Qt Creator的普及工作!"));
- }
-
- //进度对话框
- void MyWidget::on_pushButton_6_clicked()
- {
- QProgressDialog dialog(tr("文件复制进度"), tr("取消"), 0, 50000, this);
- dialog.setWindowTitle(tr("进度对话框"));
- dialog.setWindowModality(Qt::WindowModal);
- dialog.show();
-
- for(int i=0;i<50000;i++) {
- dialog.setValue(i);
- QCoreApplication::processEvents();
- if(dialog.wasCanceled()) break;
- }
-
- dialog.setValue(50000);
- qDebug()<<tr("复制结束!");
- }
-
- //错误对话框
- void MyWidget::on_pushButton_7_clicked()
- {
- QErrorMessage *dialog = new QErrorMessage(this);
- dialog->setWindowTitle(tr("错误信息对话框"));
- dialog->showMessage(tr("这里是出错信息!"));
- }
-
- QWizardPage *MyWidget::createPage1()
- {
- QWizardPage *page = new QWizardPage;
- page->setTitle(tr("介绍"));
- return page;
- }
-
- QWizardPage *MyWidget::createPage2()
- {
- QWizardPage *page = new QWizardPage;
- page->setTitle(tr("用户选择信息"));
- return page;
- }
-
- QWizardPage *MyWidget::createPage3()
- {
- QWizardPage *page = new QWizardPage;
- page->setTitle(tr("结束"));
- return page;
- }
-
- //相对对话框
- void MyWidget::on_pushButton_8_clicked()
- {
- QWizard wizard(this);
- wizard.setWindowTitle(tr("向导对话框"));
- wizard.addPage(createPage1());
- wizard.addPage(createPage2());
- wizard.addPage(createPage3());
- wizard.exec();
- }
main.cpp:
- #include "mywidget.h"
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MyWidget w;
- w.show();
-
- return a.exec();
- }
![](/attachment/201708/8/29512885_15021700786nbm.bmp)
给主人留下些什么吧!~~
评论热议