fileinfo.h
/** * 书本:【Qt5开发及实例】 * 功能:获得文件的信息 * 文件:fileinfo.h * 时间:2015年2月4日17:23:38 * 作者:cutter_point */ #ifndef FILEINFO_H #define FILEINFO_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include <QCheckBox> class fileinfo : public QDialog { Q_OBJECT public: fileinfo(QWidget *parent = 0, Qt::WindowFlags f = 0); ~fileinfo(); public slots: //按钮文件响应的事件槽函数 void slotFile(); //按钮获得文件信息槽函数 void slotGet(); private: //一共是6个label 5个lineEdit 2个pushbutton 7个checkbox //第一行 QLabel *fileNameLabel; QLineEdit *fileNameLineEdit; QPushButton *fileBtn; //第二行 QLabel *sizeLabel; QLineEdit *sizeLineEdit; //第三行 QLabel *createTimeLabel; QLineEdit *createTimeLineEdit; //第四行 QLabel *lastModifiedLabel; QLineEdit *lastModifiedLineEdit; //第五行 QLabel *lastReadLabel; QLineEdit *lastReadLineEdit; //最后选择框 QLabel *propertyLabel; QCheckBox *isDirCheckBox; QCheckBox *isFileCheckBox; QCheckBox *isSymLinkCheckBox; QCheckBox *isHiddenCheckBox; QCheckBox *isReadableCheckBox; QCheckBox *isWritableCheckBox; QCheckBox *isExecutableCheckBox; QPushButton *getBtn; }; #endif // FILEINFO_H
fileinfo.cpp
/** * 书本:【Qt5开发及实例】 * 功能:获得文件的信息 * 文件:fileinfo.h * 时间:2015年2月4日17:23:38 * 作者:cutter_point */ #include "fileinfo.h" #include <QGridLayout> #include <QHBoxLayout> #include <QVBoxLayout> #include <QFileDialog> #include <QDateTime> fileinfo::fileinfo(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f) { fileNameLabel = new QLabel(tr("文件名:")); fileNameLineEdit = new QLineEdit; fileBtn = new QPushButton(tr("文件")); sizeLabel = new QLabel(tr("大小:")); sizeLineEdit = new QLineEdit; createTimeLabel = new QLabel(tr("创建时间:")); createTimeLineEdit = new QLineEdit; lastModifiedLabel = new QLabel(tr("最后修改时间:")); lastModifiedLineEdit = new QLineEdit; lastReadLabel = new QLabel(tr("最后访问时间:")); lastReadLineEdit = new QLineEdit; propertyLabel = new QLabel(tr("属性:")); isDirCheckBox = new QCheckBox(tr("目录")); isFileCheckBox = new QCheckBox(tr("文件")); isSymLinkCheckBox = new QCheckBox(tr("符号连接")); isHiddenCheckBox = new QCheckBox(tr("隐藏")); isReadableCheckBox = new QCheckBox(tr("可读")); isWritableCheckBox = new QCheckBox(tr("可写")); isExecutableCheckBox = new QCheckBox(tr("执行")); getBtn = new QPushButton(tr(" 获得文件信息:")); QGridLayout *gridLayout = new QGridLayout; //fileNameLabel = new QLabel(tr("文件名:")); gridLayout->addWidget(fileNameLabel, 0, 0); gridLayout->addWidget(fileNameLineEdit, 0, 1); gridLayout->addWidget(fileBtn, 0, 2); //sizeLabel = new QLabel(tr("大小:")); gridLayout->addWidget(sizeLabel, 1, 0); gridLayout->addWidget(sizeLineEdit, 1, 1, 1, 2); //createTimeLabel = new QLabel(tr("创建时间:")); gridLayout->addWidget(createTimeLabel, 2, 0); gridLayout->addWidget(createTimeLineEdit, 2, 1, 1, 2); // lastModifiedLabel = new QLabel(tr("最后修改时间:")); gridLayout->addWidget(lastModifiedLabel,3,0); gridLayout->addWidget(lastModifiedLineEdit,3,1,1,2); // lastReadLabel = new QLabel(tr("最后访问时间:")); gridLayout->addWidget(lastReadLabel,4,0); gridLayout->addWidget(lastReadLineEdit,4,1,1,2); //然后就是我们的属性行和多选框 QHBoxLayout *layout2 = new QHBoxLayout; layout2->addWidget(propertyLabel); //属性 layout2->addStretch(); //弹簧 QHBoxLayout *layout3 = new QHBoxLayout; layout3->addWidget(isDirCheckBox); layout3->addWidget(isFileCheckBox); layout3->addWidget(isSymLinkCheckBox); layout3->addWidget(isHiddenCheckBox); layout3->addWidget(isReadableCheckBox); layout3->addWidget(isWritableCheckBox); layout3->addWidget(isExecutableCheckBox); //最后一个按钮 QHBoxLayout *layout4 = new QHBoxLayout; layout4->addWidget(getBtn); //把所有空间竖着排列 QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addLayout(gridLayout); mainLayout->addLayout(layout2); mainLayout->addLayout(layout3); mainLayout->addLayout(layout4); connect(fileBtn, SIGNAL(clicked()), this, SLOT(slotFile())); connect(getBtn, SIGNAL(clicked()), this, SLOT(slotGet())); } //按钮文件响应的事件槽函数 //void slotFile(); void fileinfo::slotFile() { //打开文件的对话框 QString fileName = QFileDialog::getOpenFileName(this, " 打开 ", "/", "files(*)"); fileNameLineEdit->setText(fileName); } //按钮获得文件信息槽函数 //void slotGet(); void fileinfo::slotGet() { QString file = fileNameLineEdit->text(); //获取文件路径 QFileInfo info(file); qint64 size = info.size(); QDateTime created = info.created(); //文件创建时间 QDateTime lastModified = info.lastModified(); //文件最后一次修改的时间 QDateTime lastRead = 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(); //可执行文件 sizeLineEdit->setText(QString::number(size)); //文件大小 createTimeLineEdit->setText(created.toString()); //创建时间 lastModifiedLineEdit->setText(lastModified.toString()); //最后一次修改的时间 lastReadLineEdit->setText(lastRead.toString()); //最后一次访问时间 // isDirCheckBox->setCheckState(isDir ? Qt::Checked : Qt::Unchecked); //是否选择,这个多选框 isDirCheckBox->setCheckState(isDir?Qt::Checked:Qt::Unchecked); isFileCheckBox->setCheckState(isFile?Qt::Checked:Qt::Unchecked); isSymLinkCheckBox->setCheckState(isSymLink?Qt::Checked:Qt::Unchecked); isHiddenCheckBox->setCheckState(isHidden?Qt::Checked:Qt::Unchecked); isReadableCheckBox->setCheckState(isReadable?Qt::Checked:Qt::Unchecked); isWritableCheckBox->setCheckState(isWritable?Qt::Checked:Qt::Unchecked); isExecutableCheckBox->setCheckState(isExecutable?Qt::Checked:Qt:: Unchecked); } fileinfo::~fileinfo() { }
运行结果
时间: 2024-10-24 20:47:02