Qt学习之路之获取文件信息

QFileInfo类提供了对文件进行操作时获取的文件信息,包括文件名, 文件大小(字节), 文件创建时间,

文件最后修改时间,文件最近访问时间以及一些文件是否为目录,文件,或者符号连接,和读写等属性。

不废话了,先看看代码~~~~

fileInfo_widget.h

#ifndef FILEWIDGET_H
#define FILEWIDGET_H

#include 
#include 
#include 	//---j解决中文乱码
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class FileInfoWidget : public QWidget
{
	Q_OBJECT
public:
	FileInfoWidget(QWidget *parent=0);
	 
	public slots:
		void slotFile();		//-----通过标准文件对话框获取文件名功能
		void slotGetInfo();	//-----获取文件的具体信息
private:
	QLabel *fileNameLabel;
	QLineEdit *fileNameEdit;
	QPushButton *openFileBtn;
	QPushButton *GetFileInfoBtn;

	QLabel *sizeLabel;							//---大小
	QLineEdit *sizeLabelEdit;

	QLabel *buildTimeLabel;					//--创建时间
	QLineEdit *buildTimeEdit;

	QLabel *lastModifiedTimeLabel;		//---最近修改时间
	QLineEdit *lastModifiedEdit;

	QLabel *lastVisitLabel;					//--最后访问时间
	QLineEdit *lastVisitEidt;

	QLabel *propertyLabel;						//--属性
	QCheckBox *isDirCheckBox;				//--目录
	QCheckBox *isFileCheckBox;				//--文件
	QCheckBox *isSymbolLinkCheckBox;	//--符号连接
	QCheckBox *isHiddenCheckBox;			//--隐藏
	QCheckBox *isReadableCheckBox;		//--读
	QCheckBox *isWriteableCheckBox;		//--写
	QCheckBox *isExecutableCheckBox;		//-执行
};

#endif	//FILEWIDGET_H


fileInfo_widget.cpp

#include "fileInfo_widget.h"

FileInfoWidget::FileInfoWidget(QWidget *parent/*=0*/):QWidget(parent)
{
	//----Qt5解决中文乱码
	QTextCodec *codec = QTextCodec::codecForName("GB18030");

	//-----------------------------------
	fileNameLabel = new QLabel(codec->toUnicode("文件名"));
	fileNameEdit = new QLineEdit();
	openFileBtn = new QPushButton(codec->toUnicode("文件"));

	sizeLabel = new QLabel(codec->toUnicode("大小"));
	sizeLabelEdit = new QLineEdit();

	buildTimeLabel = new QLabel(codec->toUnicode("创建时间"));
	buildTimeEdit = new QLineEdit();

	lastModifiedTimeLabel = new QLabel(codec->toUnicode("最后修改时间"));
	lastModifiedEdit = new QLineEdit();

	lastVisitLabel = new QLabel(codec->toUnicode("最近访问时间"));
	lastVisitEidt = new QLineEdit();

	propertyLabel = new QLabel(codec->toUnicode("属性"));

	isDirCheckBox = new QCheckBox(codec->toUnicode("目录"));
	isFileCheckBox = new QCheckBox(codec->toUnicode("文件"));
	isSymbolLinkCheckBox = new QCheckBox(codec->toUnicode("符号连接"));
	isHiddenCheckBox = new QCheckBox(codec->toUnicode("隐藏"));
	isReadableCheckBox = new QCheckBox(codec->toUnicode("读"));
	isWriteableCheckBox = new QCheckBox(codec->toUnicode("写"));
	isExecutableCheckBox = new QCheckBox(codec->toUnicode("执行"));

	GetFileInfoBtn = new QPushButton(codec->toUnicode("获取文件信息"));

	//------布局控件
	QGridLayout *gridLayout = new QGridLayout();
	gridLayout->addWidget(fileNameLabel, 0, 0);
	gridLayout->addWidget(fileNameEdit, 0, 1);
	gridLayout->addWidget(openFileBtn, 0, 2);

	gridLayout->addWidget(sizeLabel, 1, 0);
	gridLayout->addWidget(sizeLabelEdit, 1, 1, 1, 2);

	gridLayout->addWidget(buildTimeLabel,  2, 0);
	gridLayout->addWidget(buildTimeEdit, 2, 1, 1, 2);

	gridLayout->addWidget(lastVisitLabel, 3, 0);
	gridLayout->addWidget(lastVisitEidt, 3, 1, 1, 2);

	gridLayout->addWidget(lastVisitLabel, 4, 0);
	gridLayout->addWidget(lastVisitEidt, 4, 1, 1, 2);

	QHBoxLayout *hLayout1 = new QHBoxLayout();
	hLayout1->addWidget(propertyLabel);
	hLayout1->addStretch();

	QHBoxLayout *hLayout2 = new QHBoxLayout();
	hLayout2->addWidget(isDirCheckBox);
	hLayout2->addWidget(isFileCheckBox);
	hLayout2->addWidget(isHiddenCheckBox);
	hLayout2->addWidget(isSymbolLinkCheckBox);
	hLayout2->addWidget(isReadableCheckBox);
	hLayout2->addWidget(isWriteableCheckBox);
	hLayout2->addWidget(isExecutableCheckBox);

	QHBoxLayout *hLayout3 = new QHBoxLayout();
	hLayout3->addWidget(GetFileInfoBtn);

	QVBoxLayout *vLayout = new QVBoxLayout(this);
	vLayout->addLayout(gridLayout);
	vLayout->addLayout(hLayout1);
	vLayout->addLayout(hLayout2);
	vLayout->addLayout(hLayout3);

	//---显示布局
	setLayout(vLayout);

	//-----设置信号与槽
	connect(openFileBtn, SIGNAL(clicked()), this, SLOT(slotFile()));
	connect(GetFileInfoBtn, SIGNAL(clicked()), this, SLOT(slotGetInfo()));

	//----设置窗口属性
	setWindowTitle(codec->toUnicode("文件信息"));
}

//-----通过标准文件对话框获取文件名功能
void FileInfoWidget::slotFile()
{
	//----Qt5解决中文乱码
	QTextCodec *codec = QTextCodec::codecForName("GB18030");

	QString fileNameStr = QFileDialog :: getOpenFileName	(this);
	fileNameEdit->setText(fileNameStr);
}

//-----获取文件的具体信息
void FileInfoWidget::slotGetInfo()
{
	//----Qt5解决中文乱码
	QTextCodec *codec = QTextCodec::codecForName("GB18030");

	//---获取文件名
	QString fileStr = fileNameEdit->text();

	QFileInfo fileInfo(fileStr);
	//---获取文件大小(字节)
	qint64 size = fileInfo.size();
	//--获取文件创建时间
	QDateTime buildTime = fileInfo.created();
	//--获取文件最近修改时间
	QDateTime lastMdTime = fileInfo.lastModified();
	//---获取文件最近访问时间
	QDateTime lastVisitTime = fileInfo.lastRead();

	//--获取文件相关属性
	bool isDir = fileInfo.isDir();
	bool isFile = fileInfo.isFile();
	bool isSymLink = fileInfo.isSymLink();
	bool isHide = fileInfo.isHidden();
	bool isRead = fileInfo.isReadable();
	bool isWrite = fileInfo.isWritable();
	bool isExecu = fileInfo.isExecutable();

	//--显示属性
	sizeLabelEdit->setText(QString :: number(size) + codec->toUnicode(" 字节"));
	buildTimeEdit->setText(buildTime.toString());
	lastModifiedEdit->setText(lastMdTime.toString());
	lastVisitEidt->setText(lastVisitTime.toString());
	
	isDirCheckBox->setCheckState(isDir ? Qt::Checked : Qt :: Unchecked);
	isFileCheckBox->setCheckState(isFile ? Qt::Checked : Qt :: Unchecked);
	isSymbolLinkCheckBox->setCheckState(isSymLink ? Qt::Checked : Qt :: Unchecked);
	isHiddenCheckBox->setCheckState(isHide ? Qt::Checked : Qt :: Unchecked);
	isReadableCheckBox->setCheckState(isRead ? Qt::Checked : Qt :: Unchecked);
	isWriteableCheckBox->setCheckState(isWrite ? Qt::Checked : Qt :: Unchecked);
	isExecutableCheckBox->setCheckState(isExecu ? Qt::Checked : Qt :: Unchecked);
}
main.cpp
#pragma once
#include 
#include "fileInfo_widget.h"

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	FileInfoWidget win;
	win.show();

	return a.exec();
}

运行结果:

Qt学习之路之获取文件信息_第1张图片

更多关于QFileInfo类的使用,请参考assistant。



你可能感兴趣的:(QT学习之路)