Reading specific object in QDataStream and count number of objects stored

不问归期 提交于 2020-03-05 00:29:08

问题


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:

  • To know how many users are stored in the binary file,
  • To read a user by giving the name of this user.

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?


回答1:


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

SOLUTION



来源:https://stackoverflow.com/questions/60162120/reading-specific-object-in-qdatastream-and-count-number-of-objects-stored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!