Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing my own object to a binary file.
Qt 6.11 is out! See what's new in the release blog

Writing my own object to a binary file.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    Hi,

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

    [QUOTE]C:\Qt\Qt5.1.1\5.1.1\mingw48_32\include\QtCore\qdatastream.h:300: error: no match for 'operator<<' (operand types are 'QDataStream' and 'const User')
    s << *it;
    ^[/QUOTE]

    [CODE]
    void Dialog::on_btnWrite_clicked()
    {
    QString nike = ui->wleNike->text();
    QString password = ui->wlePassword->text();
    QString firstName = ui->wleFirstName->text();
    QString lastName = ui->wleLastName->text();
    int age = ui->wspAge->text().toInt();

    User user;
    user.nike = nike;
    user.password = password;
    user.firstName = firstName;
    user.lastName = lastName;
    user.age = age;
    
    QVector<User> users;
    users.push_back(user);
    
    QString fileName = "c:/users.txt";
    QFile file&#40;fileName&#41;;
    
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_1);
    
    out << users;
    
    file.flush();
    file.close();
    

    }
    [/CODE]

    Thank you!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Yes, you must define your own appropriate stream operators declared as non-member of the class

      @
      QDataStream &operator<<(QDataStream &, const User &);
      QDataStream &operator>>(QDataStream &, User &);
      @

      before you can use them.

      1 Reply Last reply
      0
      • 8Observer88 Offline
        8Observer88 Offline
        8Observer8
        wrote on last edited by
        #3

        Thank you! I'll try :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved