Results 1 to 2 of 2

Thread: QDataSteam.

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QDataSteam.

    I get the text from the UI's QTextEdit, and write to the file use QDataStrem,but if the input text has used Enter key, then I will get garbled from the file.

    There are some codes:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out,const MyClass &obj)
    2. {
    3. out << obj.m_name
    4. << obj.m_desc
    5. << obj.m_argMap
    6. << obj.resultVel
    7. << obj.m_code;
    8.  
    9. return out;
    10. }
    11.  
    12. QDataStream &operator>>(QDataStream &in, MyClass &obj)
    13. {
    14. in >> obj.m_name
    15. >> obj.m_desc
    16. >> obj.m_argMap
    17. >> obj.resultVel
    18. >> obj.m_code;
    19.  
    20. return in;
    21. }
    22.  
    23. .... WriteToFile()
    24. {
    25. .........................
    26. QFile file(path);
    27. if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
    28. return 1;
    29.  
    30. MyClass d;
    31. d.setName(name);
    32. d.setCode(code);
    33. d.setDescription(desc);
    34.  
    35. QDataStream out(&file);
    36. out.setVersion(QDataStream::Qt_4_6);
    37. out << d;
    38. file.close();
    39. .........................
    40.  
    41. }
    42.  
    43. .... ReadFromFile()
    44. {
    45. .........................
    46. QFile file(str);
    47. if (!file.open(QIODevice::ReadOnly))
    48. return 1;
    49.  
    50. MyClass d;
    51. QDataStream in(&file);
    52. in.setVersion(QDataStream::Qt_4_6);
    53. in >> d;
    54. file.close();
    55. .........................
    56.  
    57. }
    To copy to clipboard, switch view to plain text mode 

    What I get are like this : ㈀㌀㄀㈀㌀.........
    Last edited by wysota; 25th September 2010 at 08:48.

  2. #2
    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: QDataSteam.

    Don't open the file as text.
    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.


  3. The following user says thank you to wysota for this useful post:

    wookoon (25th September 2010)

Similar Threads

  1. QDataSteam.
    By wookoon in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2010, 16:27

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.