QT获取文件信息

简介:

效果图如下:

image

代码:

FileInformation.h

/********************************************************************
	created:	2012/04/10
	created:	10:4:2012   17:10
	filename: 	F:\C++\FileInformation\FileInformation\fileinformation.h
	file path:	F:\C++\FileInformation\FileInformation
	file base:	fileinformation
	file ext:	h
	author:		Rollen Holt
	
	purpose:	The head File of this project
*********************************************************************/
#ifndef FILEINFORMATION_H
#define FILEINFORMATION_H

#include <QtGui>   //in order to convenient, don't recommend.
#include "ui_fileinformation.h"

class FileInformation : public QDialog
{
	Q_OBJECT

public:
	FileInformation(QWidget *parent = 0, Qt::WFlags flags = 0);
	~FileInformation();

private:
	Ui::FileInformationClass ui;

	QLabel *fileNameLabel;
	QLineEdit *fileNameLineEdit;
	QPushButton *fileButton;

	QLabel *fileSize;
	QLineEdit *fileLineEditSize;

	QLabel *fileCreatLabel;
	QLineEdit *fileCreatLineEdit;

	QLabel *fileLastModifyLabel;
	QLineEdit *fileLastModifLineEdit;

	QLabel *fileLastVisitedLabel;
	QLineEdit *fileLastVisitedLineEdit;

	QLabel *filePropertyLabel;
	QCheckBox *checkIsDir;
	QCheckBox *checkIsFile;
	QCheckBox* checkBoxIsSymLink;
	QCheckBox* checkBoxIsHidden;
	QCheckBox* checkBoxIsReadable;	
	QCheckBox* checkBoxIsWritable;	
	QCheckBox* checkBoxIsExecutable;

	QPushButton* PushButtonGet;

	void getFileInformation(QString fileName);

	private slots:
		void slotFile();
		void slotGet();
};

#endif // FILEINFORMATION_H

fileInformation.cpp

#include "fileinformation.h"

FileInformation::FileInformation(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);
	setWindowTitle(tr("File Information"));

	fileNameLabel=new QLabel(tr("File Name"));
	fileNameLineEdit=new QLineEdit();
	fileButton=new QPushButton(tr("Open"));
	QHBoxLayout *layout1=new QHBoxLayout();
	layout1->addWidget(fileNameLabel);
	layout1->addWidget(fileNameLineEdit);
	layout1->addWidget(fileButton);

	fileSize=new QLabel(tr("File Size"));
	fileLineEditSize=new QLineEdit();
	QHBoxLayout *layout2=new QHBoxLayout();
	layout2->addWidget(fileSize);
	layout2->addWidget(fileLineEditSize);

	fileCreatLabel=new QLabel(tr("Creat Time"));
	fileCreatLineEdit=new QLineEdit();
	QHBoxLayout *layout3=new QHBoxLayout();
	layout3->addWidget(fileCreatLabel);
	layout3->addWidget(fileCreatLineEdit);

	fileLastModifyLabel=new QLabel(tr("Last Modify Time"));
	fileLastModifLineEdit=new QLineEdit();
	QHBoxLayout *layout4=new QHBoxLayout();
	layout4->addWidget(fileLastModifyLabel);
	layout4->addWidget(fileLastModifLineEdit);

	fileLastVisitedLabel=new QLabel(tr("Last Visited Time"));
	fileLastVisitedLineEdit=new QLineEdit();

	filePropertyLabel=new QLabel(tr("File Property"));
	checkIsDir=new QCheckBox(tr("Menu"));
	checkIsFile=new QCheckBox(tr("File"));
	checkBoxIsSymLink=new QCheckBox(tr("SymLink"));
	checkBoxIsReadable=new QCheckBox(tr("Readable"));
	checkBoxIsWritable=new QCheckBox(tr("Writeable"));
	checkBoxIsHidden=new QCheckBox(tr("Hodden"));
	checkBoxIsExecutable=new QCheckBox(tr("Executable"));
	QHBoxLayout *layout5=new QHBoxLayout();
	layout5->addWidget(filePropertyLabel);
	layout5->addWidget(checkIsDir);
	layout5->addWidget(checkIsFile);
	layout5->addWidget(checkBoxIsSymLink);
	layout5->addWidget(checkBoxIsReadable);
	layout5->addWidget(checkBoxIsWritable);
	layout5->addWidget(checkBoxIsExecutable);

	PushButtonGet=new QPushButton(tr("Get File Information"));

	// main layout
	QVBoxLayout *mainLayout=new QVBoxLayout();
	mainLayout->addLayout(layout1);
	mainLayout->addLayout(layout2);
	mainLayout->addLayout(layout3);
	mainLayout->addLayout(layout4);
	mainLayout->addLayout(layout5);
	mainLayout->addWidget(PushButtonGet);
	setLayout(mainLayout);

	connect(fileButton,SIGNAL(clicked()),this,SLOT(slotFile()));
	connect(PushButtonGet,SIGNAL(clicked()),this,SLOT(slotGet()));
}

FileInformation::~FileInformation()
{
	
}

/*
 * Get file information
 **/
void FileInformation::getFileInformation(QString fileName){
	QFileInfo info(fileName);

	qint64 size=info.size();
	QDateTime creatTime=info.created();
	QDateTime lastModifyTime=info.lastModified();
	QDateTime lastReadTime=info.lastRead();
	
	bool isDir=info.isDir();
	bool isFile=info.isFile();
	bool isSymLink = info.isSymLink();
	bool isHidden = info.isHidden();
	bool isReadable = info.isReadable();
	bool isWritable = info.isWritable();
	bool isExecutable =info.isExecutable();

	fileLineEditSize->setText(QString::number(size));
	fileCreatLineEdit->setText(creatTime.toString());
	fileLastModifLineEdit->setText(lastModifyTime.toString());
	fileLastVisitedLineEdit->setText(lastReadTime.toString());

	checkIsDir->setCheckable(isDir?Qt::Checked:Qt::Unchecked);
	checkIsFile->setCheckable(isFile?Qt::Checked:Qt::Unchecked);
	checkBoxIsSymLink->setCheckState (isSymLink?Qt::Checked:Qt::Unchecked);
	checkBoxIsHidden->setCheckState (isHidden?Qt::Checked:Qt::Unchecked);
	checkBoxIsReadable->setCheckState (isReadable?Qt::Checked:Qt::Unchecked);
	checkBoxIsWritable->setCheckState (isWritable?Qt::Checked:Qt::Unchecked);
	checkBoxIsExecutable->setCheckState (isExecutable?Qt::Checked:Qt::Unchecked);
}

/*
 *	Open file
 **/
void FileInformation::slotFile(){
	QString s=QFileDialog::getOpenFileName(this,tr("open file"),"/","files(*)");
	fileNameLineEdit->setText(s.toAscii());
}


void FileInformation::slotGet(){
	getFileInformation(fileNameLineEdit->text());
}

main.cpp

#include "fileinformation.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	FileInformation *w=new FileInformation();
	w->show();
	return a.exec();
}
目录
相关文章
|
4月前
【qt】如何读取文件并拆分信息?
【qt】如何读取文件并拆分信息?
39 0
|
4月前
|
网络协议
【qt】TCP客户端信息的接受和发送
【qt】TCP客户端信息的接受和发送
34 0
|
4月前
|
网络协议
【qt】TCP 服务端怎么收到信息?
【qt】TCP 服务端怎么收到信息?
45 0
|
5月前
|
C++
基于QT实现的拷贝文件以及实时进度条(简易版)
1.基于按钮或者菜单栏的槽里去写逻辑函数(我这边用的是菜单栏),ui实现的进度条 2.创建两个对象,一个是源文件,一个是目标文件分别用getopenfileName、getsavefileName函数即可。 3.利用QFile类去实现对两个文件的创建,因为QFile中可以获取文件的属性已经读写等。 4.循环的去读取源文件中的数据,然后写入目标文件
517 6
|
4月前
|
存储 算法 C++
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
【C++】C++ QT实现Huffman编码器与解码器(源码+课程论文+文件)【独一无二】
|
4月前
【Qt 学习笔记】Qt窗口 | 标准对话框 | 文件对话框QFileDialog
【Qt 学习笔记】Qt窗口 | 标准对话框 | 文件对话框QFileDialog
765 4
|
4月前
|
XML 开发框架 API
【Qt 学习笔记】QWidget的windowTitle属性 | windowIcon属性 | qrc文件机制
【Qt 学习笔记】QWidget的windowTitle属性 | windowIcon属性 | qrc文件机制
178 1
|
4月前
QT 软件打包为一个单独可执行.exe文件流程
QT 软件打包为一个单独可执行.exe文件流程
811 0
|
4月前
|
安全 C++ Windows
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
Windows下C++使用gRPC(Qt和VS,含文件包和使用方法)
|
4月前
|
数据安全/隐私保护
【qt】获取主机信息系统
【qt】获取主机信息系统
18 0