How to write and read a QList with binary files
-
wrote on 16 Sept 2014, 16:41 last edited by
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
-
wrote on 16 Sept 2014, 17:06 last edited by
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
-
wrote on 16 Sept 2014, 18:11 last edited by
yes I tried but gives me an error:
no match for 'operator>>' (operand types are 'QDataStream' and 'car*')
s >> t;
^ -
wrote on 16 Sept 2014, 18:23 last edited by
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.
-
wrote on 16 Sept 2014, 18:24 last edited by
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. -
wrote on 16 Sept 2014, 18:36 last edited by
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);
^ -
wrote on 16 Sept 2014, 18:46 last edited by
and sorry for all this questions and stuff is just I'm new in this
-
wrote on 16 Sept 2014, 18:49 last edited by
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. -
wrote on 16 Sept 2014, 18:54 last edited by
[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. -
wrote on 16 Sept 2014, 19:22 last edited by
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?
-
wrote on 16 Sept 2014, 23:26 last edited by
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;
}@ -
wrote on 16 Sept 2014, 23:58 last edited by
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 -
wrote on 17 Sept 2014, 00:07 last edited by
[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. -
-
wrote on 17 Sept 2014, 00:18 last edited by
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 starthere is where I write
@QFile file("register.txt");
if (file.open(QIODevice::WriteOnly))
{
QDataStream out(&file);
out << cars;
file.close();
}@ -
wrote on 17 Sept 2014, 01:44 last edited by
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 -
wrote on 18 Sept 2014, 22:17 last edited by
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 ?
10/16