Results 1 to 9 of 9

Thread: QDataStream: Reading an object from disk, please advise.

  1. #1
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QDataStream: Reading an object from disk, please advise.

    Hello,

    I am attempting to include binary file save/open to a simple program that I am working on. So far, it appears to be writing the data I need correctly (well, at least the file is being written and populated). The wall I am running into is loading the data back into the program. I need to save what LineEdits contain, which boxes are checked, etc. I have manually entered each element/widget using a for loop with pointers. Is there an easier way, ie. write the object data to the disk? I am not sure how to do that with Qt. If I could write as an object, hopefully I could read it as an object. Anyways, you guys know better than I. Any help or pointers to a tutorial or helpful example would be greatly appreciated.

    Forgive the formatting, the paste didnt take properly.
    Qt Code:
    1. void MainWindow::save()
    2. {
    3. QFile file("grades.dat");
    4.  
    5. if (!file.open(QIODevice::WriteOnly))
    6. {
    7. std::cout << "Cannot open file for writing: "
    8. << qPrintable(file.errorString()) << endl;
    9. return;
    10. }
    11.  
    12. QDataStream output(&file);
    13. output.setVersion(QDataStream::Qt_4_3);
    14.  
    15. output << quint32(0x39421872);
    16.  
    17. for ( int i = 1 ; i <= 8 ; i++ )
    18. {
    19. output << ass_label_LineEdit_[i]->text();
    20. output << ass_grade_SpinBox_[i]->value();
    21. output << ass_weight_SpinBox_[i]->value();
    22. output << ass_outof_SpinBox_[i]->value();
    23. output << ass_check_[i]->isChecked();
    24.  
    25. output << test_label_LineEdit_[i]->text();
    26. output << test_grade_SpinBox_[i]->value();
    27. output << test_weight_SpinBox_[i]->value();
    28. output << test_outof_SpinBox_[i]->value();
    29. output << test_check_[i]->isChecked();
    30.  
    31. output << other_label_LineEdit_[i]->text();
    32. output << other_grade_SpinBox_[i]->value();
    33. output << other_weight_SpinBox_[i]->value();
    34. output << other_outof_SpinBox_[i]->value();
    35. output << other_check_[i]->isChecked();
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    QDataStream does not have an << operator for QString, only for const char*.
    You can either write a const char instead of the edit's text or use QSettings.

    Regards

  3. #3
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Ok, thanks for the info. I will look into using QSetting.

  4. #4
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Apparently QSettings will not be useful to me in this case as I wish for the string to be stored in the file with the other elements.

    Is it possible to cast the QString to a const char and then store it in the file?

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Yes, with QString::toAscii().constData().

  6. The following user says thank you to marcel for this useful post:

    kramed (27th August 2007)

  7. #6
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Thank you kindly for the info.

  8. #7
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Sorry, I forgot to ask. After I get the casting syntax properly, is it possible for me to use a similar loop to read the data back into the program? Do I have to initialize copies of the same variables and then read them in ?

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream: Reading an object from disk, please advise.

    Yes, it would be better to create separate vars and read the data in them.
    Then you set the variables in the widgets.

    Regards

  10. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDataStream: Reading an object from disk, please advise.

    Quote Originally Posted by marcel View Post
    QDataStream does not have an << operator for QString
    QDataStream & operator<< ( QDataStream & stream, const QString & string )
    J-P Nurmi

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.