c/c++语言开发共享基于QT5的文件读取程序的实现

一、文件读写操作qfileqt自带了一个文件操作的类->qfile ,实验中也是着重 qfile 的操作1.1 头文件#include<qfile>1.2 内部函数这些函数没必要都去

一、文件读写操作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的文件读取程序的实现

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

到此这篇关于基于qt5的文件读取程序的实现的文章就介绍到这了,更多相关qt5 文件读取内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享基于QT5的文件读取程序的实现,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1246791.html

(0)
上一篇 2022年9月23日
下一篇 2022年9月23日

精彩推荐