lesson3-Qt对话框

简介: 一、QDialog类1、对话框的概念     对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对话框是应用程序和用户交互的平台。     对话框是一个顶层窗口,不能嵌入到其他窗口中。
一、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.     outtextEdit->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. }




相关文章
|
5月前
|
Linux iOS开发 MacOS
19 QT - 标准文件对话框
19 QT - 标准文件对话框
30 0
|
3月前
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
Qt6学习笔记五(自定义对话框、QMessageBox、QColorDialog、QFileDialog、QFontDialog)
40 0
|
5月前
15 QT - 对话框QDialog概述
15 QT - 对话框QDialog概述
20 0
|
2月前
|
存储
QT基础入门——QMainWindow与对话框QDialog(三)
QT基础入门——QMainWindow与对话框QDialog(三)
52 0
QT基础入门——QMainWindow与对话框QDialog(三)
|
8月前
QT5基本对话框
QFileDialog类的几个静态函数见上表,用户通过这些函数可以很方便地定制 自己的文件对话框。其中,getOpenFileName()函数返回用户选择的文件名。但是当 用户在选择文件时,如果选择“取消”(Cancel),则返回一个空串。在此仅详细说 明getOpenFileName()静态函数中各个参数的作用,其他文件对话框类中相关的静态函数 的参数有与其类似之处。
29 0
QT5基本对话框
|
4月前
|
数据安全/隐私保护
QT基础教程(对话框2)
QT基础教程(对话框2)
32 0
|
4月前
QT基础教程(对话框1)
QT基础教程(对话框1)
33 0
|
4月前
|
C++
C++ Qt开发:自定义Dialog对话框组件
在之前的文章中笔者已经为大家展示了默认`Dialog`组件的使用方法,虽然内置组件支持对数据的输入,但有时候我们需要一次性输入多个数据,此时如果之使用默认模态对话框似乎不太够用,此时我们需要自己创建一个自定义对话框,需要说明的是此类对话框也是一种窗体,所以可以在其上面放置任何通用组件,以实现更多复杂的开发需求。自定义对话框需要解决的问题是,如何让父窗体与子窗体进行数据交换,要实现数据的交换有两种方式,第一种方式是通过动态加载模态对话框,当用户点击确定后通过`GetValue()`来拿到数据,而第二种方式则是通过发送信号的方式将数据投递给父窗体,这两种方式都可以,读者可根据自身需求来选择不同的通
36 1
C++ Qt开发:自定义Dialog对话框组件
|
4月前
|
数据安全/隐私保护 C++ 开发者
C++ Qt开发:标准Dialog对话框组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍标准对话框`QInputDialog`、`QFileDialog `这两种对话框组件的常用方法及灵活运用。在 Qt 中,标准对话框提供了一些常见的用户交互界面,用于执行特定任务,例如获取用户输入、选择文件路径、显示消息等。这些对话框通常具有标准化的外观和行为,使得在不同的平台上能够保持一致性。在一般的开发过程中,标准对话框是开发者常用的工具之一。
50 1
C++ Qt开发:标准Dialog对话框组件
|
5月前
|
API
18 QT - 消息对话框
18 QT - 消息对话框
24 0

推荐镜像

更多