Results 1 to 2 of 2

Thread: Writing my own object to a binary file.

  1. #1
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Writing my own object to a binary file.

    Hi,

    I want to write my own object to the binary file. But I receive the message:

    C:\Qt\Qt5.1.1\5.1.1\mingw48_32\include\QtCore\qdat astream.h:300: error: no match for 'operator<<' (operand types are 'QDataStream' and 'const User')
    s << *it;
    ^
    Qt Code:
    1. void Dialog::on_btnWrite_clicked()
    2. {
    3. QString nike = ui->wleNike->text();
    4. QString password = ui->wlePassword->text();
    5. QString firstName = ui->wleFirstName->text();
    6. QString lastName = ui->wleLastName->text();
    7. int age = ui->wspAge->text().toInt();
    8.  
    9. User user;
    10. user.nike = nike;
    11. user.password = password;
    12. user.firstName = firstName;
    13. user.lastName = lastName;
    14. user.age = age;
    15.  
    16. QVector<User> users;
    17. users.push_back(user);
    18.  
    19. QString fileName = "c:/users.txt";
    20. QFile file(fileName);
    21.  
    22. QDataStream out(&file);
    23. out.setVersion(QDataStream::Qt_5_1);
    24.  
    25. out << users;
    26.  
    27. file.flush();
    28. file.close();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. The following user says thank you to 8Observer8 for this useful post:


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

    Default Re: Writing my own object to a binary file.

    You need to provide the operator<<() and operator>>() implementation for your User class. Its prototype looks like:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &, const User &);
    2. QDataStream &operator>>(QDataStream &, User &);
    To copy to clipboard, switch view to plain text mode 
    Each should do whatever is necessary to save/restore the internal state of the User object.

Similar Threads

  1. how to add a linux C compiled object file to my qt object ?
    By saman_artorious in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2012, 11:45
  2. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 20:24
  3. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 15:28
  4. How to Print a doc file (or binary file) to printer
    By rmagro in forum Qt Programming
    Replies: 15
    Last Post: 5th September 2008, 16:46
  5. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 17:23

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.