Qt对话框部分学习

来源:互联网 发布:中方网络花店 编辑:程序博客网 时间:2024/05/21 15:18
一、对话框部分常用内容
颜色对话框、文件对话框、字体对话框、输入对话框、消息对话框、进度对话框、错误对话框、向导对话框。

二、代码部分
  1. //widget.h
  2. #ifndef MYWIDGET_H
  3. #define MYWIDGET_H

  4. #include <QWidget>
  5. #include <QWizard>

  6. namespace Ui {
  7. class MyWidget;
  8. }

  9. class MyWidget : public QWidget
  10. {
  11.     Q_OBJECT

  12. public:
  13.     explicit MyWidget(QWidget *parent = 0);
  14.     ~MyWidget();

  15. private slots:
  16.     void on_pushButton_clicked();

  17.     void on_pushButton_4_clicked();

  18.     void on_pushButton_2_clicked();

  19.     void on_pushButton_5_clicked();

  20.     void on_pushButton_3_clicked();

  21.     void on_pushButton_6_clicked();

  22.     void on_pushButton_7_clicked();

  23.     void on_pushButton_8_clicked();

  24. private:
  25.     Ui::MyWidget *ui;
  26.     QWizardPage *createPage1();
  27.     QWizardPage *createPage2();
  28.     QWizardPage *createPage3();
  29. };

  30. #endif // MYWIDGET_H


  31. //widget.cpp
  32. #include "mywidget.h"
  33. #include "ui_mywidget.h"
  34. #include <QDebug>
  35. #include <QColorDialog>
  36. #include <QFileDialog>
  37. #include <QFontDialog>
  38. #include <QInputDialog>
  39. #include <QMessageBox>
  40. #include <QProgressDialog>
  41. #include <QErrorMessage>
  42. #include <QWizard>

  43. MyWidget::MyWidget(QWidget *parent) :
  44.     QWidget(parent),
  45.     ui(new Ui::MyWidget)
  46. {
  47.     ui->setupUi(this);
  48. }

  49. MyWidget::~MyWidget()
  50. {
  51.     delete ui;
  52. }

  53. //颜色对话框
  54. void MyWidget::on_pushButton_clicked()
  55. {
  56. // QColor color = QColorDialog::getColor(Qt::red, this, tr("颜色对话框"), QColorDialog::ShowAlphaChannel);
  57. // qDebug()<<"color: "<<color;
  58.     QColorDialog dialog(Qt::red, this);
  59.     dialog.setOption(QColorDialog::ShowAlphaChannel);
  60.     dialog.exec();
  61.     QColor color = dialog.currentColor();
  62.     qDebug()<<"color:"<<color;

  63. }

  64. //文本对话框
  65. void MyWidget::on_pushButton_4_clicked()
  66. {
  67.     QString filename = QFileDialog::getOpenFileName(this, tr("文件对话框"), "E:", tr("文本文件(*txt)"));
  68.     qDebug()<<"fileName:"<<filename;
  69. }

  70. //字体对话框
  71. void MyWidget::on_pushButton_2_clicked()
  72. {
  73.     bool ok;
  74.     QFont font = QFontDialog::getFont(&ok, this);
  75.     if(ok) ui->pushButton_2->setFont(font);
  76.     else qDebug()<<tr("没有选择字体!");
  77. }

  78. //输入对话框
  79. void MyWidget::on_pushButton_5_clicked()
  80. {
  81.     bool ok;
  82.     QString string = QInputDialog::getText(this, tr("输入字符串对话框"),
  83.                     tr("请输入用户名:"), QLineEdit::Normal, tr("admin"), &ok);
  84.     if(ok) qDebug()<<"string:"<<string;
  85.     //获取整数
  86.     int value1 = QInputDialog::getInt(this, tr("输入整数对话框"),
  87.                     tr("请输入-1000到1000之间的数值"), 100, -1000, 1000, 10, &ok);
  88.     if(ok) qDebug()<<"value1:"<<value1;
  89.     //获取浮点数
  90.     double value2 = QInputDialog::getDouble(this, tr("输入浮点数对话框"),
  91.                     tr("请输入-1000到1000之间的数值"), 0.00, -1000, 1000, 2, &ok);
  92.     if(ok) qDebug()<<"value2:"<<value2;
  93.     QStringList items;
  94.     items<<tr("条目1")<<tr("条目2");
  95.     //获取条目
  96.     QString item = QInputDialog::getItem(this, tr("输入条目对话框"),
  97.                     tr("请选择一个条目"), items, 0, true, &ok);
  98.     if(ok) qDebug()<<"item:"<<item;
  99. }

  100. //消息对话框
  101. void MyWidget::on_pushButton_3_clicked()
  102. {
  103.     //问题对话框
  104.     int ret1 = QMessageBox::question(this, tr("问题对话框"),
  105.                      tr("你了解Qt吗?"), QMessageBox::Yes, QMessageBox::No);
  106.     if(ret1 == QMessageBox::Yes) qDebug()<<tr("问题!");

  107.     //提示对话框
  108.     int ret2 = QMessageBox::information(this, tr("提示对话框"),
  109.                      tr("这是Qt书籍!"), QMessageBox::Ok);
  110.     if(ret2 == QMessageBox::Ok) qDebug()<<tr("提示!");

  111.     //警告对话框
  112.     int ret3 = QMessageBox::warning(this, tr("警告对话框"),
  113.                      tr("不能提前结束!"), QMessageBox::Abort);
  114.     if(ret3 == QMessageBox::Abort) qDebug()<<tr("警告!");

  115.     //错误对话框
  116.     int ret4 = QMessageBox::critical(this, tr("严重错误对话框"),
  117.                      tr("发现一个严重错误!现在要关闭所有文件!"), QMessageBox::YesAll);
  118.     if(ret4 == QMessageBox::YesAll) qDebug()<<tr("错误!");

  119.     //关于对话框
  120.     QMessageBox::about(this, tr("关于对话框"),
  121.                      tr("yafeilinux.com致力于Qt及Qt Creator的普及工作!"));
  122. }

  123. //进度对话框
  124. void MyWidget::on_pushButton_6_clicked()
  125. {
  126.     QProgressDialog dialog(tr("文件复制进度"), tr("取消"), 0, 50000, this);
  127.     dialog.setWindowTitle(tr("进度对话框"));
  128.     dialog.setWindowModality(Qt::WindowModal);
  129.     dialog.show();

  130.     for(int i=0;i<50000;i++) {
  131.         dialog.setValue(i);
  132.         QCoreApplication::processEvents();
  133.         if(dialog.wasCanceled()) break;
  134.     }

  135.     dialog.setValue(50000);
  136.     qDebug()<<tr("复制结束!");
  137. }

  138. //错误对话框
  139. void MyWidget::on_pushButton_7_clicked()
  140. {
  141.     QErrorMessage *dialog = new QErrorMessage(this);
  142.     dialog->setWindowTitle(tr("错误信息对话框"));
  143.     dialog->showMessage(tr("这里是出错信息!"));
  144. }

  145. QWizardPage *MyWidget::createPage1()
  146. {
  147.     QWizardPage *page = new QWizardPage;
  148.     page->setTitle(tr("介绍"));
  149.     return page;
  150. }

  151. QWizardPage *MyWidget::createPage2()
  152. {
  153.     QWizardPage *page = new QWizardPage;
  154.     page->setTitle(tr("用户选择信息"));
  155.     return page;
  156. }

  157. QWizardPage *MyWidget::createPage3()
  158. {
  159.     QWizardPage *page = new QWizardPage;
  160.     page->setTitle(tr("结束"));
  161.     return page;
  162. }

  163. //相对对话框
  164. void MyWidget::on_pushButton_8_clicked()
  165. {
  166.     QWizard wizard(this);
  167.     wizard.setWindowTitle(tr("向导对话框"));
  168.     wizard.addPage(createPage1());
  169.     wizard.addPage(createPage2());
  170.     wizard.addPage(createPage3());
  171.     wizard.exec();
  172. }

main.cpp:
  1. #include "mywidget.h"
  2. #include <QApplication>

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication a(argc, argv);
  6.     MyWidget w;
  7.     w.show();

  8.     return a.exec();
  9. }
ui样子


<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>
阅读(0) | 评论(0) | 转发(0) |
0

上一篇:Qt事件学习

下一篇:px4的CMakelists.txt阅读

相关热门文章
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • 彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~