Qt学习日记六---QT功能类、对话框和文件操作

首先简单介绍一些使用QT开发手册的技巧
对于Qt而言,在实际的开发过程中,
1)开发者可能知道所要使用的类 ---- >帮助手册 --->索引 -->直接输入类名进行查找
2)开发者可能不知道所要使用的类,只知道开发需求文档 ----> 帮助 手册,按下图操作:

 

一、 QT中时间和日期

时间----QTime
日期----QDate
1.QTime
     
头文件:#include <QTime>
模块:在项目的pro文件中,查看是否包含模块:QT += core
        对于QTime对象,主要包括时 分 秒 毫秒,数字时间,一个主要的功能就是,用户可以自己
设置一个时间(时 分 秒 ),然后可以给这个时间来设置毫秒数,从而达到时间可以正常运行。
        创建时间的对象的方式有两种:
                构造函数----- 指定一个时间, 通过静态成员函数--- currentTime () --- 获得当前操作系统的本地时间
                ①直接获取操作系统的时间
                
[static] QTime QTime::currentTime() //获取当前的系统时间,就是此时的时间,不会自动往下走

                ②可以创建一个QTime对象,让后再指定该对象的时,分,秒

QTime(int h, int m, int s = 0, int ms = 0)
QTime time(0,0,0); //可以做为秒表的功能再通过增加毫秒的时间,增加到1000,秒针就会自动加1

       2. 简单案例:

        实现嵌入式产品的系统时间

        第一步:通过currentTime() ----->得到QTime对象

        第二步:将QTime时间更新UI(QLabel)-----> tostring()

 QString QTime::toString(const QString &format) const ‐‐‐将时间对象转换成字符串,并且在转换字符串时,可以指定字符串显示的格式
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,t1(0,0,0)
{
    ui->setupUi(this);

    //通过构造函数创建一个时间对象,并指定它的时分秒分别为0
    QTime t(0,0,0); //t --- 0 0 0  0

    //将时间对象转换成字符串并输出
    qDebug() << "time = " << t.toString("hh:mm:ss:zzz"); //通过手册上的格式,指定时间显示的格式

    //通过 QTime addSecs(int s) const 给时间对象增加指定的秒

    t = t.addSecs(5);

     qDebug() << "time = " << t.toString("hh-mm-ss-zzz");

     //通过 QTime addMSecs(int ms) const 给时间对象增加指定的毫秒

     t = t.addMSecs(200);
     qDebug() << "time = " << t.toString("hh:mm:ss:zzz");

     //通过静态成员函数

     t = QTime::currentTime(); //获取当前操作系统的时间
      qDebug() << "time = " << t.toString("hh:mm:ss:zzz");

      qDebug() << "time = " << t1.toString("hh:mm:ss:zzz");

}

 

 使用格式如下: "hh:mm:ss" 或者 "hh-mm-ss"

 3.QTime常用接口

        ①QString QTime::toString(QStringView format) const

                功能说明:将一个时间对象以指定格式转换成字符串

                指定格式:"hh:mm:ss" 或者 "hh-mm-ss“

        ②[static] QTime QTime::currentTime()

                功能:获取系统当前的时间

        ③

2、日期类QDate

       ①.功能:获取系统当前的日期

       ②.创建对象的方法:

                ①构造函数:QDate(int y, int m, int d)

                ②静态函数:currentDate()

        ③.功能接口函数:

                QString QDate::toString(const QString & format ) const --- 将日期转换成字符串

         指定格式:

                        

                         比如:"yyyy/MM/dd"

                ④.简单的小例子

                        

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QDate d(2021,6,8);

   qDebug() << d.toString("yyyy/MM/dd");

  QDate dd =  QDate::currentDate();

   qDebug() << dd.dayOfWeek(); //返回这周是这个月中的第几周

}

三 QT对话框类部件                  

        对于对话框的功能,在GUI图形界面开发过程,使用是非常多,那么Qt也提供了丰富的对话
框类
        The QDialog class is the base class of dialog windows, QDialog 是所有对话框的基类,对话框的框架类图如下:

        

        1. QColorDialog---颜色对话框

               ① 该类主要来获得一个颜色(The QColorDialog class provides a dialog widget for         specifying colors)
                通过该类的静态成员函数(The static getColor () function shows the dialog),打开的
        对话框来选择一个颜色。
                 头文件:#include<QColorDialog>
                

        

[static] QColor QColorDialog::getColor(const QColor &initial = Qt::white,QWidget *parent = nullptr, const QString &title = QString(),QColorDialog::ColorDialogOptions options = ColorDialogOptions())

                功能:得到一个颜色(QColor)

                参数说明:

                 参数一:const QColor &initial = Qt::white ---- 默认一个初始化的颜色,一般情况
下,默认即可 
               参数二:QWidget *parent = nullptr ---- 这个参数说明,跟对话框(QColorDialog)
之间关系,该对话框的父部件,设置为nullptr,则说明对话框没有父部件,对话框跟面 是没有关系两个都是独立存在,如果想要设置的话,通常设置为 this(通常为界面类对象)
                 参数三:const QString &title = QString() ---- 对话框的标题
                参数四:QColorDialog::ColorDialogOptions options = ColorDialogOptions() 对话框
的颜色选项,类似对话框皮肤的设置                 
                返回值: QColor ----- RGB
                               
                

         

                ②简单的案例:设置一个控件的背景颜色

                

void ColorWindow::on_colorSetButton_clicked()
{ 
    //[1]打开颜色对话框,并选择一个颜色
    QColor color = QColorDialog::getColor();
    //[2] 将颜色值分别获取它的rgb 
    int red = color.red(); 
    int green = color.green(); 
    int blue = color.blue(); 
    //[3] 将rgb设置给控件
    ui‐>testLabel‐>setStyleSheet(QString("background‐color: rgb(%1, %2, % 3);").arg(red) 
    .arg(green) 
    .arg(blue));
 }

        2.QFontDialog---字体对话框

                ①让用户来选择一个字体(The QFontDialog class provides a dialog widget for selecting
a font)
                头文件:#include<QFontDialog>

                函数原型:

[static] QFont QFontDialog::getFont(bool *ok, const QFont &initial,QWidget *parent = nullptr, const QString &title = QString(),QFontDialog::FontDialogOptions options = FontDialogOptions())
                功能:得到一个字体对象(QFont)
                参数说明:
                参数一:bool *ok ----> 对获得字体结果,成功返回true,否则,返回 false
                参数二:const QFont &initial ----> 初始化字体
                参数三:QWidget *parent = nullptr ----> 字体对话框的父部件,一般可以传this,或者不传
                参数四:const QString &title = QString() ----> 对话框标题
                参数五:QFontDialog::FontDialogOptions options = FontDialogOptions() --- 可选项,默认即可
                
                返回值:
                        QFont(字体)
                通过结合函数setFont来给一个控件设置一个字体
                
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
if (ok) 
{
    // font is set to the font the user selected 
} else {
    // the user canceled the dialog; font is set to the initial
    // value, in this case Times, 12
}

                ②一个简单的案例

        

void FontDialogWin::on_fontSetButton_clicked() 
{
    //[1] 打开对话框 
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok)
    { 
        qDebug() << "ok = " << ok << font.toString();
        //[2] 如果获得成功,则将font设置给指定的控件
        ui‐>label‐>setFont(font);
    }else {
        qDebug() << " get font failed";
    }
}

        3.QFileDialog---文件对话框  

                      

打开文件对话框,可以让用户选择一个文件或者目录(路径),并不能打开文件中的内容
(The QFileDialog class provides a dialog that allow users to select files or
directories)
头文件:#include <QFileDialog>

静态函数的原型
        函数功能:得到一个文件的路径
        参数说明:
        参数一: QWidget * parent = nullptr ----- 该对话框的父部件,一般传this
        参数二: const QString &caption = QString() --- 对话框的标题
        参数三:const QString &dir = QString() ---- 文件所在目录 QString()--- 字符串匿名对象
        参数四:const QString &filter = QString() --- 文件过滤器,指定显示指定格式文件
        参数五:QString *selectedFilter = nullptr ---- 过滤器
        参数六:QFileDialog::Options options = Options() --- 文件对话框的可选项
        返回值:QString ---- 文件的路径

                它经常结合QFile来进行读或者写文件

                

QString fileName = 
QFileDialog::getOpenFileName(this, tr("Open File"),/*tr()‐‐‐国标化标准*/
"/home", //绝对路径 
tr("Images (*.png *.xpm *.jpg)"));
如果要设置过滤器,类型之间使用";;",比如:
 "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
具体用法:
void FileDialogWin::on_browseButton_clicked()
{
    //[1]打开文件对话框
        QString filePath = QFileDialog::getOpenFileName(this,"打开文件",                 "D:/Qt/qt_gz2057_workspace/DAY4/cpp_demo6_qfiledialog",Text files (*.cpp *.h)");
    //[2] 将文件路径更新UI
        ui‐>fileEdit‐>setText(filePath); 
    //[3]根据文件路径打开此文件
}
指定的路径都是以Linux路径的标准来执行

        4.QFile--文件

                该类可以用来读写文件的数据(The QFile class provides an interface for reading from
and writing to files)
                头文件:#include <QFile>
                QFile操作文件流程:
               
                第一步:根据文件的路径,将文件转换成QFile对象 ----- > 创建一个文件对象 --- 构 造函数              
                   

                 第二步:打开文件---- open

                        

                 

                 第三步:访问文件 ---- read 或者 write

                 QByteArray QIODevice::readAll() ----> 一次性读取文件中所有的数 据,数据以
QByteArray(字节数组)
                

                 第四步:关闭文件 ---- close

                具体的实现代码:

                

void FileDialogWin::on_browseButton_clicked()
{
    //[1]打开文件对话框
    QString filePath = QFileDialog::getOpenFileName(this, "打开文件",                                                              "D:/Qt/qt_gz2057_workspace/DAY4/cpp_demo6_qfiledialog",
"Text files (*.cpp *.h)");

    //[2] 将文件路径更新UI
    ui‐> fileEdit‐> setText(filePath);

    //[3]根据文件路径打开此文件
    QFile file(filePath);

    //[4] 打开文件 ‐‐‐ open
    if (file.open(QIODevice::ReadOnly))
    {
        QByteArray array = file.readAll();
        qDebug() << "ARRAY= " << array;
    }
    file.close();
}
void SaveFileWin::on_saveButton_clicked()
{
    //[1] 打开另存为对话框,并手动设置文件的名字
    QString filename = QFileDialog::getSaveFileName(this);

    //[2] 创建文件和打开文件
    QFile file(filename);

    if (file.open(QIODevice::WriteOnly))

    {
        //[3] 获得UI控件上的内容,再将它写入到文件中
        QString data = ui‐> textEdit‐> toPlainText();
        //QString ‐‐‐‐> QByteArray ‐‐‐‐ QString::toUtf8() const : QByteArray
        file.write(data.toUtf8());
    }
    file.close();
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值