How to write and read a QList with binary files



  • I'm stuck when i tried to read a qlist inside a binary file someone can show me how to do it because I've been trying to do it, but I cant



  • Have you tried to write the list to a file using the operator >> to a QDataStream?

    "http://qt-project.org/doc/qt-5/qlist.html#operator-gt-gt-61":http://qt-project.org/doc/qt-5/qlist.html#operator-gt-gt-61



  • yes I tried but gives me an error:

    no match for 'operator>>' (operand types are 'QDataStream' and 'car*')
    s >> t;
    ^



  • Yes, it's written in the doc:

    bq. This function requires the value type to implement operator>>()

    You have to define the operator, natively the compiler doesn't know how to write your type:

    @QDataStream & operator<<(QDataStream & stream, const MyType & myVar)@

    And don't forget, the pointer is just a pointer (so it's just an address), you should dereference it, but it's up to you when you define your operator<<(). If your QList is defined with pointers, you will define operator<<() with pointer as the second type.



  • You need to define the stream operators for your class
    @
    QDataStream & operator>>(QDataStream & in, car* aCar)
    QDataStream & operator<<(QDataStream & out, car* aCar)
    @
    QList will call the stream operator for each its element.



  • Hey, I do what you say but now gives me this error

    QDataStream& car::operator>>(QDataStream&, car*)' must take exactly one argument
    QDataStream & operator>>(QDataStream & in, car* aCar);
    ^



  • and sorry for all this questions and stuff is just I'm new in this



  • If you define operator>> as a member of a class then you need only one parameter QDataStream&
    If you define operator as an external function then you need two.
    QList uses external functions because QList does not know anything about its content except of a type.
    Move the operator declarations out of car class and it should work.



  • [quote author="ricardo210" date="1410893187"]and sorry for all this questions and stuff is just I'm new in this[/quote]
    This forum was created to help the developers who uses Qt to find the answers on their ultimate questions. So, without the questions the forum will die.



  • what woul happend if i want to write a qlist of objects who have a qlist inside them on the binary file?

    should I overload all the <<and>> for all the classes?



  • hey, guys I almost end my program but givesme an error qhen i tried to take out a qlist from one of my variables car here is the code who is giving me problesma;
    @QDataStream& operator>>(QDataStream &out, car *&list)
    {
    QString w,x,y,z;
    out>>w;
    out>>x;
    out>>y;
    out>>z;
    out>>list->register;
    list->plate=w;
    list->brand=x;
    list->=mileage.toDouble();
    list->=cylinder_capacity.toDouble();
    return out;
    }
    @
    Here is it where I overload

    @QDataStream& operator<<(QDataStream &out, QList<gasoline*> &list)
    {
    while (!out.atEnd())
    {
    gasoline* petrol = new gasoline();
    out >> petrol;
    list.append(petrol);
    }
    return out;
    }@



  • when i tried to read te file givesme an error

    @ QFile read("register.txt");
    if(read.exists()){
    if (read.open(QIODevice::ReadOnly)){;
    QDataStream in(&read);
    in >> cars;
    read.close();
    }
    }@
    here is when i am reading
    cars is my qlist of car



  • [quote]but givesme an error[/quote]
    Could you post an error message here.

    I see couple things here

    • these lines should give an error on compilation
      @
      list->=mileage.toDouble();
      list->=cylinder_capacity.toDouble();
      @

    • here you defined operator for output but trying to read from the stream
      @
      QDataStream& operator<<(QDataStream &out, QList<gasoline*> &list)
      {
      ...
      out >> petrol;
      ...
      }
      @

    You don't need to define stream operator for QList<gasoline*>
    As "Max13 said":http://qt-project.org/forums/viewthread/47469/#194374 you need to define a stream operator for gasoline.
    QList will use it.



  • ok thanks for the information i just do what you said but still when i run the program qt says:
    The program has unexpectedly finished.
    and I dont know if this error is because I'm savin wrong at the end of the program or I'm reading wrong the binary file at the start

    here is where I write
    @QFile file("register.txt");
    if (file.open(QIODevice::WriteOnly))
    {
    QDataStream out(&file);
    out << cars;
    file.close();
    }@



  • it seem what the error is in this line
    @QDataStream& operator>>(QDataStream &out, car *&list)
    {
    QString w,x,y,z;
    out>>w;
    out>>x;
    out>>y;
    out>>z;
    out>>list->register; //<--- It seems this is the line with the error
    list->plate=w;
    list->brand=x;
    list->mileage=y.toDouble();
    list->cylinder_capacity=z.toDouble();
    return out;
    }@
    in this part register is a qlist too



  • When you want to write/read a QList using operator>>()/operator<<(), as the doc says, the type of the QList must also implement the SAME operator.

    Which means that except Qt's included types (quint, QString, QByteArray, ...), you need to provide such an operator for your type.

    I see you have provided one, and you're pointing out a problem when using it on another QList (line 8: list->register). Is there a stream operator defined for the type of element in list->register ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.