QFileInfo
Headers
fileinfo.h
cpp
#ifndef FILEINFO_H
#define FILEINFO_H
#include <QWidget>
namespace Ui {
class FileInfo;
}
class FileInfo : public QWidget
{
Q_OBJECT
public:
explicit FileInfo(QWidget *parent = 0);
~FileInfo();
void getFileInfo(QString & fileName);
private slots:
void on_browseFileBTtn_clicked();
private:
Ui::FileInfo *ui;
};
#endif // FILEINFO_H
Sources
fileinfo.cpp
cpp
#include "fileinfo.h"
#include "ui_fileinfo.h"
#include <QFileDialog>
#include <QDateTime>
FileInfo::FileInfo(QWidget *parent) :
QWidget(parent),
ui(new Ui::FileInfo)
{
ui->setupUi(this);
setWindowTitle("文件信息");
}
FileInfo::~FileInfo()
{
delete ui;
}
void FileInfo::getFileInfo(QString &fileName)
{
QFileInfo info(fileName);
qint64 size = info.size(); //文件大小
QDateTime createTime = info.created(); //文件创建日期
QDateTime lastMTime = info.lastModified(); //文件最后修改时间
QDateTime lastRTime = info.lastRead(); //文件最近访问时间
//文件的属性信息
bool bIsDir = info.isDir(); //文件是否是目录
bool BIsFile = info.isFile(); //文件是否是文件
bool bIsSymLink = info.isSymLink();
bool bIsHidden = info.isHidden();
bool bIsReadable = info.isReadable();
bool bIsWritable = info.isWritable();
bool bIsExcutable = info.isExecutable();
ui->FileSizeLineEidt->setText(QString::number(size));
ui->FileCreateLineEdit->setText(createTime.toString());
ui->FileModifyLineEdit->setText(lastMTime.toString());
ui->FileReadLineEdit->setText(lastRTime.toString());
ui->isDirCheckBox->setChecked(bIsDir);
ui->isFileCheckBox->setChecked(BIsFile);
ui->isSymLinkCheckBox->setChecked(bIsSymLink);
ui->isHiddenCheckBox->setChecked(bIsHidden);
ui->isWritableCheckBox->setChecked(bIsWritable);
ui->isReadableCheckBox->setChecked(bIsReadable);
ui->isExecutableCheckBox->setChecked(bIsExcutable);
}
void FileInfo::on_browseFileBTtn_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
"打开文件",
".",
"files(*)");
// QString path = QFileDialog::getExistingDirectory(this,
// "选择目录",
// ".");
ui->FileNameLineEdit->setText(fileName);
getFileInfo(fileName);
}
main.cpp
cpp
#include "fileinfo.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileInfo w;
w.show();
return a.exec();
}
Forms
fileinfo.ui
运行结果
目录:
文件: