当前位置:网站首页>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 .
边栏推荐
- OSPF - detailed explanation of stub area and full stub area
- 我眼中真正优秀的CTO长啥样
- Integrated easy to pay secondary domain name distribution system
- Dynamic planning -- expansion topics
- The more you talk, the more your stupidity will be exposed.
- Free sharing | linefriends hand account inner page | horizontal grid | not for sale
- Floating source code comment (38) parallel job processor
- Basic principle of LSM tree
- Flutter网络和数据存储框架搭建 -b1
- Nous avons fait une plateforme intelligente de règlement de détail
猜你喜欢
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
[leetcode] [SQL] notes
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
my. INI file not found
ActiveMQ的基础
Thesis study - 7 Very Deep Convolutional Networks for Large-Scale Image Recognition (3/3)
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
随机推荐
Using the visualization results, click to appear the corresponding sentence
PyTorch中在反向传播前为什么要手动将梯度清零?
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
235. 二叉搜索树的最近公共祖先【lca模板 + 找路径相同】
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
Which do MySQL and Oracle learn?
【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
HOW TO WRITE A DAILY LAB NOTE?
01. Preparation for automated office (free guidance, only three steps)
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
Comments on flowable source code (37) asynchronous job processor
Briefly describe the quantitative analysis system of services
These problems should be paid attention to in the production of enterprise promotional videos
How to design a high concurrency system
Luogu-p1107 [bjwc2008] Lei Tao's kitten
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
High concurrency Architecture - separate databases and tables
The earliest record
Smart wax therapy machine based on STM32 and smart cloud
Basic principle of LSM tree