Results 1 to 8 of 8

Thread: QTextStream with QString

  1. #1
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextStream with QString

    Hello,

    i don't quite understand how QTextStream works together with QStrings. I've got this code:

    Qt Code:
    1. QTextStream ts(&s);
    2. ts<<11<<" Freunde";
    3. std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();
    To copy to clipboard, switch view to plain text mode 

    That produces this output:
    1.11 Freunde

    Why doesn't QTextStream write to the String, as intended? I would be happy with this behaviour, too, but why do I have to specify a string as an argument with the constructor? Is there a function that needs to be called? I didn't find one and in the examples it works without one - but the examples work with devices rather than QStrings.
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream with QString

    This might help to understand things better.

  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream with QString

    Quote Originally Posted by munna
    This might help to understand things better.
    The Qt Documentation is my first ressource if I want to look up something, so I've read that already. I don't see an answer to my question in there.

  4. #4
    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: QTextStream with QString

    Try calling QTextStream::flush() to flush the buffered data waiting to be written.
    J-P Nurmi

  5. #5
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextStream with QString

    That's weird, when I run it, the output I get is:

    1.11 Freunde
    2.11 Freunde

    which is what I expect. (Running WinXP, Qt 4.1.3)
    The docs say that QTextStream::flush() has no effect when QTextStream operates on a string... Maybe your cout just needs to be flushed? weird.
    Software Engineer



  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream with QString

    Thanks, but neither

    Qt Code:
    1. QTextStream ts(&s);
    2. ts<<11<<" Freunde";
    3. ts.flush();
    4. std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();
    To copy to clipboard, switch view to plain text mode 

    nor

    Qt Code:
    1. QTextStream ts(&s);
    2. ts<<11<<" Freunde";
    3. std::cout<<"1."<<ts.readAll().toStdString()<<'\n'<<"2."<<s.toStdString();
    4. std::cout.flush();
    To copy to clipboard, switch view to plain text mode 

    nor a combination ob both helps. But perhaps we are on the right track
    I'm using Qt 4.1.4 on Linux.

  7. #7
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextStream with QString

    Why "2." doesn't even show in your output?
    What happens if you split the cout into two lines?

    std::cout<<"1."<<ts.readAll().toStdString()<<'\n';
    std::cout <<"2."<<s.toStdString();
    Software Engineer



  8. #8
    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: QTextStream with QString

    What about if you try to use qDebug() for outputting?

    Qt Code:
    1. #include <QtDebug>
    2. qDebug() <<"1."<<ts.readAll()<<'\n'<<"2."<<s;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  2. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  3. qtextstream & qstring
    By mickey in forum Newbie
    Replies: 3
    Last Post: 24th February 2006, 19:18
  4. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.