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 );

}

qt mysql显示文件名字_【实例】Qt获取文件属性相关推荐

  1. qt mysql 系统时间_使用QT和参数在SQLite数据库中插入日期时间

    我想从C++/QT应用程序执行插入到SQLite数据库. 我想要插入数据的表格中的一列是datetime(yyyy-mm-dd hh:mm:ss).使用QT和参数在SQLite数据库中插入日期时间 我 ...

  2. qt连接mysql创建表_用Qt访问数据库写一个 表格

    访问数据库,将数据填充数据表格大致样子如下: 该怎么实现呢. 首先创建一个数据库模型的对象:QSqlQueryModel *model = new QSqlQueryModel; 然后提取数据库数据: ...

  3. qt web混合编程_基于Qt与MATLAB的混合编程技术

    摘要:在Qt雷达仿真系统中,数据处理及图形显示尤为重要.本文为此提出了一种Qt与MATLAB混合编程的方法.通过VC++和MATLAB混合编程技术,将MATLAB函数封装成动态链接库,Qt调用这个动态 ...

  4. qt 历史记录控件_基于Qt图形界面软件的操作日志记录方法及系统_2015106293015_说明书_专利查询_专利网_钻瓜专利网...

    技术领域 本发明涉及一种软件系统的日志记录技术,特别涉及一种基于Qt图形界面软件的操作日志记录方法及系统. 背景技术 软件操作日志是记录用户在使用软件的过程中,通过鼠标和键盘在操作界面上执行的点击和输 ...

  5. qt控制程序打开记事本_基于QT记事本应用程序开发.doc

    基于QT记事本应用程序开发 基于QT记事本应用程序开发 [摘要]本文通过对嵌入式Linux和Qt的分析,利用Qt在源代码级上能够实现跨平台特性,在源代码开放的Linux操作系统上,根据嵌入式应用的特点 ...

  6. qt制作一个画板_基于Qt的画图板的设计与实现(含录像)

    基于Qt的画图板的设计与实现(含录像)(任务书,开题报告,外文翻译,毕业论文20000字,程序代码,答辩PPT,答辩视频录像) 摘要 本文的主要内容是记述画图板的设计与实现课程设计中的一些关键技术和辅 ...

  7. ubuntu qt编译mysql报错_[Linux]QT编译Mysql驱动(Mariadb驱动),解决qmake报错问题

    编译环境 系统:Ubuntu18.04 QT版本:Qt 5.14.0 gcc版本:gcc version 7.5.0 qt官方编译驱动方式 在使用QT连接数据库时遇见的第一个错误就是QMYSQL dr ...

  8. qt 启动画面显示图片_用QT实现软件启动画面的效果,学习参考!

    不少时候,软件在彻底启动以前要作一些准备,这个时候显示一个画面,告诉用户软件在干啥,让用户知道软件启动了,不是死机了. Qt中有现成的Api共咱们使用,实现启动画面要用的头文件是 #include 这 ...

  9. qt 串口粘包_用Qt 5写一个串口收发桌面工具

    今天用Qt写了一个串口收发工具,记录下整个流程. 1.项目工程布局 2.ui文件布局 3.widget.h文件 #ifndef WIDGET_H #define WIDGET_H #include # ...

最新文章

  1. 源代码查看工具 Source Navigator 使用心得
  2. 【转】C语言的memset函数
  3. C#中将字符串内容换行写入到txt文件中
  4. 关于异步IO与同步IO的写操作区别
  5. 数学奥赛用不用计算机,报考自招必看!五大学科竞赛利弊详解,到底哪科最适合你?...
  6. scrapy使用cookie的三种方式
  7. 北京邮电大学c语言按要求输出_C语言经典100例004-统计各个年龄阶段的人数
  8. toolStrip 按钮图片大小的修改
  9. hsrp 切换_HSRP、VRRP、GLBP | 网络工程师之网关高可用、冗余
  10. Wijmo 5 与Breeze 的组合,及与METRONIC 的集成
  11. 简易CPU的C++实现
  12. 深入浅出 MFC_华中理 工_简体版电子书pdf下载
  13. java-net-php-python-jsp安利达物流公司管理系统计算机毕业设计程序
  14. 学计算机的 1加1,如何用“一加一等于几”解析人生
  15. 技术宅日记:机器学习修炼的每一步
  16. Android UI开发:AlertDialog对话框
  17. dotnet publish 不生成pdb文件
  18. gateway中的局部过滤器_GateWay过滤器
  19. 论文精读:Selective Convolutional Descriptor Aggregation
  20. 行业品牌监测报告|《中国汽车产业舆情报告2022(上半年)》

热门文章

  1. 多线程--GCD的基本用法
  2. Android中项目中各个文件夹的含义和用途详解
  3. 额外域建立FSMO角色转移及夺取
  4. 常用汉字的unicode 编码
  5. golang 指定范围 生成随机数
  6. golang os.Rename 移动文件 报错 invalid cross-device link 解决方法
  7. linux 内核参数somaxconn TCP监听队列长度
  8. golang 映射 map 简介
  9. linux挂载卸载不掉 umount target is busy
  10. 3W法—what,why,how的运用