基于QT5的文件讀取程序的實現(xiàn)
一、文件讀寫操作QFile
QT自帶了一個文件操作的類->QFile
,實驗中也是著重 QFile
的操作
1.1 頭文件
#include<QFile>
1.2 內(nèi)部函數(shù)
這些函數(shù)沒必要都去記住,我們只需要記住簡單的例如open()
、readLine()
、atEnd()
、close()
等常用的函數(shù)即可
- 首先我們
new
一個QFile
對象的時候有四種構(gòu)造方法,通常來說我們傳入 文件的路徑名 就好了 - 然后我們要調(diào)用
open()
函數(shù),這個函數(shù)是告訴操作系統(tǒng)我們通過什么樣的方式打開,例如只讀打開、只寫打開、可讀可寫打開……,這個和我們在C語言中的文件打開函數(shù)是類似的,我們在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 };
這些就是文件打開的一些模式了,可以根據(jù)自己的需求選用,我們這里既然是文件的讀取顯示操作,那么只需要讀取,于是我們的打開方式就是:QIODevice::ReadOnly
然后就是對這個文件從頭到尾讀取,在以前我們學(xué)的C語言中有一個文件結(jié)束標(biāo)志EOF
,一般這個EOF
是 − 1 -1 −1 但是這里的QFile
提供了一個函數(shù)atEnd()
如果當(dāng)我們讀到了文件末尾,那么就會返回一個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()
關(guān)閉數(shù)據(jù)流就好了
二、UI設(shè)計
這里隨便畫畫就好了,不過可以在文本顯示框插入背景圖,只需要在組件的styleSheet
中添加資源即可
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>文本內(nèi)容:</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函數(shù),熟悉匿名表達(dá)式 connect(ui->pushButton,&QPushButton::clicked,[=](){ //點擊按鈕,彈出文件選擇對話框 //使用對話框,獲取打開路徑,注意參數(shù)一是父類對象 ,參數(shù)二是對話窗口名稱 參數(shù)三是默認(rèn)打開路徑 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "C:\\data"); //使路徑顯示到路徑 line edit地方 ui->label->setText(fileName); //調(diào)試的跟蹤打印 qDebug()<<"文件路徑為:"+fileName; //使用Qfile操作文件 QFile file(fileName); //打開文件,注意參數(shù)的使用,文件修飾符,文件指針,可以和之前的嵌入式環(huán)境編程的知識聯(lián)系起來,包括 模式操作 file.open(QIODevice::ReadOnly); //使用數(shù)組數(shù)據(jù)結(jié)構(gòu)接讀取數(shù)據(jù) QByteArray array; while(!file.atEnd()) { array += file.readLine(); //按行讀 } ui->textEdit->setText(array); //關(guān)閉文件數(shù)據(jù)流 file.close(); //編碼格式類 //QTextCodec * codec = QTextCodec::codecForName("gbk"); QFileInfo info(fileName); qDebug() << "大?。? << info.size() << " 后綴名:" << info.suffix() << " 文件名稱:"<<info.fileName() << " 文件路徑:"<< info.filePath(); qDebug() << "創(chuàng)建日期:" << info.birthTime().toString("yyyy/MM/dd hh:mm:ss"); qDebug() << "最后修改日期:"<<info.lastModified().toString("yyyy-MM-dd hh:mm:ss"); //獲取文件名,之后,根據(jù)這個文件名找到指定文件,并打開 }); } MainWindow::~MainWindow() { delete ui; }
四、效果
我們可以看到我們的程序中將我們的日程表打開了,并且在終端打印了這個文件的一些信息,例如:路徑、文件名、大小等等
到此這篇關(guān)于基于QT5的文件讀取程序的實現(xiàn)的文章就介紹到這了,更多相關(guān)QT5 文件讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用C語言實現(xiàn)珠璣妙算Mastermind小游戲
這篇文章主要介紹了使用C語言實現(xiàn)珠璣妙算Mastermind小游戲,這是一款益智類多人游戲游戲,非常有趣,需要的朋友可以參考下2023-03-03Visual Studio 2019配置OpenCV4.1.1詳細(xì)圖解教程
這篇文章主要介紹了Visual Studio 2019配置OpenCV4.1.1詳細(xì)圖解教程 ,需要的朋友可以參考下2020-02-02C++?primer超詳細(xì)講解關(guān)聯(lián)容器
兩個主要的關(guān)聯(lián)容器為map和set,map中元素是一些關(guān)鍵字—值對,關(guān)鍵字起索引的作用,值則表示與索引相關(guān)聯(lián)的數(shù)據(jù)。set中每個元素只包含一個關(guān)鍵字,set支持高效的關(guān)鍵字查詢操作——檢查一個給定關(guān)鍵字是否在set中2022-07-07MFC創(chuàng)建模態(tài)對話框和非模態(tài)對話框的方法
這篇文章主要介紹了MFC創(chuàng)建模態(tài)對話框和非模態(tài)對話框的方法,需要的朋友可以參考下2014-07-07C/C++讀寫JSON數(shù)據(jù)的詳細(xì)過程記錄
JSON文件無論是在web開發(fā)、客戶端開發(fā)、服務(wù)端等開發(fā)中都是應(yīng)用比較廣泛的的第一種輕量級數(shù)據(jù)交換格式,非常方便閱讀和編寫,下面這篇文章主要給大家介紹了關(guān)于C/C++讀寫JSON數(shù)據(jù)的詳細(xì)過程,需要的朋友可以參考下2023-04-04