![]() |
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: |
Re: QDataStream ad QList problem!
Quote:
Code:
| pointer | pointer | pointer | |
Re: QDataStream ad QList problem!
Quote:
When you append an item to a collection in such a way, or assign it to a variable, it gets copied. (Its copy constructor or operator= gets called.) Eg. Code:
MyClass item; //creating a new item You need to tell HOW it is gonna be copied. That's why there is a copy constructor or an operator=. Basically, Code:
item2 = item; Code:
item2.operator=(item); Code:
MyClass::operator=(const MyClass& other) { ... } And Code:
MyClass item2 = item; Code:
MyClass item2(item); Code:
MyClass::MyClass(const MyClass& other) { ... } |
Re: QDataStream ad QList problem!
Quote:
ok that's the point, i've a list of Service in my main classes. i've to share this list with other classes, like "EditService". how can i pass list, or elements, to other class in order to make them change original list?! :( with the old list of pointer it worked well(they actually changed value of original list), but as u said save pointer doesn't make sense. so what i'm supposed to do?! back with pointer list changing saving function?! or there are other solutions!? :o |
Re: QDataStream ad QList problem!
Quote:
BTW, Qt's collections give back const references to items stored within them. Quote:
Note that QList<Sevice*> is NOT QList<Service>* !! (BTW, I edited my previous post to make it more clear how C++ works.) |
Re: QDataStream ad QList problem!
Quote:
gri said to not use list in heap, so no QList<Service>* or not!? :o if i use it like this(not real code): main class: QList<Service> * services; EditService edit(services); EditService class: Service service = services.at(0); service.changeSomething(); service in QList has not be really changed...am i right!? so the only solution is user a list of pointer :confused: |
Re: QDataStream ad QList problem!
Quote:
Okay. You have a pointer to a list that contains items. If you give a pointer to someone, it can do whatever it wants to the memory where the pointer points to. So, basically: yes, you can change the collection this way. Again: Feel free to check how I do this in my own app. (Hint: CharacterListWindow, EveRefresher, and EveConnector all share a QList<EveCharacter>* and do stuff with it all the time.) |
Re: QDataStream ad QList problem!
ok i'll give a look to your app! thanks again, i'll let you know ;)
|
Re: QDataStream ad QList problem!
i dunno how, but now it works :P
jk, i know how...i've change a bit the original code :) Now i've a QList<Service*> services, so no list in the heap(as gri suggested). I still have a list of pointer but change code in order to save object pointed instead of pointer(as it logically should be). In service class now i've: Q_DECLARE_METATYPE(Service); Q_DECLARE_METATYPE(Service*); the first is necessary to save object: Code:
QByteArray array; Code:
QVariant var = settings->value("services"); Code:
QListIterator<Service*> i(services); Code:
Service *service= new Service(); maybe it's not pretty elegant, but works well :) thanks to all for help :) ps. venemo your code will be usefull when i'll get into "user interface", i've seen interesting code about rotation management ;) |
Re: QDataStream ad QList problem!
Quote:
https://garage.maemo.org/plugins/scm...ms&view=markup (usage) https://garage.maemo.org/plugins/scm...ms&view=markup (orientation helper class) Screenshots: http://gri.not-censored.com/orientat...horizontal.png http://gri.not-censored.com/orientat...t/vertical.png |
Re: QDataStream ad QList problem!
really nice code gri, i think i'm gonna try it :P
btw i've a question about ui...how to set window size?! i mean max res is 800*480 or 480*800 but what's about status bar?! :S |
Re: QDataStream ad QList problem!
Quote:
|
Re: QDataStream ad QList problem!
mmm so what's about dialogs(or widget) size?! should i've to fix it?! :o
|
Re: QDataStream ad QList problem!
Quote:
Just open the Qt Designer and play around with layouting (there are tool buttons for that). You will also need to understand the sizePolicy options. That's all you need. No pixel coordinates or something else :) |
Re: QDataStream ad QList problem!
i guess layout will do everything about screensize!
btw once app's core is done i'll start move around in order to create a "cute" ui :) thanks again! |
Re: QDataStream ad QList problem!
could you suggest me some guide about ui creation!? i've found a lot of documents but i'd like to found a good one to learn how to create ui ;)
|
Re: QDataStream ad QList problem!
Quote:
http://doc.qt.nokia.com/qt-maemo-4.6...d-layouts.html http://doc.qt.nokia.com/qt-maemo-4.6...s-layouts.html |
Re: QDataStream ad QList problem!
thanks for these links! btw i still don't understand how set items size properly in order to fix them in main widget :O
where can i find a real example!? i mean a *.ui file from some app :O EDIT. ok i've found a good one :p |
All times are GMT. The time now is 00:59. |
vBulletin® Version 3.8.8