缓存区操作与目录操作

Qt 中缓冲区的概念

缓冲区的本质是一段连续的存储空间

QBuffer 是 Qt 中缓冲区相关的类

在 Qt 中可以将缓冲区看作一种特殊的 IO 设备

文件辅助流可直接用于操作缓冲区

QBuffer 缓冲区的使用方法

QBuffer 缓冲区的使用场合

1. 在线程间进行不同类型的数据传递

2. 缓存外部设备中的数据返回

3. 数据读取速度小于数据写入速度

缓冲区操作

void write_buffer(int type, QBuffer &buffer)
{
    QDataStream out(&buffer);

    if(buffer.open(QIODevice::WriteOnly))
    {
        out << type;

        if(type == 0)
        {
            out << QString("liujie");
            out << QString("3.14");
        }
        else if(type == 1)
        {
            out << 3;
            out << 1592;
        }
        else if(type == 2)
        {
            out << 3.1415;
        }

        buffer.close();
    }
}

void read_buffer(QBuffer &buffer)
{
    QDataStream in(&buffer);

    if(buffer.open(QIODevice::ReadOnly))
    {
        int type = -1;

        in >> type;

        if(type == 0)
        {
            QString s1("");
            QString s2("");

            in >> s1;
            in >> s2;

            qDebug() << s1;
            qDebug() << s2;
        }
        else if(type == 1)
        {
            int a = 0;
            int b = 0;

            in >> a;
            in >> b;

            qDebug() << a;
            qDebug() << b;
        }
        else if(type == 2)
        {
            double d = 0;

            in >> d;

            qDebug() << d;
        }

        buffer.close();
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QByteArray array;
    QBuffer buffer(&array);

    write_buffer(0, buffer);
    read_buffer(buffer);

    return a.exec();
}

运行结果如下图所示

QDir 是 Qt 中功能强大的目录操作类

Qt 中的目录分隔符统一使用 '/'

QDir 能够对目录进行任意操作 (创建、删除、重命名)

QDir 能够获取指定目录中的所有条目 (文件和文件夹)

QDir 能够使用过滤字符串获取指定条目

QDir 能够获取系统中的所有根目录

目录操作基础示例

目录操作示例

void test_dir()
{
    QString path("C:/Users/Administrator/Desktop/QDir");
    QDir dir(path);

    if(!dir.exists())
    {
        dir.mkdir(path);
    }
    if(dir.exists())
    {
        dir.cd(path);

        QStringList list = dir.entryList();

        for(int i = 0; i < list.count(); i++)
        {
            qDebug() << list[i];
        }
    }
}

unsigned int calculate_size(QString path)
{
    int ret = 0;
    QFileInfo info(path);

    if(info.isFile())
    {
        ret = info.size();
    }
    else if(info.isDir())
    {
        QDir dir(path);
        QFileInfoList list = dir.entryInfoList();

        for(int i = 0; i < list.count(); i++)
        {
            if((list[i].fileName() != ".") && (list[i].fileName() != ".."))
            {
                ret += calculate_size(list[i].absoluteFilePath());
            }
        }
    }
    return ret;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    test_dir();
    qDebug() << calculate_size("C:/Users/Administrator/Desktop/QDir");

    return a.exec();
}

运行结果如下图所示

和该文件夹的大小相同

QFileSystemWatcher 用于监控文件和目录的状态变化

能够监控特定目录和文件的状态

能够同时对多个目录和文件进行监控

当目录或文件发生改变时将触发信号

可以通过信号与槽的机制捕捉信号并作出响应

文件监控示例

class Watcher : public QObject
{
    Q_OBJECT

public:
    explicit Watcher(QObject *parent = nullptr);
    void addPath(QString path);

private:
    QFileSystemWatcher watcher;

private slots:
    void statusChanged(const QString &path);
};

Watcher::Watcher(QObject *parent) : QObject(parent)
{
    connect(&watcher, &QFileSystemWatcher::fileChanged, this, &Watcher::statusChanged);
    connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &Watcher::statusChanged);
}

void Watcher::statusChanged(const QString &path)
{
    qDebug() << path << " status changed";
}

void Watcher::addPath(QString path)
{
    watcher.addPath(path);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Watcher w;

    w.addPath("C:/Users/Administrator/Desktop/test.txt");
    w.addPath("C:/Users/Administrator/Desktop/QDir");

    return a.exec();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值