Qt——实现一个简单的获取文件信息的dialog
实现效果
选择文件:
获取文件信息:
实现
1.创建项目
新建一个项目,随便起一个项目名FileInfo
,让FileInfo
继承自 QDialog
类。
2.项目布局
topLayout
使用的是 网格布局(QGridLayout
),把 属性 标签以上的所有控件都放入其布局中centerLayout
使用的是 水平布局 (·QHBoxLayout
),把几个多选框放入其布局中mainLayout
使用的是 垂直布局 (QVBoxLayout
),把topLayout
和centerLayout
以及 最后一个获取文件信息的按钮放入到其布局中
3.代码
fileinfo.h
:
#ifndef FILEINFO_H
#define FILEINFO_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
class FileInfo : public QDialog
{
Q_OBJECT
public:
FileInfo(QWidget *parent = nullptr);
~FileInfo();
public slots:
void slotFile();
void slotGet();
private:
QLabel * fileLabel;
QLineEdit * fileLineEdit;
QPushButton * fileBtn;
QLabel * sizeLabel;
QLineEdit * sizeLineEdit;
QLabel * createdTimeLabel;
QLineEdit * createdTimeLineEdit;
QLabel * lastModifiedLabel;
QLineEdit * lastModifiedLineEdit;
QLabel * lastVisitLabel;
QLineEdit * lastVisitLineEdit;
QLabel * propertyLabel;
QCheckBox * isDirCheckBox;
QCheckBox * isFileCheckBox;
QCheckBox * isSymbolLinkCheckBox;
QCheckBox * isHiddenCheckBox;
QCheckBox * isReadCheckBox;
QCheckBox * isWritaleCheckBox;
QCheckBox * isExecutableCheckBox;
QPushButton * getBtn;
};
#endif // FILEINFO_H
fileinfo.cpp
:
#include "fileinfo.h"
#include <QGridLayout>
#include <QFileDialog>
#include <QDateTime>
FileInfo::FileInfo(QWidget *parent)
: QDialog(parent)
{
fileLabel = new QLabel(tr("文件名:"));
fileLineEdit = new QLineEdit;
fileBtn = new QPushButton(tr("文件"));
sizeLabel = new QLabel(tr("大小:"));
sizeLineEdit = new QLineEdit;
createdTimeLabel = new QLabel(tr("创建时间:"));
createdTimeLineEdit = new QLineEdit;
lastModifiedLabel = new QLabel(tr("最后修改时间:"));
lastModifiedLineEdit = new QLineEdit;
lastVisitLabel = new QLabel(tr("最后访问时间:"));
lastVisitLineEdit = new QLineEdit;
propertyLabel = new QLabel(tr("属性:"));
isDirCheckBox = new QCheckBox(tr("目录"));
isFileCheckBox = new QCheckBox(tr("文件"));
isSymbolLinkCheckBox = new QCheckBox(tr("符号链接"));
isHiddenCheckBox = new QCheckBox(tr("隐藏"));
isReadCheckBox = new QCheckBox(tr("读"));
isWritaleCheckBox = new QCheckBox(tr("写"));
isExecutableCheckBox = new QCheckBox(tr("执行"));
getBtn = new QPushButton(tr("获取文件信息"));
//top
QGridLayout * topLayout = new QGridLayout;
topLayout->addWidget(fileLabel,0,0);
topLayout->addWidget(fileLineEdit,0,1);
topLayout->addWidget(fileBtn,0,2);
topLayout->addWidget(sizeLabel,1,0);
topLayout->addWidget(sizeLineEdit,1,1);
topLayout->addWidget(createdTimeLabel,2,0);
topLayout->addWidget(createdTimeLineEdit,2,1);
topLayout->addWidget(lastModifiedLabel,3,0);
topLayout->addWidget(lastModifiedLineEdit,3,1);
topLayout->addWidget(lastVisitLabel,4,0);
topLayout->addWidget(lastVisitLineEdit,4,1);
topLayout->addWidget(propertyLabel,5,0);
//center
QHBoxLayout * centerLayout = new QHBoxLayout;
centerLayout->addWidget(isDirCheckBox);
centerLayout->addWidget(isFileCheckBox);
centerLayout->addWidget(isSymbolLinkCheckBox);
centerLayout->addWidget(isHiddenCheckBox);
centerLayout->addWidget(isReadCheckBox);
centerLayout->addWidget(isWritaleCheckBox);
centerLayout->addWidget(isExecutableCheckBox);
//main
QVBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(topLayout);
mainLayout->addLayout(centerLayout);
mainLayout->addWidget(getBtn);
connect(fileBtn,SIGNAL(clicked()),this,SLOT(slotFile()));
connect(getBtn,SIGNAL(clicked()),this,SLOT(slotGet()));
}
//获取文件名
void FileInfo::slotFile(){
QString fileName = QFileDialog::getOpenFileName(this,tr("打 开"),"/","files (*)");
fileLineEdit->setText(fileName);
}
//通过QFileInfo 获取文件信息
void FileInfo::slotGet(){
QString fileName = fileLineEdit->text();
QFileInfo info(fileName);
qint64 size = info.size();
QDateTime createdTime = info.created();
QDateTime lastModified = info.lastModified();
QDateTime lastVisit = info.lastRead();
bool isDir = info.isDir();
bool isFile = info.isFile();
bool isSymbolLink = info.isSymLink();
bool isHidden = info.isHidden();
bool isReadable = info.isReadable();
bool isWritable = info.isWritable();
bool isExecutable = info.isExecutable();
sizeLineEdit->setText(QString::number(size));
createdTimeLineEdit->setText(createdTime.toString());
lastModifiedLineEdit->setText(lastModified.toString());
lastVisitLineEdit->setText(lastVisit.toString());
isDirCheckBox->setCheckState(isDir ? Qt::Checked : Qt::Unchecked);
isFileCheckBox->setCheckState(isFile ? Qt::Checked : Qt::Unchecked);
isSymbolLinkCheckBox->setCheckState(isSymbolLink ? Qt::Checked : Qt::Unchecked);
isHiddenCheckBox->setCheckState(isHidden ? Qt::Checked : Qt::Unchecked);
isReadCheckBox->setCheckState(isReadable ? Qt::Checked : Qt::Unchecked);
isWritaleCheckBox->setCheckState(isWritable ? Qt::Checked : Qt::Unchecked);
isExecutableCheckBox->setCheckState(isExecutable ? Qt::Checked : Qt::Unchecked);
}
FileInfo::~FileInfo()
{
}
main.cpp
:
#include "fileinfo.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileInfo w;
w.show();
return a.exec();
}