Results 1 to 3 of 3

Thread: Nice Solution to push Class on QVariant and take back...

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Nice Solution to push Class on QVariant and take back...

    You like the Long or fast way to transport self made class on QObject?

    Here a short way..

    Qt Code:
    1. class Qmailf;
    2.  
    3. /// 6 okt. 2013 save on svn
    4. /// great this to push class on qvariant
    5. //// http://qt-gmail-access.googlecode.com/svn/trunk/GmailHand/mime_standard.h
    6.  
    7. template <class T> class VPtr {
    8. public:
    9.  
    10. static T* asPtr(QVariant v) {
    11. return (T *) v.value<void *>();
    12. }
    13.  
    14. static QVariant asQVariant(T* ptr) {
    15. return qVariantFromValue((void *) ptr);
    16. }
    17. };
    18.  
    19. /*
    20.  http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/
    21.  MyClass *p;
    22. QVariant v = VPtr<MyClass>::asQVariant(p);
    23.  
    24. MyClass *p1 = VPtr<MyClass>::asPtr(v);
    25.  * MMime::MimeTypes help;
    26.  */
    To copy to clipboard, switch view to plain text mode 

    Or the long long way...

    Qt Code:
    1. class Qmailf {
    2. public:
    3. Qmailf(int id) : chunk(""), mime("txt"), file("file.txt"), ext("txt"), uuid(id) {
    4.  
    5. }
    6. void SetChunk(const QByteArray& x) {
    7. chunk = x;
    8. ///// qDebug() << "incomming <" << uuid << "> ";
    9. }
    10. /// set mime automatic
    11. /// todo filename resolver from header && id from inline image
    12. void SetMeta(const QString x) {
    13. meta_header = x;
    14. MMime::MimeTypes help;
    15. mime = help.Contenttype_Resolver(x);
    16. ext = help.MimeFromContent(mime);
    17. QString def(QString("mail_header_%1.%2").arg(uuid).arg(ext));
    18. file = def;
    19. }
    20. /// no make file or db take the chunk from mail and send at the right place
    21. void SetFile(const QString dfile) {
    22. MMime::MimeTypes fmime;
    23. QFileInfo fi(dfile);
    24. file = fi.fileName();
    25. ext = fi.suffix();
    26. mime = fmime.value(ext);
    27. }
    28. QByteArray Chunk() {
    29. return chunk;
    30. }
    31. QString Mime() {
    32. return mime;
    33. }
    34. int Uid() {
    35. return uuid;
    36. }
    37. QString Filename() {
    38. return file;
    39. }
    40. void Info() {
    41. QByteArray text = QByteArray::fromBase64(chunk);
    42. qDebug() << "Content-Type:" << Mime() << " - " << Filename();
    43. qDebug() << "Meta:" << meta_header;
    44. qDebug() << "Body first 28 chunk:" << text.mid(0,20).simplified();
    45. text.clear();
    46. }
    47. private:
    48. QByteArray chunk; // base64 encoded or other meta_heade having info
    49. QString mime;
    50. QString meta_header; /// meta info to create a file or o drupal node
    51. QString file;
    52. QString ext;
    53. int uuid;
    54. };
    55.  
    56. typedef QMap<int, QVariant> Attach_File_Map;
    57. //// http://www.qtcentre.org/wiki/index.php?title=Using_custom_data_types_with_Qt
    58.  
    59. /// from this can create
    60. /// mail dir format or other
    61. /// i need to convert on drupal node and send on cms
    62.  
    63. struct ICmail {
    64. quint64 uuid;
    65. QString msgid;
    66. quint64 multipart;
    67. QString sender; //// long chunk history from one mail
    68. QString md5; /// from incomming file server
    69. QString from;
    70. QString to;
    71. QString subject;
    72. QByteArray charcodec;
    73. QString transferencoding;
    74. QString language;
    75. QString date;
    76. QString txt;
    77. QString html; // image inline base64 encodet
    78. QString master;
    79. QString boundary;
    80. QString root_cmd;
    81. QMap<int, QVariant> alist;
    82. operator QVariant() const {
    83. return QVariant::fromValue(*this);
    84. }
    85. };
    86.  
    87. /// todo a textstream for console to insert chort info from this mail
    88.  
    89. inline QDataStream& operator<<(QDataStream& out, const ICmail& mail) {
    90. out << mail.from;
    91. out << mail.subject;
    92. out << mail.md5;
    93. out << mail.date;
    94. out << mail.boundary;
    95. return out;
    96. }
    97. inline QDataStream& operator>>(QDataStream& in, ICmail& mail) {
    98. in >> mail.from;
    99. in >> mail.subject;
    100. in >> mail.md5;
    101. in >> mail.date;
    102. in >> mail.boundary;
    103. return in;
    104. }
    105.  
    106. /*
    107.  
    108.   << mail.txt << ",\n"
    109.   << mail.html << ",\n"
    110.  */
    111. inline QDebug &operator <<(QDebug debug, const ICmail &mail) {
    112. debug.nospace() << "ICmail{start(\n"
    113. << mail.from << ",\n"
    114. << mail.subject << ",\nContent-Transfer-Encoding:"
    115. << mail.transferencoding << "\n,"
    116. << mail.date << "\n,"
    117. << mail.boundary << "\n,"
    118. << mail.multipart << "\n,\nRoot cmd Content-Type:\n"
    119. << mail.root_cmd << ")end}\n";
    120.  
    121. return debug.space();
    122. }
    123. Q_DECLARE_METATYPE(ICmail);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Nice Solution to push Class on QVariant and take back...

    The long way looks a lot shorter then the short way

    The long way requires a Q_DECLARE_METATYPE, the short way requires a helper template and C-style casts.

    Cheers,
    _

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

    Default Re: Nice Solution to push Class on QVariant and take back...

    The short way doesn't check validity of the cast.
    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.


Similar Threads

  1. QSharedPointer in a QVariant and back again
    By tomschuring in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2012, 00:02
  2. Replies: 16
    Last Post: 22nd March 2011, 09:01
  3. Solution for render text in some low-level class
    By deedw in forum Qt Programming
    Replies: 0
    Last Post: 19th April 2010, 13:45
  4. Storing and reading back template types in QVariant
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2010, 08:40
  5. Replies: 3
    Last Post: 21st November 2009, 01:02

Tags for this Thread

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.