读取QDataStream中的特定对象并计算存储的对象数 [英] Reading specific object in QDataStream and count number of objects stored

查看:312
本文介绍了读取QDataStream中的特定对象并计算存储的对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些对象写入二进制文件中,我想将它们读回. 为了向您解释我要做什么,我准备了一个简单的示例,其中包含User类,其中包含QString名称和childs的QList名称.请参见下面的代码.

I am writting some objects in a binary file and I would like to read them back. To explain you what I am trying to do, I prepared a simple example with a class User that contains the QString name and QList name of childrens. Please see the code below.

#include "QString"
#include "QFile"
#include "QDataStream"
#include "qdebug.h"

class User
{
protected:
QString name;
QList<QString> childrens;

public:
QString getName(){ return name;}
QList<QString> getChildrens(){ return childrens;}

void setName(QString x) {name = x;}
void setChildrens(QList<QString> x) {childrens = x;}

//I have no idea of how to get the number of users in "test.db"
int countDatabase()
{

}

//I would like to read the user named "pn" without putting all users in memory
void read(QString pn)
{
    QFile fileRead("test.db");
    if (!fileRead.open(QIODevice::ReadOnly)) {
        qDebug() << "Cannot open file for writing: test.db";
        return;
    }
    QDataStream in(&fileRead);
    in.setVersion(QDataStream::Qt_5_14);
    in>>*this;
}


void write()
{
    QFile file("test.db");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
        qDebug() << "Cannot open file for writing: test.db";
        return;
    }
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_14);
    out<<*this;
}

friend QDataStream &operator<<(QDataStream &out, const User &t)
{
    out << t.name << t.childrens;
    return out;
}

friend QDataStream &operator>>(QDataStream &in, User &t)
{
    QString inname;
    QList<QString> inchildrens;
    in >> inname >> inchildrens;
    t.name = inname;
    t.childrens = inchildrens;
    return in;
}

};


////////////////////////////////////////////////////////////////
int main()
{
    User u;
    u.setName("Georges");
    u.setChildrens(QList<QString>()<<"Jeanne"<<"Jean");
    u.write();

    User v;
    u.setName("Alex");
    u.setChildrens(QList<QString>()<<"Matthew");
    u.write();

    User w;
    w.setName("Mario"); // no children
    w.write();

    User to_read;
    to_read.read("Alex");

    qDebug()<<to_read.getName();
    return 0;
}

我成功地在二进制文件中写入了所有想要的用户.但是,我希望能够在不将所有内容加载到内存的情况下:

I successfully write all the users I want in my binary file. However, I would like to be able, without loading everything in memory:

  • 要知道二进制文件中存储了多少用户,
  • 通过提供此用户的名称来读取用户.

到目前为止,我一直使用QDataStream,而<<和>>运算符进行序列化.也许我想要的是用这种方法无法实现的.您能为我提供一些使用QDataStream或其他方法成功的提示吗?

I have used until now a QDataStream and I am overloading the << and >> operators for the serialization. Maybe what I want is not possible with this method. Could you please provide me some hints to succeed with QDataStream or some other methods?

推荐答案

请在此处找到最终不需要二进制文件但在SQL db中使用BLOB的解决方案:

Please find a solution here that finally did not required binary files but uses a BLOB in a SQL db:

这篇关于读取QDataStream中的特定对象并计算存储的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆