Results 1 to 7 of 7

Thread: QString assignment problem in C++ style but not in C style

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question QString assignment problem in C++ style but not in C style

    Hi,

    This C++ does not work:
    Qt Code:
    1. string line;
    2. getline (INfile,line);
    3. QString Line = line;
    To copy to clipboard, switch view to plain text mode 
    error: conversion from ‘std::string’ to non-scalar type ‘QString’ requested

    However this C style does work:
    Qt Code:
    1. char line[50]="";
    2. fgets(line, 50, INfile);
    3. QString Line = line;
    To copy to clipboard, switch view to plain text mode 

    Why is that? I'd really like to get the C++ style to work. What's the best way to obtain a line from a file in Qt?

    Thanks!!
    Last edited by timmu; 29th December 2011 at 14:56.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString assignment problem in C++ style but not in C style

    Why is that?
    Because there is no QString constructor taking a std::string as agrument.
    What's the best way to obtain a line from a file in Qt?
    Use QFile and QTextStream, there is an example of reading a file line-by-line in QFile documentation.
    Last edited by stampede; 29th December 2011 at 15:09. Reason: updated contents

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

    timmu (29th December 2011)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString assignment problem in C++ style but not in C style

    Thank you so much. But what would be the best way to read getline into QString then? How would I convert getline text into QString text?

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString assignment problem in C++ style but not in C style

    You can use the c_str() member function to get an char array from std::string.

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

    timmu (29th December 2011)

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString assignment problem in C++ style but not in C style

    Second example from QFile documentation:
    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4.  
    5. QTextStream in(&file);
    6. while (!in.atEnd()) {
    7. QString line = in.readLine(); // here you have a line from text file stored in QString
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    timmu (29th December 2011)

  9. #6
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString assignment problem in C++ style but not in C style

    Thanks all.

    readLine() is a nice way to get the entire line into QString. But what if I want just one entry in a space-delimited file. I'd like to do what fsanf() does. What would be the analogous function to fscanf()?

    Thanks!

  10. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString assignment problem in C++ style but not in C style

    using the operator >> from a std:stream will probably suit your need.

    e.g. for some fstream 'f', and some std::string 'str':

    f >> str;

    although you can get some empty tokens from end of lines etc.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. copy constructor and assignment operator
    By sajis997 in forum Newbie
    Replies: 9
    Last Post: 27th July 2011, 18:02
  2. QSettings Assignment
    By SixDegrees in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2010, 00:00
  3. QString assignment
    By valgaba in forum Qt Programming
    Replies: 4
    Last Post: 25th April 2010, 18:31
  4. QVector assignment question
    By hvw59601 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 21:34
  5. copying and assignment constructors
    By TheKedge in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 16:09

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.