Qt:qbytearray storing binary data (including structs, custom Qt objects)

Source: Internet
Author: User

It is necessary to build database Blob field by using Qbytearray to make it easy to use its API to access and modify memory data. How to write custom structures and classes into blobs

1. Copy memory data with memcpy

Customizing the person structure body

CPP Code
  1. typedef struct
  2. {
  3. int age;
  4. Char name[20];
  5. }person;
  6. Write multiple structures to Qbytearray
  7. void Writestruct ()
  8. {
  9. Qbytearray BA;
  10. Ba.resize (person); //Set capacity
  11. //Serialization
  12. For (int i=0;i<2;i++)
  13. {
  14. Person P1;
  15. P1.age=10+i;
  16. strcpy (P1.name,"Javaeye");
  17. memcpy (Ba.data () +i*sizeof (person), &P1,sizeof (person)); //Pointer movement, write multiple data
  18. }
  19. //Restore Data
  20. Person *person= (person*) ba.data ();
  21. Qdebug () <age<<"---" <name;
  22. person++;
  23. Qdebug () <age<<"---" <name;
  24. }

memcpy can only handle fields as basic type of struct, when using qstring name, I use Person->name to access its value, program crashes; This shows that the memory data cannot be restored and built into the Qstring class. If you want to write a custom QT class, you can only use Qbuffer to write to the binary stream

2. Qbuffer writing QT Custom structure

CPP Code
  1. Qbuffer Serialization of Custom objects
  2. typedef struct
  3. {
  4. int age;
  5. QString name;
  6. }qperson;
  7. /**
  8. * @brief overloading the input of custom objects
  9. */
  10. Inline Qdatastream &operator<< (qdatastream &out,const Qperson &per)
  11. {
  12. out<<per.age<<per.name; < span= "" >
  13. return out;
  14. }
  15. /**
  16. * @brief overloading the output of custom objects
  17. */
  18. Inline Qdatastream &operator>> (qdatastream &in,qperson &per)
  19. {
  20. int age;
  21. QString name;
  22. in>>age>>name;
  23. Per.age=age;
  24. Per.name=name;
  25. return in;
  26. }
  27. /**
  28. * @brief Qbuffer can handle QT custom types
  29. */
  30. void Testqbuffer ()
  31. {
  32. Qbytearray BA;
  33. Ba.resize (Qperson);
  34. Qbuffer buffer (&BA);
  35. Buffer.open (qiodevice::writeonly);
  36. //Input
  37. Qdatastream out (&buffer);
  38. For (int i=0;i<2;i++)
  39. {
  40. Qperson per;
  41. Per.age=20+i;
  42. Per.name=qstring ("sun_%1"). Arg (i+1);
  43. out<<per; < span= "" >
  44. }
  45. Buffer.close ();
  46. //Output
  47. Qbuffer buf (&BA);
  48. Buf.open (qiodevice::readonly);
  49. Qdatastream in (&BUF);
  50. For (int i=0;i<2;i++)
  51. {
  52. Qperson per;
  53. in>>per;
  54. Qdebug () <<per.age<<"---"
  55. }
  56. Buf.close ();
  57. }

Original: http://blog.chinaunix.net/uid-23381466-id-3896956.html

Qt:qbytearray storing binary data (including structs, custom Qt objects)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.