![]() |
QDataStream ad QList problem!
hi there!
i'm working with qt nokia sdk and i'm blocked with a strange issue with list serialization! I've a QList of objects called Service(pointer of Service, QList<Service*>), and i've to store it in a QSetting file using a QDataStream. I've already overloaded << and >> operator for Service objects and they work well. Now i'm trying to save all the list, but: Quote:
here some snap of my code: Code:
#ifndef SERVICE_H Code:
#include "service.h" load: Code:
QVariant var = settings->value("services"); Code:
QByteArray array; QList<Service*> *services; i hope u can help me :o |
Re: QDataStream ad QList problem!
First, please make the writing function const.
Code:
QDataStream &operator<<(QDataStream &out, const Service &service); Code:
QList<Service> services; Code:
QList<Service*> services; Quote:
Code:
mystream << *services; |
Re: QDataStream ad QList problem!
Quote:
Code:
error: passing ‘const Service’ as ‘this’ argument of ‘QString Service::getName()’ discards qualifiers getName() const { return name;} Quote:
Quote:
Quote:
|
Re: QDataStream ad QList problem!
Quote:
But, when the memory is deallocated (using delete or leaving the current scope) the pointer points at memory that you can not access without triggering an error. References share some of the pointer semantics but have some extra constraints and properties. For one, a reference always has to have a value that can be referenced. (Hence the name.) |
Re: QDataStream ad QList problem!
Hey,
I also encountered the exact same situation. Gri and the others wrote down everything well, but let me add a few side notes. You can basically serialize a QList<T> if T is serializable. (Meaning: it has a << operator.) I wrote for myself a class called QSettingsManager for this purpose. You can view the code here and the entire app in this repository. Feel free to use it in your app, too. There are also examples for serialization, too. The relevant part is this: Code:
template<typename T> Code:
QList<QString> myList; |
Re: QDataStream ad QList problem!
Code:
error: passing ‘const Service’ as ‘this’ argument of ‘QString Service::getName()’ discards qualifiers Quote:
Code:
Service service; Quote:
Quote:
|
Re: QDataStream ad QList problem!
thanks for explanation joorin :o
Quote:
Quote:
http://vcs.maemo.org/svn/eve-watcher...ngsmanager.cpp now i'm going to try this: 1) put away QList<> from the heap 2) try to use QList<Service> instead of QList<Service*> 3) if 2 won't work, i'll try to save element by element. @gri, if i can use non-pointer to do that...i wonder...when a pointer is necessary!? :confused: |
Re: QDataStream ad QList problem!
Quote:
(The template methods which I quoted earlier are in the header file.) You can check out the entire app so you can see how it works, if you wish. Quote:
It is (basically) a 32-bit integer of 32-bit machines and a 64-bit integer on 64-bit machines. Because of this, every pointer is "serializable". Still, you don't gain anything by storing a random memory address. Why? 1. There is no guarantee that the same address will be yours next time. 2. The data stored in that address is erased when your app is closed, so no point in storing its address anyway. Solution: Store the data itself, not its address. Quote:
This simple task is not one of them. |
Re: QDataStream ad QList problem!
This isn't directly related to you question, but it might explain something.
Code:
int i = 17; Using this pointer, you can change the stored value: Code:
*pointer_to_int = 4711; Continuing from above, you can extend this indirection: Code:
int **pointer_to_pointer_to_int = &pointer_to_int; Code:
int do_stuff(char *foo, char **error) { References can in most cases be seen as lacking the second level of indirection. The compiler imposes limitations on what you can do with a reference and dictates when they have to be initialized. It is not interesting to change references like pointers because the reference already gives you direct access to whatever it references If you play around with it a bit, you'll understand more. Take the time to look at one of the many introductions to C++ online. |
Re: QDataStream ad QList problem!
ok with 2) i've solve problem with >> operator...but anything else is working xD
i'm sure is a reference/pointer stuff. i mean, i'm adding to my QList<Service> a reference Service service in a method called "addItems". Will it be this reference deleted once the addItems is over even if it's now linked in my list?! so should i use Service * service = new Service() and then add with list->append(*service) ?! :confused::confused::confused: |
All times are GMT. The time now is 02:10. |
vBulletin® Version 3.8.8