重点知识已近在代码里注释...
请仔细看代码
本文原创
转载请保留此链接 http://blog.csdn.net/siren0203
头文件
getInfo.h
#ifndef HEADER_H #define HEADER_H #include <QDialog> #include <QObject> class QPushButton; class QLineEdit; class QPushButton; class QLabel; class QVBoxLayout; class QHBoxLayout; class QGridLayout; class getFileInfo: public QDialog{ Q_OBJECT public: getFileInfo(); public: QPushButton *openFile; QPushButton *getInfo; QLineEdit *filename; QLineEdit *editSize; QLineEdit *editCreateTime; QLineEdit *editUpdateTime; QLineEdit *editLastVTime; QLabel *labelFilename; QLabel *labelSize; QLabel *labelCreateTime; QLabel *labelUpdateTime; QLabel *labelLastVTime; QVBoxLayout *vbox; QHBoxLayout *hbox; QGridLayout *gbox; public slots: void openFileDlg(); void getFileDlg(); }; #endif // HEADER_H
程序实现代码
main.cpp
#include <QApplication> #include <QFileDialog> #include <QPushButton> #include <QLineEdit> #include <QLabel> #include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> #include <QTextCodec> #include <QDialog> #include <QFileInfo> #include <QDateTime> #include "getInfo.h" getFileInfo::getFileInfo(){ openFile=new QPushButton(tr("打开")); getInfo=new QPushButton(tr("获取信息")); editSize=new QLineEdit(this); editCreateTime=new QLineEdit(this); editUpdateTime=new QLineEdit(this); editLastVTime=new QLineEdit(this); filename=new QLineEdit(this); labelFilename=new QLabel(tr("文件名")); labelSize=new QLabel(tr("文件大小")); labelCreateTime=new QLabel(tr("创建时间")); labelUpdateTime=new QLabel(tr("修改时间")); labelLastVTime=new QLabel(tr("最后访问时间")); gbox=new QGridLayout(this); gbox->addWidget(labelFilename,0,1); gbox->addWidget(filename,0,2); gbox->addWidget(openFile,0,3); gbox->addWidget(labelSize,1,1); gbox->addWidget(editSize,1,2); gbox->addWidget(labelCreateTime,2,1); gbox->addWidget(editCreateTime,2,2); gbox->addWidget(labelUpdateTime,3,1); gbox->addWidget(editUpdateTime,3,2); gbox->addWidget(labelLastVTime,4,1); gbox->addWidget(editLastVTime,4,2); hbox=new QHBoxLayout(this); hbox->addWidget(getInfo); gbox->addLayout(hbox,5,2); setLayout(gbox); //关联信号到槽.. connect(openFile,SIGNAL(clicked()),this,SLOT(openFileDlg())); connect(getInfo,SIGNAL(clicked()),this,SLOT(getFileDlg())); setWindowTitle(tr("斯人http://blog.csdn.net/siren0203")); } //打开文件 void getFileInfo::openFileDlg(){ QString files=QFileDialog::getOpenFileName(this); filename->setText(files); } //获取文件信息 void getFileInfo::getFileDlg(){ //打开文件 QFileInfo f(filename->text()); //获取文件创建时间 QDateTime createTime=f.created(); //获取文件最后更新时间 QDateTime updateTime=f.lastModified(); //获取文件最后浏览时间 QDateTime lastRTime=f.lastRead(); //显示属性 editSize->setText(QString("%1").arg(f.size())); editCreateTime->setText(createTime.toString()); editUpdateTime->setText(updateTime.toString()); editLastVTime->setText(lastRTime.toString()); } int main(int argc,char ** argv){ QApplication app(argc,argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("gb2312")); getFileInfo info; info.show(); return app.exec(); }