|
|
2010-06-03
, 21:49
|
|
Posts: 190 |
Thanked: 129 times |
Joined on Mar 2010
@ Bavaria, Germany
|
#2
|
QDataStream &operator<<(QDataStream &out, const Service &service); QDataStream &operator>>(QDataStream &in, Service &service);
QList<Service> services; mystream << services;
QList<Service*> services; mystream << services.size(); foreach(Service* service, services) mystream << *service;

the strange thing is that << seems to work with QList<Service*>!
mystream << *services;
|
|
2010-06-03
, 22:00
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#3
|
First, please make the writing function const.
Code:QDataStream &operator<<(QDataStream &out, const Service &service); QDataStream &operator>>(QDataStream &in, Service &service);
error: passing ‘const Service’ as ‘this’ argument of ‘QString Service::getName()’ discards qualifiers
The second is, you can only use non-pointer types of Service when exporting the whole QList to a QDataStream. The following would work:
Code:QList<Service> services; mystream << services;

If you can't change to non-pointer type, you will have to iterate through the list at your own.
Code:QList<Service*> services; mystream << services.size(); foreach(Service* service, services) mystream << *service;

|
|
2010-06-03
, 22:06
|
|
Posts: 726 |
Thanked: 345 times |
Joined on Apr 2010
@ Sweden
|
#4
|
what's the matter with this?! i'm still so confused about reference and pointer
|
|
2010-06-03
, 22:06
|
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#5
|
template<typename T>
void QSettingsManager::storeBinary(const QString& key, const T& value)
{
checkSettingsObj();
QByteArray array;
QDataStream stream(&array, QIODevice::WriteOnly);
stream << value;
storeSetting(key, array);
}
QList<QString> myList;
... // fill myList with values
QSettingsManager::storeBinary<QList<QString> >("settingskey", myList);
| The Following User Says Thank You to Venemo For This Useful Post: | ||
|
|
2010-06-03
, 22:10
|
|
Posts: 190 |
Thanked: 129 times |
Joined on Mar 2010
@ Bavaria, Germany
|
#6
|
error: passing ‘const Service’ as ‘this’ argument of ‘QString Service::getName()’ discards qualifiers
that's would be good, but i've to pass these pointers to other class in order to edit them, so i think i can't use non-pointer...am i right?!
Service service; Service* servicePointer = &service; Service& serviceReference = service; Service& serviceReferenceFromPointer = *service;
nice this way, but...should i store manually the list's size before store elements?
If you create a QList<Service*> and call the shift operator for the QDataStream, your datastream will fill something strange (or it crashes). Since the QList operator for the QDataStream does not know that it should create a Service instance with "new".what's the matter with this?! i'm still so confused about reference and pointer
| The Following User Says Thank You to gri For This Useful Post: | ||
|
|
2010-06-03
, 22:24
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#7
|

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.

|
|
2010-06-03
, 22:32
|
|
|
Posts: 1,296 |
Thanked: 1,773 times |
Joined on Aug 2009
@ Budapest, Hungary
|
#8
|
is this?!
http://vcs.maemo.org/svn/eve-watcher...ngsmanager.cpp
that's why i said that << and >> work well with "Service*". so T is serializable!
@gri, if i can use non-pointer to do that...i wonder...when a pointer is necessary!?
| The Following User Says Thank You to Venemo For This Useful Post: | ||
|
|
2010-06-03
, 22:46
|
|
Posts: 726 |
Thanked: 345 times |
Joined on Apr 2010
@ Sweden
|
#9
|
int i = 17; int *pointer_to_int = &i;
*pointer_to_int = 4711;
int **pointer_to_pointer_to_int = &pointer_to_int;
int do_stuff(char *foo, char **error) {
...
}
|
|
2010-06-03
, 22:49
|
|
Posts: 124 |
Thanked: 10 times |
Joined on Jan 2007
@ Italy
|
#10
|


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:
here some snap of my code:
#ifndef SERVICE_H #define SERVICE_H #include <QString> #include "qmetatype.h" class Service { public: Service(); Service(QString name, QString address, bool needU, QString user, bool needP, QString password, bool needN, QString nick, bool active); QString getName(); QString getAddress(); QString getUser(); QString getPassword(); QString getNick(); .......... bool isActive(); bool needUser(); bool needPass(); bool needNick(); private: QString name; QString address; QString user; QString password; QString nick; bool active; bool needU; bool needP; bool needN; }; QDataStream &operator<<(QDataStream &out, Service &service); QDataStream &operator>>(QDataStream &in, Service &service); Q_DECLARE_METATYPE(Service*); #endif // SERVICE_H#include "service.h" #include <QStringList> #include <QDebug> Service::Service() { this->name=""; this->address=""; this->user=""; this->password=""; this->nick=""; this->active=true; } Service::Service(QString name, QString address, bool needU, QString user, bool needP, QString password, bool needN, QString nick,bool active){ this->name=name; this->address=address; this->user=user; this->password=password; this->nick=nick; this->needN=needN; this->needP=needP; this->needU=needU; this->active=active; } QString Service::getAddress(){ return address; } QString Service::getName(){ return name; } QString Service::getUser(){ return user; } QString Service::getPassword(){ return password; } QString Service::getNick(){ return nick; } ....... bool Service::isActive(){ return this->active; } bool Service::needUser(){ return this->needU; } bool Service::needPass(){ return this->needP; } bool Service::needNick(){ return this->needN; } QDataStream &operator<<(QDataStream &out, Service &service) { out << service.getName() << service.getAddress() << service.needUser() << service.getUser() << service.needPass() << service.getPassword() << service.needNick() << service.getNick() << service.isActive(); return out; } QDataStream &operator>>(QDataStream &in, Service &service) { QString name; QString address; QString user; QString password; QString nick; bool needU, needP, needN, active; in >> name >> address >> needU >> user >> needP >> password >> needN >> nick >> active; service = Service(name, address, needU, user, needP, password, needN, nick, active); return in; }load:
QVariant var = settings->value("services"); QByteArray array; array =var.value<QByteArray>(); QDataStream data(&array,QIODevice::ReadWrite); data >> services;QByteArray array; QDataStream data(&array, QIODevice::ReadWrite); data << services; QVariant var; var.setValue(array); //} settings->setValue("services", var);QList<Service*> *services;
i hope u can help me