Qt获取文件属性
文档名称 |
Qt获取文件属性 |
创建时间 |
2012-9-13 |
修改时间 |
2012-9-13 |
创建人 |
Baifx |
简介(收获) |
QFileInfo类的使用 |
一、综述。
在Qt中QFileInfo类提供了系统独立的文件信息。QFileInfo提供了文件在文件系统中的文件名称与位置信息,以及文件的权限、目录、文件或符号连接等,也提供了文件的大小、创建时间、最后修改时间、最后访问时间等信息。QFileInfo可以使用绝对路径和相对路径来指向同一个文件,绝对路径以“/”开头(在windows中以磁盘符号开头),相对路径则以目录名或文件名开头,isRelative()方法可以用来判断QFileInfo使用的是绝对路径还是相对路径。makAbsolute()方法可以用来将相对路径转化为绝对路径。
为了加快执行效率,QFileInfo可以将文件信息进行一次读取缓存,这样后续的访问就不需要持续访问文件了,但是由于文件在读取信息之后可能被其他程序或本程序改变属性,因此QFileInfo通过refresh()方法提供了一种刷新机制可以更新文件信息,用户也可以通过setCaching()方法关闭这种缓冲功能。
文件属性可以通过isFile()、isDir()和isSysLink()获得,对于符号连接,sysLinkTarget()方法可以获得符号连接指向的文件名称。
QFileInfo还提供了对文件路径的有效分离的方法,如下:
QFileInfo fi( “/tmp/archive.tar.gz” );
QString absoluteFilePath = fi.absoluteFilePath();
//absoluteFilePath = “/tmp/archive.tar.gz”
QString absolutePath = fi.absolutePath(); //absolutePath = “/tmp”
QString baseName = fi.baseName(); //baseName = “archive”
QString completeBaseName = fi.completeBaseName();
//completeBaseName = “archive.tar”
QString completeSuffix = fi.completeSuffix();
//completeSuffix = “tar.gz”
QString fileName = fi.fileName(); //fileName = “archive.tar.gz”
QString filePath = fi.filePath(); //filePath = “/tmp/archive.tar.gz”
QString path = fi.path(); //path = “/tmp”
QString suffix = fi.suffix(); //suffix = “gz”
文件的日期可以由created()、lastModified()、lastRead()等方法获得,文件的存取权限可以由isReadable()、isWritable()和i***ecutable()的方法获得,文件的所有权限可以由owner()、ownerId()、group()、groupId()等方法获得。Permission()方法可以用来测试一个文件的权限。
二、实例
“fileInfo.h”代码如下:
class fileInfo : public QDialog
{
Q_OBJECT
public:
fileInfo(QWidget *parent = 0, Qt::WFlags flags = 0);
~fileInfo();
protected slots:
void slotFile();
void slotGet();
private:
void getFileInformation( QString file );
private:
QLabel * pLabelFileName;
QLineEdit * pLineEditFileName;
QPushButton * pPushButtonFile;
QLabel * pLabelSize;
QLineEdit * pLineEditSize;
QLabel * pLabelCreated;
QLineEdit * pLineEditCreated;
QLabel * pLabelLastModified;
QLineEdit * pLineEditLastModified;
QLabel * pLabelLastRead;
QLineEdit * pLineEditLastRead;
QLabel * pLabelProperty;
QCheckBox * pCheckBoxIsDir;
QCheckBox * pCheckBoxIsFile;
QCheckBox * pCheckBoxIsSymLink;
QCheckBox * pCheckBoxIsHidden;
QCheckBox * pCheckBoxIsReadable;
QCheckBox * pCheckBoxIsWritable;
QCheckBox * pCheckBoxI***ecutable;
QPushButton * pPushButtonGet;
};
“fileInfo.cpp”代码如下:
fileInfo::fileInfo(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
setWindowTitle( tr( "FileInformation" ) );
pLabelFileName = new QLabel();
pLabelFileName->setText( tr( "File Name:" ) );
pLineEditFileName = new QLineEdit();
pPushButtonFile = new QPushButton( this );
pPushButtonFile->setText( tr( "File" ) );
pLabelSize = new QLabel();
pLabelSize->setText( tr( "Size:" ) );
pLineEditSize = new QLineEdit();
pLabelCreated = new QLabel();
pLabelCreated->setText( "Created:" );
pLineEditCreated = new QLineEdit();
pLabelLastModified = new QLabel();
pLabelLastModified->setText( tr( "LastModified:" ) );
pLineEditLastModified = new QLineEdit();
pLabelLastRead = new QLabel();
pLabelLastRead->setText( tr( "LastRead:" ) );
pLineEditLastRead = new QLineEdit();
pLabelProperty = new QLabel();
pLabelProperty->setText( tr( "Property:" ) );
pCheckBoxIsDir = new QCheckBox();
pCheckBoxIsDir->setText( tr( "Dir" ) );
pCheckBoxIsFile = new QCheckBox();
pCheckBoxIsFile->setText( tr( "File" ) );
pCheckBoxIsSymLink = new QCheckBox();
pCheckBoxIsSymLink->setText( tr( "SymLink" ) );
pCheckBoxIsHidden = new QCheckBox();
pCheckBoxIsHidden->setText( tr( "Hidden" ) );
pCheckBoxIsReadable = new QCheckBox();
pCheckBoxIsReadable->setText( tr( "Readable" ) );
pCheckBoxIsWritable = new QCheckBox();
pCheckBoxIsWritable->setText( tr( "Writable" ) );
pCheckBoxI***ecutable = new QCheckBox();
pCheckBoxI***ecutable->setText( tr( "Executable" ) );
pPushButtonGet = new QPushButton();
pPushButtonGet->setText( tr( "Get FileInformation" ) );
QGridLayout * pLayout = new QGridLayout( this );
pLayout->addWidget( pLabelFileName, 0, 0, 1, 2 );
pLayout->addWidget( pLineEditFileName, 0, 2, 1, 4 );
pLayout->addWidget( pPushButtonFile, 0, 6, 1, 1 );
pLayout->addWidget( pLabelSize, 1, 0, 1, 2 );
pLayout->addWidget( pLineEditSize, 1, 2, 1, 5 );
pLayout->addWidget( pLabelCreated, 2, 0, 1, 2 );
pLayout->addWidget( pLineEditCreated, 2, 2, 1, 5 );
pLayout->addWidget( pLabelLastModified, 3, 0, 1, 2 );
pLayout->addWidget( pLineEditLastModified, 3, 2, 1, 5 );
pLayout->addWidget( pLabelLastRead, 4, 0, 1, 2 );
pLayout->addWidget( pLineEditLastRead, 4, 2, 1, 5 );
pLayout->addWidget( pLabelProperty, 5, 0, 1, 2 );
pLayout->addWidget( pCheckBoxIsDir, 6, 0 );
pLayout->addWidget( pCheckBoxIsFile, 6, 1 );
pLayout->addWidget( pCheckBoxIsSymLink, 6, 2 );
pLayout->addWidget( pCheckBoxIsHidden, 6, 3 );
pLayout->addWidget( pCheckBoxIsReadable, 6, 4 );
pLayout->addWidget( pCheckBoxIsWritable, 6, 5 );
pLayout->addWidget( pCheckBoxI***ecutable, 6, 6 );
pLayout->addWidget( pPushButtonGet, 7, 0, 1, 7 );
setLayout( pLayout );
connect( pPushButtonFile, SIGNAL( clicked() ), this, SLOT( slotFile() ) );
connect( pPushButtonGet, SIGNAL( clicked() ), this, SLOT( slotGet() ) );
}
fileInfo::~fileInfo()
{
}
void fileInfo::slotFile()
{
QString sFilePath = QFileDialog::getOpenFileName( this, "open file dialog", "", "file (*)" );
pLineEditFileName->setText( sFilePath.toAscii() );
}
void fileInfo::slotGet()
{
getFileInformation( pLineEditFileName->text() );
}
void fileInfo::getFileInformation( QString file )
{
QFileInfo info( file );
pLineEditSize->setText( QString::number( info.size() ) );
pLineEditCreated->setText( info.created().toString() );
pLineEditLastModified->setText( info.lastModified().toString() );
pLineEditLastRead->setText( info.lastRead().toString() );
pCheckBoxIsDir->setCheckState( info.isDir() ? Qt::Checked : Qt::Unchecked );
pCheckBoxIsFile->setCheckState( info.isFile() ? Qt::Checked : Qt::Unchecked );
pCheckBoxIsSymLink->setCheckState( info.isSymLink() ? Qt::Checked : Qt::Unchecked );
pCheckBoxIsHidden->setCheckState( info.isHidden() ? Qt::Checked : Qt::Unchecked );
pCheckBoxIsReadable->setCheckState( info.isReadable() ? Qt::Checked : Qt::Unchecked );
pCheckBoxIsWritable->setCheckState( info.isWritable() ? Qt::Checked : Qt::Unchecked );
pCheckBoxI***ecutable->setCheckState( info.i***ecutable() ? Qt::Checked : Qt::Unchecked );
}