[Qt-interest] how to achive the serialization in Qt

David Boosalis david.boosalis at gmail.com
Tue Apr 13 16:25:31 CEST 2010


I do not tend to use serializers for Widgets but data, maybe this example
will be of help


class User: public QObject {
  Q_OBJECT
 public:
  typedef enum {InUse,NotInUse,Suspended,NeverUsed} UseType;
  User();
  User(const User &);
  ~User();
  QString firstName;
  QString lastName;
  int     id; //* database ID
  int     profileID;
  QString email;
  QString loginID;
  QString password;
  QDateTime lastAccess;
  QString phone;
  QString note;
  bool    enableLogin;
  bool   operator==( const User &s) const;
  User   &operator= ( const User &rhs );
  void    *socket;
  QHostAddress  ipAddress;
  protected slots:
 protected:
  QUuid threadID; // used by servers
  //ConnectStatus connectStatus;
};
QDataStream &operator<<(QDataStream &ds,User &);
QDataStream &operator>>(QDataStream &ds,User &);


--------------------------- user.cpp follows:

...
QDataStream &operator<<(QDataStream &ds,User &s)
{

  ds << (qint32) s.id;
  ds << (qint32) s.profileID;
  ds << s.firstName;
  ds << s.lastName;
  ds << s.email;
  ds << s.loginID;
  ds << s.password;
  ds << s.lastAccess;
  ds << s.phone;
  ds << s.note;
  ds << s.ipAddress;
  return(ds);
}
QDataStream &operator>>(QDataStream &ds,User &s)
{
  qint32  i32;
  // quint16 ui16;
  QUuid threadID;
  QHostAddress ha;
  ds >> i32;
  s.id = i32;
  ds >> i32;
  s.profileID = i32;
  ds >> s.firstName;
  ds >> s.lastName;
  ds >> s.email;
  ds >> s.loginID;
  ds >> s.password;
  ds >> s.lastAccess;
  ds >> s.phone;
  ds >> s.note;
  ds >> s.ipAddress;
  return(ds);
}


For convenience I also like to make serializers for my list class.

 class UserList : public QList<User *> {
 public:
  UserList();
  bool removeUserByID(int ID);
  User *findByID(int id);
  User *findByLoginID(QString &id);
  User *findByLoginID(char *);
  User *validateUser(const QString &userName,const QString &password);
};

QDataStream &operator<<(QDataStream &ds,UserList &);
QDataStream &operator>>(QDataStream &ds,UserList &);
...
/********************************************************************/
QDataStream &operator << (QDataStream &ds,UserList &sl)
{
  qint32  i32;
  User *s;
  i32 = sl.count();
  ds << i32;
  QListIterator<User *> iter(sl);
  while(iter.hasNext()) {
    s = iter.next();
    ds << *s;
  }
  return(ds);
}
/********************************************************************/
QDataStream &operator >> (QDataStream &ds,UserList &sl)
{
  User *s;
  int i;
  qint32  i32;
  ds >> i32;
  // qDebug() << __FILE__ << "read in this many users: " << i32;
  if (i32 < 1) {
    return(ds);
  }
  for (i=0;i<i32;i++) {
    s = new User();
    ds >> *s;
    sl.append(s);
  }
  return(ds);
}









On Tue, Apr 13, 2010 at 6:07 AM, Ramesh <ramesh.bs at robosoftin.com> wrote:

>  Hi,
>
>
>
> Folks, can you help me in how to achieve the serialization in Qt,
>
>
>
> I have some idea about QDataStream with this we can achieve the data
> stream,
>
> In the documentation it is mentioned that we can write char, int, etc the
> other  Data types, if I try to write the object it is throwing error L
>
>
>
> How to write the object to file using QDataStream?
>
>  -----------------------------------------------
>
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were not
> an intended recipient, please notify the sender and delete all copies.
> Emails to and from our network may be logged and monitored. This email and
> its attachments are scanned for virus by our scanners and are believed to be
> safe. However, no warranty is given that this email is free of malicious
> content or virus.
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100413/5cc20037/attachment.html 


More information about the Qt-interest-old mailing list