QT获取文件信息

QT获取文件信息

效果图如下:

image

代码:

FileInformation.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/********************************************************************
     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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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

?
1
2
3
4
5
6
7
8
9
10
#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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值