Results 1 to 7 of 7

Thread: convert each element of QVector<double> to QString and save in a text file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default convert each element of QVector<double> to QString and save in a text file

    Hi,
    there is a QVector<double> in name of "v1". Size of "v1" is 1000. I need to convert all this 1000 elements to QString (or convert to a QStringList) and then save this list into a text file. Please help me in this way.

    thanks

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: convert each element of QVector<double> to QString and save in a text file

    Read about QString::number

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

    Alex22 (10th February 2016)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: convert each element of QVector<double> to QString and save in a text file

    If you want to save directly into the file, see QTextStream.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Alex22 (10th February 2016)

  6. #4
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: convert each element of QVector<double> to QString and save in a text file

    Quote Originally Posted by Lesiok View Post
    Read about QString::number
    thanks Lesiok,
    I used this:
    Qt Code:
    1. for(int y=0; y<v1.size(); y++)
    2. {
    3. qDebug()<< QString("%1").arg(v1.at(y));
    4. }
    To copy to clipboard, switch view to plain text mode 

    how could i save this in a file (text type) in a one column?
    Last edited by Alex22; 10th February 2016 at 19:53.

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: convert each element of QVector<double> to QString and save in a text file

    See anda_skoa's post above.

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

    Alex22 (11th February 2016)

  9. #6
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: convert each element of QVector<double> to QString and save in a text file

    Thanks all
    I used this and works well:
    Qt Code:
    1. for(int y=0; y<v1.size(); y++)
    2. {
    3. list<< QString("%1").arg(v1.at(y));
    4. }
    5.  
    6.  
    7. QFile g1f;
    8. g1f.setFileName("test.txt");
    9. if(!g1f.open(QFile::WriteOnly | QFile::Text))
    10. {
    11. qDebug()<<"can not open to write";
    12. }
    13.  
    14. QTextStream df(&g1f);
    15. for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
    16. df << *it << "\n";
    17.  
    18. g1f.flush();
    19. g1f.close();
    To copy to clipboard, switch view to plain text mode 

    is there an optimal way to convert QVector<double> to QStringList? can we do this by QVector::toList()?

  10. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: convert each element of QVector<double> to QString and save in a text file

    The most optimal way is to not create the string list.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 17th December 2015, 10:26
  2. Replies: 9
    Last Post: 23rd April 2012, 13:53
  3. Convert data in QVector<QString> to Decimal
    By rex in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2011, 00:31
  4. Replies: 5
    Last Post: 29th April 2009, 22:39
  5. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04

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
  •  
Qt is a trademark of The Qt Company.