Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5719006
  • 博文数量: 409
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 8273
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-23 19:15
个人简介

qq:78080458 学习交流群:150633458

文章分类

全部博文(409)

文章存档

2019年(127)

2018年(130)

2016年(20)

2015年(60)

2014年(41)

2013年(31)

分类: 嵌入式

2016-02-02 13:41:28

一、QT目录操作
1、Qt提供了QFile类来实现文件的操作,为了更方便的处理文本和二进制,还提供的QTextStream类QDataStream类。处理目录可以使用QDir
2、QDir类具有存取目录结构和内容的能力,使用QDir可以操作、存取目录、文件信息、底层文件系统,还可以存取Qt的资源文件
3、QDir可以使用相对路径也可以使用绝对路径,你可以使用isRelative() isAbsolute()函数来检测使用的是相对路径还是绝对路径。
4、使用makeAbsolute()将一个相对路径最为绝对路径
5、使用isReadable() isRoot()来测试给定目录的属性
6、目录的路径可以通过path()函数返回,setPath函数设置新的路径,使用absolutePath函数返回绝对路径。
7、使用dirName函数返回目录名,它通常返回绝对路径的最后一个元素。
8、mkDir创建目录
9、exists函数判断目录是否可用,count返回目录下的条目个数,entrylist返回目录下所有元素组成的列表,rmdir删除目录,remove删除文件
10、separator函数返回目录的分隔符
11、QFileInfo类,提供了一个与系统无关的文件信息,包含文件名、路径、存取权限、文件大小、修改时间等等
12、为了提高效率,QFileInfo将文件信息进行了缓存,如果希望每次都从文件系统读取信息,那么使用setCaching(false)来关闭缓存
13、文件名可以通过path和fileName来分解,fileName返回的部分可以通过baseName和extension来获取主文件名和扩展名
14、created函数返回文件创建日期,lastModified返回最后一次修改日期,lastRead返回最后一次读取事件
15、文件的读写权限由isReadable() isWriteable() isExecuteable()来获取
16、文件的属主可以通过owner() group() ownerId() groupId() 来获取
17、permission()函数可以将文件的权限和属主一起读取出来


二、 QT文件读取
QDataStream类可以将二进制数据串行化,同时实现了c++数据的串行化
QTextStream可以很方便的读写文本数据
Qt读写文件的方式:
1、根据文件名实例化一个QFile类
QFile file("123.txt");
2、使用QFile的open方法打开文件
file.open(QIODevice::ReadOnly);
file.open(QIODevice::ReadWrite);
file.open(QIODevice::WriteOnly);
3、得到一个输入或者输出流
QTextStream out(&file)
4、写入或者读出数据
out<<"hello\n"<<"world\n";
 


三、案列
1、目录操作
头文件

点击(此处)折叠或打开

  1. #ifndef MYDIALOG_H
  2. #define MYDIALOG_H

  3. #include <QtGui>

  4. class myDialog : public QDialog
  5. {
  6. Q_OBJECT
  7. public:
  8.     myDialog();
  9. private:
  10.     QLineEdit *fileNameEdit;
  11.     QPushButton *openBtn, *getBtn;
  12.     QTextEdit *infoText;
  13.     QCheckBox *dir, *readable, *writeable, *executeable;
  14. public slots:
  15.     void openFile();
  16.     void getInfo();
  17. };

  18. #endif
cpp

点击(此处)折叠或打开

  1. #include "myDialog.h"
  2. #include <QFileInfo>

  3. myDialog::myDialog()
  4. {
  5.     //控件初始化
  6.     fileNameEdit = new QLineEdit();
  7.     openBtn = new QPushButton(tr("打开"));
  8.     infoText = new QTextEdit();
  9.     getBtn = new QPushButton(tr("获取文件信息"));
  10.     dir = new QCheckBox(tr("目录"));
  11.     readable = new QCheckBox(tr("可读"));
  12.     writeable = new QCheckBox(tr("可 写"));
  13.     executeable = new QCheckBox(tr("可执行"));

  14.     //布局
  15.     QHBoxLayout *hLay0 = new QHBoxLayout();
  16.     hLay0->addWidget(new QLabel(tr("文件名")));
  17.     hLay0->addWidget(fileNameEdit);
  18.     hLay0->addWidget(openBtn);
  19.     
  20.     QHBoxLayout *hLay1 = new QHBoxLayout();
  21.     hLay1->addWidget(dir);
  22.     hLay1->addWidget(readable);
  23.     hLay1->addWidget(writeable);
  24.     hLay1->addWidget(executeable);

  25.     QVBoxLayout *vLay = new QVBoxLayout();
  26.     vLay->addLayout(hLay0);
  27.     vLay->addWidget(infoText);
  28.     vLay->addLayout(hLay1);
  29.     vLay->addWidget(getBtn);

  30.     //绑定信号和槽函数
  31.     connect(openBtn, SIGNAL(clicked()), this, SLOT(openFile()));
  32.     connect(getBtn, SIGNAL(clicked()), this, SLOT(getInfo()));
  33.     setLayout(vLay);
  34. }

  35. void myDialog::openFile()
  36. {
  37.     QString path = QFileDialog::getOpenFileName(this, tr("选择文件"), "/", "file(*)");
  38.     fileNameEdit->setText(path);
  39. }    
  40. void myDialog::getInfo()
  41. {
  42.     //fileNameEdit infoText dir readable writeable executeable
  43.     //获取QFileInfo对象
  44.     QFileInfo fileInfo(fileNameEdit->text());
  45.     //清空所有的信息
  46.     infoText->clear();
  47.     //从QFileinfo对象中获取信息
  48.     qint64 size = fileInfo.size();
  49.     QDateTime created = fileInfo.created();
  50.     QDateTime lastModified = fileInfo.lastModified();
  51.     QDateTime lastRead = fileInfo.lastRead();
  52.     bool isDir = fileInfo.isDir();
  53.     bool isReadable = fileInfo.isReadable();
  54.     bool isWriteable = fileInfo.isWritable();
  55.     bool isExecutable = fileInfo.isExecutable();

  56.     //加载所有信息
  57.     infoText->append(QString(tr("file size: %1\n")).arg(size));
  58.     infoText->append(QString(tr("file create time: %1\n")).arg(created.toString()));
  59.     infoText->append(QString(tr("file lastModified time: %1\n")).arg(lastModified.toString()));
  60.     infoText->append(QString(tr("file lastRead time: %1\n")).arg(lastRead.toString()));
  61.     //set checkbox
  62.     dir->setCheckState(isDir?Qt::Checked :Qt::Unchecked);
  63.     readable->setCheckState(isReadable?Qt::Checked :Qt::Unchecked);
  64.     writeable->setCheckState(isWriteable?Qt::Checked :Qt::Unchecked);
  65.     executeable->setCheckState(isExecutable?Qt::Checked :Qt::Unchecked);
  66. }


2、文件操作

点击(此处)折叠或打开

  1. #include <QApplication>
  2. #include <QIODevice>
  3. #include <QFile>
  4. #include <QDataStream>
  5. #include <QtDebug>

  6. int main(int argc, char *argv[])
  7. {
  8.     QApplication app(argc, argv);

  9.     QFile file("123.txt");
  10.     file.open(QIODevice::WriteOnly);
  11.     QDataStream out(&file);
  12.     out<<"hello world\n";
  13.     file.close();

  14.     file.open(QIODevice::ReadOnly);
  15.     QDataStream in(&file);
  16.     QString str;
  17.     in>>str;
  18.     qDebug()<<str;
  19.     file.close();

  20.     return app.exec();
  21. }


阅读(1300) | 评论(0) | 转发(0) |
0

上一篇:lesson7-容器类

下一篇:lesson9-QT事件

给主人留下些什么吧!~~