Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5707612
  • 博文数量: 409
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 8273
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-23 19:15
个人简介

qq:78080458 学习交流群:150633458

文章分类

全部博文(409)

文章存档

2019年(127)

2018年(130)

2016年(20)

2015年(60)

2014年(41)

2013年(31)

分类: 嵌入式

2015-11-07 21:12:48

一、QDialog类
1、对话框的概念
    对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对话框是应用程序和用户交互的平台。
    对话框是一个顶层窗口,不能嵌入到其他窗口中。
2、对话框的种类
    1)、模式对话框,该应用程序的其他窗口不能被访问,必须等待当前对话框消失,显示模式对话框一般调用它的exec()函数
    2)、非模式对话框,该应用程序的其他窗口还能继续被访问,显示非模式对话框一般调用它的show()函数
3、QDialog类的父类是QWidget





二、QDialog的派生类
为了方便开发人员的使用,Qt对一些特殊功能的对话框做了封装,提供一套标准的对话框。这些内建对话框提供静态函数便于使用,通常都是调用系统本地的对话框

1、QFileDialog

使用方法:
1、打开文件对话框,返回选择的文件名
    QString str = QFileDialog::getOpenFileName(
                                 父窗口, 
                                 对话框名字,
                                 默认选择路径,
                                 文件过滤器);
2、根据名字打开文件,成功返回true,失败返回false
    QFile file(str);
    file.open(QIODevice::ReadWrite);
3、得到一个输入流
    QTextStream in(&file);
4、逐行读出输入流
    in.readLine();


2
、QColorDialog

使用方法:
1、获取调色板
    QPalette palette = textEdit->palette();
2、打开颜色对话框,获取颜色
    QColor color = QColorDialog::getColor(
                                 palette.color(QPalette::Text), //对话框初始颜色
                                 this  //父窗口
                                    );
3、设置调色板颜色
    palette->setColor(
                                 QPalette::Text,  //要设置的调色板的部位
                                 color //要设置的颜色
                                );
4、加载调色板
    textEdit->setPalette(palette);

GUI为不同的部位分别设置了颜色标志


3、QFontDialog


使用方法:
1、打开字体对话框,获取字体
bool ok;
QFont font = QFontDialog::getFont(&ok);
如果点击对话框的“确定”按钮,那么ok的值就会变为true;如果点击对话框的“取消”按钮,那么ok的值就会变为false
2、设置字体
textEdit->setFont(font);

4、QInputDialog

使用方法:
打开输入对话框,输入的内容会返回
QString str = QInputDialog::getText(
  this,  //父窗口
  “inputDialog”,    //窗口标题
  “please input”,   //输入框上面的标签文字
  QLineEdit::Normal, //编辑框的显示方式
  QDir::home(),       //编辑框默认的内容
  ok                    //回填bool变量
)

5、QProgressDialog

QProgress::setRange(0,100)  //设置进度条范围
QProgress::setValue(50)     //设置进度条当前值

三、QMessageBox
    Qt提供了几种显示信息的消息框,这些消息框都是模态对话框,平时在软件里面会经常用到
1、QMessageBox::question
    一个具有标题和文本的消息询问框,开发人员可以根据具体需要定制按钮的个数和按钮的作用

2、QMessageBox::informat
    一个具有标题和提示文本的提示消息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用


3、QMessageBox::warning
    一个具有标题和文本信息的警示消息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用


4、QMessageBox::critical
    一个具有标题和文本信息的致命信息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用


5、QMessageBox::about
    一个具有标题和文本的消息框


6、QMessageBox::aboutQt
    显示关于Qt的消息框


7、消息按钮的制订



四、QDialog实例
1、头文件

点击(此处)折叠或打开

  1. #ifndef BUILDINDIALOG_H
  2. #define BUILDINDIALOG_H

  3. #include <QtGui>

  4. class buildInDialog : public QDialog
  5. {
  6.     Q_OBJECT
  7. public:
  8.     buildInDialog();
  9. private:
  10.     QPushButton *fileBtn;
  11.     QPushButton *colorBtn;
  12.     QPushButton *fontBtn;
  13.     QPushButton *saveBtn;
  14.     QPushButton *closeBtn;

  15.     QTextEdit *textEdit;
  16. private slots:
  17.     void fileSlot();
  18.     void colorSlot();
  19.     void fontSlot();
  20.     void saveSlot();
  21.     void closeSlot();

  22. };



  23. #endif
2、实现文件

点击(此处)折叠或打开

  1. #include "buildInDialog.h"

  2. buildInDialog::buildInDialog()
  3. {
  4.     fileBtn = new QPushButton("open");
  5.     colorBtn = new QPushButton("color");
  6.     fontBtn = new QPushButton("font");
  7.     saveBtn = new QPushButton("save");
  8.     closeBtn = new QPushButton("close");

  9.     textEdit = new QTextEdit();


  10.     //布局
  11.     QVBoxLayout *vLay = new QVBoxLayout();
  12.     QHBoxLayout *hLay = new QHBoxLayout();
  13.     vLay->addWidget(fileBtn);
  14.     vLay->addWidget(colorBtn);
  15.     vLay->addWidget(fontBtn);
  16.     vLay->addWidget(saveBtn);
  17.     vLay->addWidget(closeBtn);

  18.     hLay->addWidget(textEdit);
  19.     hLay->addLayout(vLay);

  20.     setLayout(hLay);

  21.     connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
  22.     connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
  23.     connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
  24.     connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
  25.     connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
  26. }

  27. void buildInDialog::fileSlot()
  28. {
  29.     //获取文件名字
  30.     QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");
  31.     
  32.     //打开文件
  33.     QFile file(str);
  34.     if(!file.open(QIODevice::ReadWrite))
  35.         return;
  36.     //得到输入流
  37.     QTextStream in(&file);
  38.     //读取数据
  39.     while(!in.atEnd())
  40.     {
  41.         QString st = in.readLine();
  42.         textEdit->append(st);
  43.     }
  44. }

  45. void buildInDialog::colorSlot()
  46. {
  47.     //获取条色板
  48.     QPalette palette = textEdit->palette();
  49.     //打开对话框,获取颜色
  50.     QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);

  51.     if(color.isValid())
  52.     {
  53.         //将颜色放到条色板
  54.         palette.setColor(QPalette::Window, color);
  55.         //加载调色板
  56.         textEdit->setPalette(palette);
  57.     }

  58. }

  59. void buildInDialog::fontSlot()
  60. {
  61.     bool ok;
  62.     QFont font = QFontDialog::getFont(&ok);
  63.     if(ok)
  64.         textEdit->setFont(font);
  65. }

  66. void buildInDialog::saveSlot()
  67. {
  68.     bool ok;
  69.     //获取输入的信息
  70.     QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);

  71.     //根据输入的名字打开文件
  72.     QFile file(str);
  73.     file.open(QIODevice::WriteOnly);
  74.     //获取输出流
  75.     QTextStream out(&file);
  76.     //将textEdit的内容写入到out
  77.     out<<textEdit->toPlainText()<<"\n";
  78. }

  79. void buildInDialog::closeSlot()
  80. {
  81.     QProgressDialog *progress = new QProgressDialog();
  82.     progress->setRange(0, 100);
  83.     for(int i=0; i<=100; i+=10)
  84.     {
  85.         qApp->processEvents();
  86.         progress->setValue(i);
  87.         sleep(1);
  88.     }
  89. }
3、主函数

点击(此处)折叠或打开

  1. #include "buildInDialog.h"
  2. #include <QApplication>

  3. int main(int argc, char *argv[])
  4. {
  5.     //设置编码,防止汉字出现乱码
  6.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
  7.     QApplication app(argc, argv);

  8.     buildInDialog dialog;
  9.     dialog.show();

  10.     return app.exec();
  11. }




阅读(9690) | 评论(0) | 转发(0) |
0

上一篇:lesson2-信号与槽机制

下一篇:lesson4-Qt窗口

给主人留下些什么吧!~~