Results 1 to 5 of 5

Thread: Write the content of a QStringList to a QTextStream

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Write the content of a QStringList to a QTextStream

    Hi
    In this code i have to write all the items of a QStringList into a text file:
    Qt Code:
    1. QString fileName = QFileDialog::getSaveFileName(this);
    2. if (fileName.isEmpty()){
    3. //
    4. }else{
    5. QFile file(fileName);
    6. if (!file.open(QFile::WriteOnly | QFile::Text)) {
    7. QMessageBox::warning(this, tr("Application"),
    8. tr("Cannot write file %1:\n%2.")
    9. .arg(fileName)
    10. .arg(file.errorString()));
    11. }else{
    12. QTextStream out(&file);
    13. //here
    14. out << "i want to write the content of the QStringList x here";
    15. file.close();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    I read about using QVariant for this purpose!
    Is this the right approach?
    Can someone provide me with an example?

    Thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Write the content of a QStringList to a QTextStream

    I think your aproch is correct just do

    Qt Code:
    1. out << x;
    To copy to clipboard, switch view to plain text mode 

    where x is your QStringList.

  3. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write the content of a QStringList to a QTextStream

    Qt Code:
    1. QTextStream out(&file);
    2. //here
    3. x << "item1" << "item2" << "item3";
    4. out << x;
    To copy to clipboard, switch view to plain text mode 
    leads to
    Qt Code:
    1. /home/torrao/Desktop/qt/passwordCreator/mainwindow.cpp:233: error: no match for ‘operator<<’ in ‘out << x’
    To copy to clipboard, switch view to plain text mode 
    Only QString is found for this operator http://doc.trolltech.com/4.5/qtextstream.html
    I could use a loop to access all indexes in the list .. but there must be a more sophisticated way ... i think
    Last edited by graciano; 8th August 2009 at 19:00.

  4. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Write the content of a QStringList to a QTextStream

    This does it.
    Qt Code:
    1. QTextStream out(&file);
    2. //here
    3. x << "item1" << "item2" << "item3";
    4. for ( QStringList::Iterator it = x.begin(); it != x.end(); ++it )
    5. out << *it << "\n";
    To copy to clipboard, switch view to plain text mode 

    Now i guess i must do some reading about QVariant

  5. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Write the content of a QStringList to a QTextStream

    if you use QDataStream instead of QTextStream it will work fine without the loop.
    Do you have to use text files or could you use binary ones ?

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

    hakermania (29th December 2010)

Similar Threads

  1. How to write to a TCPSocket with a QTextStream?
    By Denarius in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2009, 12:28
  2. read and write content of lineedit widget in Other Form
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 15:07
  3. segfault on qtextstream
    By patcito in forum Qt Programming
    Replies: 13
    Last Post: 26th February 2006, 13:10

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.