Qt对话框部分学习

一、对话框部分常用内容
颜色对话框、文件对话框、字体对话框、输入对话框、消息对话框、进度对话框、错误对话框、向导对话框。

二、代码部分

 
  1      //widget.h
  2     #ifndef MYWIDGET_H
  3     #define MYWIDGET_H
  4 
  5     #include <QWidget>
  6     #include <QWizard>
  7 
  8     namespace Ui {
  9     class MyWidget;
 10     }
 11 
 12     class MyWidget : public QWidget
 13     {
 14         Q_OBJECT
 15 
 16     public:
 17         explicit MyWidget(QWidget *parent = 0);
 18         ~MyWidget();
 19 
 20     private slots:
 21         void on_pushButton_clicked();
 22 
 23         void on_pushButton_4_clicked();
 24 
 25         void on_pushButton_2_clicked();
 26 
 27         void on_pushButton_5_clicked();
 28 
 29         void on_pushButton_3_clicked();
 30 
 31         void on_pushButton_6_clicked();
 32 
 33         void on_pushButton_7_clicked();
 34 
 35         void on_pushButton_8_clicked();
 36 
 37     private:
 38         Ui::MyWidget *ui;
 39         QWizardPage *createPage1();
 40         QWizardPage *createPage2();
 41         QWizardPage *createPage3();
 42     };
 43 
 44     #endif // MYWIDGET_H
 45 
 46 
 47     //widget.cpp
 48     #include "mywidget.h"
 49     #include "ui_mywidget.h"
 50     #include <QDebug>
 51     #include <QColorDialog>
 52     #include <QFileDialog>
 53     #include <QFontDialog>
 54     #include <QInputDialog>
 55     #include <QMessageBox>
 56     #include <QProgressDialog>
 57     #include <QErrorMessage>
 58     #include <QWizard>
 59 
 60     MyWidget::MyWidget(QWidget *parent) :
 61         QWidget(parent),
 62         ui(new Ui::MyWidget)
 63     {
 64         ui->setupUi(this);
 65     }
 66 
 67     MyWidget::~MyWidget()
 68     {
 69         delete ui;
 70     }
 71 
 72     //颜色对话框
 73     void MyWidget::on_pushButton_clicked()
 74     {
 75     // QColor color = QColorDialog::getColor(Qt::red, this, tr("颜色对话框"), QColorDialog::ShowAlphaChannel);
 76     // qDebug()<<"color: "<<color;
 77         QColorDialog dialog(Qt::red, this);
 78         dialog.setOption(QColorDialog::ShowAlphaChannel);
 79         dialog.exec();
 80         QColor color = dialog.currentColor();
 81         qDebug()<<"color:"<<color;
 82 
 83     }
 84 
 85     //文本对话框
 86     void MyWidget::on_pushButton_4_clicked()
 87     {
 88         QString filename = QFileDialog::getOpenFileName(this, tr("文件对话框"), "E:", tr("文本文件(*txt)"));
 89         qDebug()<<"fileName:"<<filename;
 90     }
 91 
 92     //字体对话框
 93     void MyWidget::on_pushButton_2_clicked()
 94     {
 95         bool ok;
 96         QFont font = QFontDialog::getFont(&ok, this);
 97         if(ok) ui->pushButton_2->setFont(font);
 98         else qDebug()<<tr("没有选择字体!");
 99     }
100 
101     //输入对话框
102     void MyWidget::on_pushButton_5_clicked()
103     {
104         bool ok;
105         QString string = QInputDialog::getText(this, tr("输入字符串对话框"),
106                         tr("请输入用户名:"), QLineEdit::Normal, tr("admin"), &ok);
107         if(ok) qDebug()<<"string:"<<string;
108         //获取整数
109         int value1 = QInputDialog::getInt(this, tr("输入整数对话框"),
110                         tr("请输入-1000到1000之间的数值"), 100, -1000, 1000, 10, &ok);
111         if(ok) qDebug()<<"value1:"<<value1;
112         //获取浮点数
113         double value2 = QInputDialog::getDouble(this, tr("输入浮点数对话框"),
114                         tr("请输入-1000到1000之间的数值"), 0.00, -1000, 1000, 2, &ok);
115         if(ok) qDebug()<<"value2:"<<value2;
116         QStringList items;
117         items<<tr("条目1")<<tr("条目2");
118         //获取条目
119         QString item = QInputDialog::getItem(this, tr("输入条目对话框"),
120                         tr("请选择一个条目"), items, 0, true, &ok);
121         if(ok) qDebug()<<"item:"<<item;
122     }
123 
124     //消息对话框
125     void MyWidget::on_pushButton_3_clicked()
126     {
127         //问题对话框
128         int ret1 = QMessageBox::question(this, tr("问题对话框"),
129                          tr("你了解Qt吗?"), QMessageBox::Yes, QMessageBox::No);
130         if(ret1 == QMessageBox::Yes) qDebug()<<tr("问题!");
131 
132         //提示对话框
133         int ret2 = QMessageBox::information(this, tr("提示对话框"),
134                          tr("这是Qt书籍!"), QMessageBox::Ok);
135         if(ret2 == QMessageBox::Ok) qDebug()<<tr("提示!");
136 
137         //警告对话框
138         int ret3 = QMessageBox::warning(this, tr("警告对话框"),
139                          tr("不能提前结束!"), QMessageBox::Abort);
140         if(ret3 == QMessageBox::Abort) qDebug()<<tr("警告!");
141 
142         //错误对话框
143         int ret4 = QMessageBox::critical(this, tr("严重错误对话框"),
144                          tr("发现一个严重错误!现在要关闭所有文件!"), QMessageBox::YesAll);
145         if(ret4 == QMessageBox::YesAll) qDebug()<<tr("错误!");
146 
147         //关于对话框
148         QMessageBox::about(this, tr("关于对话框"),
149                          tr("yafeilinux.com致力于Qt及Qt Creator的普及工作!"));
150     }
151 
152     //进度对话框
153     void MyWidget::on_pushButton_6_clicked()
154     {
155         QProgressDialog dialog(tr("文件复制进度"), tr("取消"), 0, 50000, this);
156         dialog.setWindowTitle(tr("进度对话框"));
157         dialog.setWindowModality(Qt::WindowModal);
158         dialog.show();
159 
160         for(int i=0;i<50000;i++) {
161             dialog.setValue(i);
162             QCoreApplication::processEvents();
163             if(dialog.wasCanceled()) break;
164         }
165 
166         dialog.setValue(50000);
167         qDebug()<<tr("复制结束!");
168     }
169 
170     //错误对话框
171     void MyWidget::on_pushButton_7_clicked()
172     {
173         QErrorMessage *dialog = new QErrorMessage(this);
174         dialog->setWindowTitle(tr("错误信息对话框"));
175         dialog->showMessage(tr("这里是出错信息!"));
176     }
177 
178     QWizardPage *MyWidget::createPage1()
179     {
180         QWizardPage *page = new QWizardPage;
181         page->setTitle(tr("介绍"));
182         return page;
183     }
184 
185     QWizardPage *MyWidget::createPage2()
186     {
187         QWizardPage *page = new QWizardPage;
188         page->setTitle(tr("用户选择信息"));
189         return page;
190     }
191 
192     QWizardPage *MyWidget::createPage3()
193     {
194         QWizardPage *page = new QWizardPage;
195         page->setTitle(tr("结束"));
196         return page;
197     }
198 
199     //相对对话框
200     void MyWidget::on_pushButton_8_clicked()
201     {
202         QWizard wizard(this);
203         wizard.setWindowTitle(tr("向导对话框"));
204         wizard.addPage(createPage1());
205         wizard.addPage(createPage2());
206         wizard.addPage(createPage3());
207         wizard.exec();
208     }


main.cpp:

    #include "mywidget.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MyWidget w;
        w.show();

        return a.exec();
    }

 

 

ui样子


posted @ 2017-08-15 09:12  习惯就好233  阅读(177)  评论(0编辑  收藏  举报