Results 1 to 4 of 4

Thread: Is it possible to serialize QVariant into a QString?

  1. #1
    Join Date
    Oct 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Is it possible to serialize QVariant into a QString?

    I'm writing a client-side class, and a server-side class, who are meant to communicate data with one another. In the middle I have to use a network client whose API's "Send" function takes a QString. That Send function encapsulates my data within a larger string, which is then sent over the network, decapsulated at the other end, and then the body is given to my other class.

    I have no control over the encapsulation: basically whatever I need to send, has to be stored within a QString. I'd rather avoid annoying manual serializing/deserializing stuff in XML or somesuch. My data is a collection of native Qt types that can fit in a QVariant (bool, ASCII-only QStrings, and qint32). With this in mind, is there a way I can serialize QVariant/QVariantList into a QString?

    QTextStream does not seem to serialize QVariant, only QDataStream does, and from what I saw there's no combination of QDataStream, QBuffer, and QByteArray which will allow me to use QString as a container. I'm asking here with the hopes that I am wrong. Please oh please tell me that I'm wrong

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to serialize QVariant into a QString?

    Well, apart from fixing the broken assumption that everything fits into a string, you can serialise to binary any way that works and then base-64 or hex encode the binary blob to go through th string pathway.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to serialize QVariant into a QString?

    bools, strings and ints can be converted to text using QVariant::toString().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is it possible to serialize QVariant into a QString?

    Quote Originally Posted by ChrisW67 View Post
    Well, apart from fixing the broken assumption that everything fits into a string, you can serialise to binary any way that works and then base-64 or hex encode the binary blob to go through th string pathway.
    Thanks Chris, I should have been looking at QByteArray closer. I got it working.

    Full example in case anyone finds this by googling:

    Qt Code:
    1. QList<QVariant> CreateData();
    2. QString SerializeDataToB64String(const QList<QVariant>& listToSerialize);
    3. QList<QVariant> DeserializeB64String(const QString& serializedList);
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QList<QVariant> dataToWrite = CreateData();
    10. QString container = SerializeDataToB64String(dataToWrite);
    11. qDebug() << "b64 string container contents:" << container;
    12. QList<QVariant> dataRead = DeserializeB64String(container);
    13.  
    14. Q_ASSERT(dataToWrite.size() == dataRead.size());
    15. for (int i = 0; i < dataToWrite.size(); i++)
    16. {
    17. Q_ASSERT(dataToWrite[i] == dataRead[i]);
    18. qDebug() << dataToWrite[i];
    19. }
    20.  
    21. return 0;
    22. }
    23.  
    24.  
    25. QList<QVariant> CreateData()
    26. {
    27. QList<QVariant> vars;
    28.  
    29. vars.append("a string");
    30. vars.append((qint32) 123);
    31. vars.append(false);
    32.  
    33. return vars;
    34. }
    35.  
    36. QString SerializeDataToB64String(const QList<QVariant>& listToSerialize)
    37. {
    38. QByteArray byteArray;
    39. QBuffer writeBuffer(&byteArray);
    40. writeBuffer.open(QIODevice::WriteOnly);
    41. QDataStream out(&writeBuffer);
    42.  
    43. out << listToSerialize;
    44.  
    45. writeBuffer.close();
    46.  
    47. QString s = QString(byteArray.toBase64());
    48.  
    49. qDebug() << "array size when written:" << byteArray.size();
    50.  
    51. return s;
    52. }
    53.  
    54. QList<QVariant> DeserializeB64String(const QString& serializedList)
    55. {
    56. QByteArray readArr = QByteArray::fromBase64(serializedList.toAscii());
    57. QBuffer readBuffer(&readArr);
    58. readBuffer.open(QIODevice::ReadOnly);
    59. QDataStream in(&readBuffer);
    60.  
    61. QList<QVariant> readVars;
    62.  
    63. in >> readVars;
    64.  
    65. qDebug() << "array size when read:" << readArr.size();
    66.  
    67. return readVars;
    68. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QVariant to QString (binary)
    By 8080205 in forum Newbie
    Replies: 1
    Last Post: 9th October 2012, 12:22
  2. Replies: 4
    Last Post: 4th January 2011, 12:07
  3. QVariant with QString / who free's its data?
    By elias.bachaalany in forum Newbie
    Replies: 1
    Last Post: 26th October 2010, 10:42
  4. Replies: 0
    Last Post: 25th June 2009, 08:17
  5. QDataStream and QHash<QString,QVariant> crash on read
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2008, 12:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.