当前位置:网站首页>QT -- qfileinfo file information reading
QT -- qfileinfo file information reading
2022-07-03 19:15:00 【xuechanba】
QFileInfo File information read
QFileInfo Class can read important file information
1、 Returns the suffix of the file 
2、 Return the file name , Exclude path 
3、 Return file path , Do not include filename 
4、 Return the filename , Including paths ( It can be an absolute path or a relative path )
Use QFileInfo Class needs to contain header files #include <QFileInfo>
The code is as follows :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QFileInfo>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Hit the new button , A file dialog box pops up
connect(ui->pushButton,&QPushButton::released,[=](){
// File dialog -- Return the file path of the selected file
QString path = QFileDialog::getOpenFileName(this," Select file ","/home/chantui/work/resourceFile","Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
// Use file information class QFileInfo
// Incoming file path
QFileInfo fileinfo(path);
//1、 The suffix of the output file
qDebug()<<" The suffix of the file :"<<fileinfo.suffix();
//2、 Return the file name , Exclude path
qDebug()<<" File name , Exclude path :"<<fileinfo.fileName();
//3、 Return file path , Do not include filename
qDebug()<<" File path , Do not include filename :"<<fileinfo.path();
//4、 Return the filename , Including paths ( It can be an absolute path or a relative path )
qDebug()<<" file name , Including paths ( It can be an absolute path or a relative path ):"<<fileinfo.filePath();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
The operation results are as follows :
5、 Return file size 
6、 Returns the file creation time 、 Last modified time .
File creation time :birthTime() (Windows Available under the system ,Linux Not available under the system )
Last modified time of file :lastModified()
We need to pay attention to , When you need to return the relevant time information of the file , You need to pay attention to the return value . You also need to include the header file #include <QDateTime>
When used directly qDebug() To output ,
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QFileInfo>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Hit the new button , A file dialog box pops up
connect(ui->pushButton,&QPushButton::released,[=](){
// File dialog -- Return the file path of the selected file
QString path = QFileDialog::getOpenFileName(this," Select file ","/home/chantui/work/resourceFile","Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
// Use file information class QFileInfo
// Incoming file path
QFileInfo fileinfo(path);
//1、 Size of output file
qDebug()<<" Size of output file :"<<fileinfo.size();
//2、 Return the creation date of the file
qDebug()<<" Date the file was created :"<<fileinfo.birthTime();
//3、 Returns the last modified date of the file
qDebug()<<" Date of last modification of the document :"<<fileinfo.lastModified();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
The output is as follows :
I don't know why , The printed creation date is invalid .
If I want to output in the date format I specify , So how do you do that ? View help documents , We can use toString() This function .
How to set the format ? Look below 

The code is as follows ,
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QFileInfo>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Hit the new button , A file dialog box pops up
connect(ui->pushButton,&QPushButton::released,[=](){
// File dialog -- Return the file path of the selected file
QString path = QFileDialog::getOpenFileName(this," Select file ","/home/chantui/work/resourceFile","Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
// Use file information class QFileInfo
// Incoming file path
QFileInfo fileinfo(path);
//1、 Size of output file , Unit is byte
qDebug()<<" Size of output file , Unit is byte :"<<fileinfo.size();
//2、 Return the creation date of the file
qDebug()<<" Date the file was created :"<<fileinfo.birthTime().toString("yyyy/MM/dd hh:mm:ss");
//3、 Returns the last modified date of the file
qDebug()<<" Date of last modification of the document :"<<fileinfo.lastModified().toString("yyyy/MM/dd hh:mm:ss");
});
}
MainWindow::~MainWindow()
{
delete ui;
}
The operation results are as follows :
stay ubuntu In the system , Using functions birthTime() To get the creation time of the file always returns invalid .
stay ubuntu In the system , Click on the file -> attribute , Nor can we find the creation time , You can only view the modification and access time .
Conclusion :birthTime() stay Windows You can use , stay Linux In the system , The file has no information about the creation time ,( Generally, there are only the latest modification time and the latest access time , Corresponding function respectively lastModified and lastRead )
And it was said before ,created The function has been discarded , Don't use it in code . In future projects , When we're in Linux When you want to view information about file time in the system , Don't check its creation time , Only check its latest modification time and latest access time .
边栏推荐
- Simulation scheduling problem of SystemVerilog (1)
- Understanding of database architecture
- Today I am filled with emotion
- 我眼中真正优秀的CTO长啥样
- 235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
- Random numbers in a long range, is that right- Random number in long range, is this the way?
- Web3 credential network project galaxy is better than nym?
- 【光学】基于matlab介电常数计算【含Matlab源码 1926期】
- flask 生成swagger文档
- 记录在模拟器中运行flutter时报的错
猜你喜欢

SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)

Help change the socket position of PCB part

Basic principle of LSM tree

Pan for in-depth understanding of the attention mechanism in CV
![[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)](/img/4e/a51365bb88b1fc29d1c77fcdde5350.jpg)
[new year job hopping season] test the technical summary of interviewers' favorite questions (with video tutorials and interview questions)

KINGS

Merge K ascending linked lists

Record the errors reported when running fluent in the simulator

Flutter network and data storage framework construction-b1

Php based campus lost and found platform (automatic matching push)
随机推荐
Bad mentality leads to different results
The more you talk, the more your stupidity will be exposed.
Does SQL always report foreign key errors when creating tables?
Common PostgreSQL commands
SQL: special update operation
math_ Taylor formula
Why should we do feature normalization / standardization?
Which do MySQL and Oracle learn?
flask 生成swagger文档
Differential constrained SPFA
EGO Planner代码解析bspline_optimizer部分(3)
These problems should be paid attention to in the production of enterprise promotional videos
Nous avons fait une plateforme intelligente de règlement de détail
Understanding of database architecture
Luogu-p1107 [bjwc2008] Lei Tao's kitten
Ego planner code parsing Bspline_ Optimizer section (1)
OSPF - detailed explanation of stub area and full stub area
DriveSeg:动态驾驶场景分割数据集
Find the median of two positive arrays
SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)