首页 > 代码库 > C/C++ -- Gui编程 -- Qt库的使用 -- 标准对话框

C/C++ -- Gui编程 -- Qt库的使用 -- 标准对话框

-----mywidget.cpp-----

  1 #include "mywidget.h"
  2 #include "ui_mywidget.h"
  3 #include <QFileDialog>
  4 #include <QColorDialog>
  5 #include <QFontDialog>
  6 #include <QInputDialog>
  7 #include <QMessageBox>
  8 #include <QProgressDialog>
  9 #include <QErrorMessage>
 10 #include <QDebug>
 11 
 12 MyWidget::MyWidget(QWidget *parent) :
 13     QWidget(parent),
 14     ui(new Ui::MyWidget)
 15 {
 16     ui->setupUi(this);
 17 }
 18 
 19 MyWidget::~MyWidget()
 20 {
 21     delete ui;
 22 }
 23 
 24 void MyWidget::on_pushButton_clicked()
 25 {
 26     QStringList filenames =  27             QFileDialog::getOpenFileNames( 28                 this, "文件对话框", "F:", 29                 "图片(*png);;音乐(*mp3 *wma)");
 30     qDebug()<<"FileNames"<<filenames<<endl;
 31 }
 32 
 33 void MyWidget::on_pushButton_3_clicked()
 34 {
 35     QColor color = QColorDialog::getColor(Qt::red, this, "颜色选择",  36                                           QColorDialog::ShowAlphaChannel);
 37     qDebug()<<color<<endl;
 38 }
 39 
 40 void MyWidget::on_pushButton_2_clicked()
 41 {
 42     bool ok;
 43     QFont font = QFontDialog::getFont(&ok, this);
 44     if(not ok)
 45         qDebug()<<"没有选择字体"<<endl;
 46     else
 47         qDebug()<<"字体:"<<font<<endl;
 48 }
 49 
 50 void MyWidget::on_pushButton_4_clicked()
 51 {
 52     bool ok;
 53     QString string = QInputDialog::getText(this, "字符串", "请输入字符串",QLineEdit::Normal, "admin", &ok);
 54     if(ok) qDebug()<<"输入了字符串"<<string<<endl;
 55     int value = http://www.mamicode.com/QInputDialog::getInt(this, "数字", "请输入整数", 0, -99, 99, 1, &ok);
 56     if(ok) qDebug()<<"输入了整数"<<value<<endl;
 57     double d = QInputDialog::getDouble(this, "浮点数", "请输入浮点数", 0.00, -99.99, +99.99, 2, &ok);
 58     if(ok) qDebug()<<"输入了浮点数"<<d<<endl;
 59     QStringList items;
 60     items<<"条目1"<<"条目2";
 61     QString item = QInputDialog::getItem(this, "条目", "请选择一个条目", items, 0, true, &ok);
 62     if(ok)qDebug()<<"选择了条目"<<item;
 63 }
 64 
 65 void MyWidget::on_pushButton_5_clicked()
 66 {
 67     int ret = QMessageBox::question(this, "列位看官", "你道此书从何而来?", QMessageBox::Yes, QMessageBox::No);
 68     if(ret == QMessageBox::Yes)
 69         qDebug()<<"说起根由虽近荒唐,细按则深有趣味"<<endl;
 70     ret = QMessageBox::information(this, "一场幽梦同谁近", "千古情人独我痴", QMessageBox::Ok);
 71     if(ret == QMessageBox::Ok)
 72         qDebug()<<"谋事在人,成事在天"<<endl;
 73     ret = QMessageBox::warning(this,"《四部丛刊》", "《经进东坡文集事略》 ", QMessageBox::Abort);
 74     if(ret == QMessageBox::Abort)
 75         qDebug()<<"天道不言而品物亨、岁功成"<<endl;
 76     ret = QMessageBox::critical(this, "致虚极,守静笃", "万物并作,吾以观复", QMessageBox::YesAll);
 77     if(ret == QMessageBox::YesAll)
 78         qDebug()<<"明月皎皎照我床,星汉西流夜未央"<<endl;
 79     QMessageBox::about(this, "不出户,知天下;不窥牖,见天道。",  80                        "其出弥远,其知弥少。是以圣人不行而知,不见而明,不为而成");
 81     QMessageBox::aboutQt(this);
 82 }
 83 
 84 void MyWidget::on_pushButton_6_clicked()
 85 {
 86     QProgressDialog dlg("数据恢复进度", "取消", 0, 50000, this);
 87     dlg.setWindowTitle("RECUVA");
 88     dlg.setWindowModality(Qt::WindowModal);
 89     dlg.show();
 90     for(int i=0; i<50000; i++)
 91     {
 92         dlg.setValue(i);
 93         QCoreApplication::processEvents();
 94         if(dlg.wasCanceled())
 95             break;
 96     }
 97     dlg.setValue(50000);
 98     qDebug()<<"恢复结束"<<endl;
 99 }
100 
101 void MyWidget::on_pushButton_7_clicked()
102 {
103     QErrorMessage *dlg = new QErrorMessage(this);
104     dlg->setWindowTitle("这不是你的错");
105     dlg->showMessage("卡莱尔轻声地安慰我说");
106 }
107 
108 
109 QWizardPage* MyWidget::createPage1()
110 {
111     QWizardPage * page = new QWizardPage;
112     page->setTitle("欢迎进入Windows卸载向导");
113     return page;
114 }
115 QWizardPage* MyWidget::createPage2()
116 {
117     QWizardPage * page = new QWizardPage;
118     page->setTitle("你真的要卸载吗?");
119     return page;
120 }
121 QWizardPage* MyWidget::createPage3()
122 {
123     QWizardPage * page = new QWizardPage;
124     page->setTitle("卸载已完成");
125     return page;
126 }
127 
128 void MyWidget::on_pushButton_8_clicked()
129 {
130     QWizard wizard(this);
131     wizard.setWindowTitle("Win7卸载向导");
132     wizard.addPage(createPage1());
133     wizard.addPage(createPage2());
134     wizard.addPage(createPage3());
135     wizard.exec();
136 }