1. 程式人生 > >QT 獲取檔案屬性

QT 獲取檔案屬性

參考教材例項,增加了註釋和新功能,方便大家交流學習

 //fileinformation.cpp

include"fileinformation.h"

#

FileInformation::FileInformation(QWidget*parent,Qt::WindowFlagsf)
:QDialog(parent,f)
{
setWindowTitle(tr("FileInformation"));
LabelFileName=newQLabel();
LabelFileName->setText(tr("FileName:"));
LineEditFileName=newQLineEdit
();
PushButtonFile=newQPushButton(this);
PushButtonFile->setText(tr("File"));
LabelSize=newQLabel();
LabelSize->setText(tr("Size:"));
LineEditSize=newQLineEdit();
LabelCreated=newQLabel();
LabelCreated->setText(tr("Created:"));
LineEditCreated=newQLineEdit();
LabelLastModified=newQLabel();
LabelLastModified->setText(tr("LastModified:"));
LineEditLastModified=newQLineEdit();
LabelLastRead=newQLabel();
LabelLastRead->setText(tr("LastRead:"));
LineEditLastRead=newQLineEdit();
LabelProperty=newQLabel();
LabelProperty->setText(tr("Property:"));
CheckBoxIsDir=newQCheckBox();
CheckBoxIsDir->
setText(tr("Dir"));
CheckBoxIsFile=newQCheckBox();
CheckBoxIsFile->setText(tr("File"));
CheckBoxIsSymLink=newQCheckBox();
CheckBoxIsSymLink->setText(tr("SymLink"));
CheckBoxIsHidden=newQCheckBox();
CheckBoxIsHidden->setText(tr("Hidden"));
CheckBoxIsReadable=newQCheckBox();
CheckBoxIsReadable->setText(tr("Readable"));
CheckBoxIsWritable=newQCheckBox();
CheckBoxIsWritable->setText(tr("Writable"));
CheckBoxIsExecutable=newQCheckBox();
CheckBoxIsExecutable->setText(tr("Executable"));
PushButtonGet=newQPushButton(this);
PushButtonGet->setText(tr("Get"));
QHBoxLayout*hbLayout1=newQHBoxLayout();
hbLayout1->addWidget(LabelFileName);
hbLayout1->addWidget(LineEditFileName);
hbLayout1->addWidget(PushButtonFile);
QHBoxLayout*hbLayout2=newQHBoxLayout();
hbLayout2->addWidget(LabelSize);
hbLayout2->addWidget(LineEditSize);
QHBoxLayout*hbLayout3=newQHBoxLayout();
hbLayout3->addWidget(LabelCreated);
hbLayout3->addWidget(LineEditCreated);
QHBoxLayout*hbLayout4=newQHBoxLayout();
hbLayout4->addWidget(LabelLastModified);
hbLayout4->addWidget(LineEditLastModified);
QHBoxLayout*hbLayout5=newQHBoxLayout();
hbLayout5->addWidget(LabelLastRead);
hbLayout5->addWidget(LineEditLastRead);
QHBoxLayout*hbLayout6=newQHBoxLayout();
hbLayout6->addWidget(LabelProperty);
hbLayout6->addStretch();
QHBoxLayout*hbLayout7=newQHBoxLayout();
hbLayout7->addWidget(CheckBoxIsDir);
hbLayout7->addWidget(CheckBoxIsFile);
hbLayout7->addWidget(CheckBoxIsSymLink);
hbLayout7->addWidget(CheckBoxIsHidden);
hbLayout7->addWidget(CheckBoxIsReadable);
hbLayout7->addWidget(CheckBoxIsWritable);
hbLayout7->addWidget(CheckBoxIsExecutable);
QVBoxLayout*vbLayout=newQVBoxLayout(this);
vbLayout->addLayout(hbLayout1);
vbLayout->addLayout(hbLayout2);
vbLayout->addLayout(hbLayout3);
vbLayout->addLayout(hbLayout4);
vbLayout->addLayout(hbLayout5);
vbLayout->addLayout(hbLayout6);
vbLayout->addLayout(hbLayout7);
vbLayout->addWidget(PushButtonGet);
connect(PushButtonFile,SIGNAL(clicked()),this,SLOT(slotFile()));
connect(PushButtonGet,SIGNAL(clicked()),this,SLOT(slotGet()));
}
FileInformation::~FileInformation()
{
}
voidFileInformation::slotFile()
{
QStrings=QFileDialog::getOpenFileName(//獲取檔案路徑
this,"openfiledialog",
"/",
"files(*)");
LineEditFileName->setText(s.toAscii().data());//檔案路徑
}
voidFileInformation::slotGet()
{
QFileInfoinfo(LineEditFileName->text());
//獲取檔案大小
//在不同的平臺上,基本的C++型別如short,char,int,long,longlong會有不同的字長。
//最好把它們轉換為qint8,quint8,qint16,quint16,qint32,
//quint32,qint64,quint64,這些型別能確保字長是不隨平臺改變的。
qint64size=info.size();
//讀取檔案時間資訊
QDateTimecreated=info.created();
QDateTimelastModified=info.lastModified();
QDateTimelastRead=info.lastRead();
//讀取檔案屬性
boolisDir=info.isDir();
boolisFile=info.isFile();
boolisSymLink=info.isSymLink();
boolisHidden=info.isHidden();
boolisReadable=info.isReadable();
boolisWritable=info.isWritable();
boolisExecutable=info.isExecutable();
LineEditSize->setText(QString::number(size));
//顯示檔案時間資訊
LineEditCreated->setText(created.toString());
LineEditLastModified->setText(lastModified.toString());
LineEditLastRead->setText(lastRead.toString());
//顯示檔案屬性
CheckBoxIsDir->setCheckState(isDir?Qt::Checked:Qt::Unchecked);
CheckBoxIsFile->setCheckState(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);
}

----------------------------------------------------------------------------------------------

//fileinformation.h

#ifndefFILEINFORMATION_H

#defineFILEINFORMATION_H
#include<QtGui>
classFileInformation:publicQDialog
{
Q_OBJECT
public:
FileInformation(QWidget*parent=0,Qt::WindowFlagsf=0);
~FileInformation();
public:
QLabel*LabelFileName;
QLineEdit*LineEditFileName;
QPushButton*PushButtonFile;
QLabel*LabelSize;
QLineEdit*LineEditSize;
QLabel*LabelCreated;
QLineEdit*LineEditCreated;
QLabel*LabelLastModified;
QLineEdit*LineEditLastModified;
QLabel*LabelLastRead;
QLineEdit*LineEditLastRead;
QLabel*LabelProperty;
QCheckBox*CheckBoxIsDir;
QCheckBox*CheckBoxIsFile;
QCheckBox*CheckBoxIsSymLink;
QCheckBox*CheckBoxIsHidden;
QCheckBox*CheckBoxIsReadable;
QCheckBox*CheckBoxIsWritable;
QCheckBox*CheckBoxIsExecutable;
QPushButton*PushButtonGet;
publicslots:
voidslotFile();
voidslotGet();
};
#endif
----------------------------------------------------------------------------------------------
//main.cpp

#include"fileinformation.h"

#include<QApplication>
intmain(intargc,char**argv)
{
QFontfont("ZYSong18030",12);
QApplication::setFont(font);
QApplicationa(argc,argv);
FileInformationfileinformation;
fileinformation.show();
returna.exec();
}
----------------------------------------------------------------------------------------------

//fileinformation.pro

TEMPLATE = app

TARGET		= fileinformation
HEADERS		= fileinformation.h
SOURCES		= fileinformation.cpp /
		  main.cpp

----------------------------------------------------------------------------------------------