目录
  • 一、文件读写操作QFile
    • 1.1 头文件
    • 1.2 内部函数
  • 二、UI设计
    • 三、代码
      • 3.1 mainwindow.h
      • 3.2 mainwindow.c
    • 四、效果

      一、文件读写操作QFile

      QT自带了一个文件操作的类->QFile ,实验中也是着重 QFile 的操作

      1.1 头文件

      #include<QFile>

      1.2 内部函数

      基于QT5的文件读取程序的实现

      这些函数没必要都去记住,我们只需要记住简单的例如open()readLine()atEnd()close() 等常用的函数即可

      • 首先我们new 一个 QFile 对象的时候有四种构造方法,通常来说我们传入 文件的路径名 就好了
      • 然后我们要调用open()函数,这个函数是告诉操作系统我们通过什么样的方式打开,例如只读打开、只写打开、可读可写打开……,这个和我们在C语言中的文件打开函数是类似的,我们在QIODevice看到一个枚举类型的 OpenModeFlag打开方式
      enum OpenModeFlag {
              NotOpen = 0x0000,
              ReadOnly = 0x0001,
              WriteOnly = 0x0002,
              ReadWrite = ReadOnly | WriteOnly,
              Append = 0x0004,
              Truncate = 0x0008,
              Text = 0x0010,
              Unbuffered = 0x0020,
              NewOnly = 0x0040,
              ExistingOnly = 0x0080
          };
      

      这些就是文件打开的一些模式了,可以根据自己的需求选用,我们这里既然是文件的读取显示操作,那么只需要读取,于是我们的打开方式就是:QIODevice::ReadOnly

      然后就是对这个文件从头到尾读取,在以前我们学的C语言中有一个文件结束标志EOF,一般这个EOF是 − 1 -1 −1 但是这里的QFile 提供了一个函数atEnd()如果当我们读到了文件末尾,那么就会返回一个true

      例如:

      QFile file("in.txt");
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;
      
      while (!file.atEnd()) {
          QByteArray line = file.readLine();
          process_line(line);
      }
      

      最后我们通过file.close()关闭数据流就好了

      二、UI设计

      这里随便画画就好了,不过可以在文本显示框插入背景图,只需要在组件的styleSheet中添加资源即可

      基于QT5的文件读取程序的实现

      基于QT5的文件读取程序的实现

      mainwindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>300</width>
          <height>500</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <property name="autoFillBackground">
         <bool>false</bool>
        </property>
        <property name="styleSheet">
         <string notr="true"/>
        </property>
        <widget class="QWidget" name="centralwidget">
         <property name="autoFillBackground">
          <bool>true</bool>
         </property>
         <property name="styleSheet">
          <string notr="true"/>
         </property>
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QWidget" name="widget" native="true">
            <layout class="QHBoxLayout" name="horizontalLayout">
             <item>
              <widget class="QLabel" name="label">
               <property name="text">
                <string>文件路径</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="pushButton">
               <property name="text">
                <string>打开文件</string>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
          <item>
           <widget class="QGroupBox" name="groupBox">
            <property name="styleSheet">
             <string notr="true"/>
            </property>
            <property name="title">
             <string>文本内容:</string>
            </property>
            <layout class="QHBoxLayout" name="horizontalLayout_2">
             <item>
              <widget class="QTextEdit" name="textEdit">
               <property name="styleSheet">
                <string notr="true">background-image: url(:/a/tmp/back.png);
      background-color: rgba(0, 0, 0, 12);</string>
               </property>
              </widget>
             </item>
            </layout>
           </widget>
          </item>
         </layout>
        </widget>
        <widget class="QMenuBar" name="menubar">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>0</y>
           <width>300</width>
           <height>23</height>
          </rect>
         </property>
        </widget>
        <widget class="QStatusBar" name="statusbar"/>
       </widget>
       <resources/>
       <connections/>
      </ui>
      
      

      三、代码

      3.1 mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
      };
      #endif // MAINWINDOW_H
      
      

      3.2 mainwindow.c

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QFile>
      #include <QFileDialog>
      #include <QDebug>
      #include <QPushButton>
      #include <QTextStream>
      #include <QFileInfo>
      #include <QDateTime>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          //使用connec函数,熟悉匿名表达式
          connect(ui->pushButton,&QPushButton::clicked,[=](){
             //点击按钮,弹出文件选择对话框
             //使用对话框,获取打开路径,注意参数一是父类对象 ,参数二是对话窗口名称 参数三是默认打开路径
             QString fileName = QFileDialog::getOpenFileName(this,
                    tr("Open File"), "C:\\data");
             //使路径显示到路径 line edit地方
             ui->label->setText(fileName);
             //调试的跟踪打印
             qDebug()<<"文件路径为:"+fileName;
             //使用Qfile操作文件
             QFile file(fileName);
             //打开文件,注意参数的使用,文件修饰符,文件指针,可以和之前的嵌入式环境编程的知识联系起来,包括 模式操作
             file.open(QIODevice::ReadOnly);
             //使用数组数据结构接读取数据
      
             QByteArray array;
             while(!file.atEnd())
             {
                 array += file.readLine(); //按行读
             }
             ui->textEdit->setText(array);
             //关闭文件数据流
             file.close();
             //编码格式类
             //QTextCodec * codec = QTextCodec::codecForName("gbk");
             QFileInfo info(fileName);
             qDebug() << "大小:" << info.size() << " 后缀名:" << info.suffix() << " 文件名称:"<<info.fileName() << " 文件路径:"<< info.filePath();
             qDebug() << "创建日期:" << info.birthTime().toString("yyyy/MM/dd hh:mm:ss");
             qDebug() << "最后修改日期:"<<info.lastModified().toString("yyyy-MM-dd hh:mm:ss");
             //获取文件名,之后,根据这个文件名找到指定文件,并打开
         });
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      

      四、效果

      基于QT5的文件读取程序的实现

      我们可以看到我们的程序中将我们的日程表打开了,并且在终端打印了这个文件的一些信息,例如:路径、文件名、大小等等

      声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。