Results 1 to 4 of 4

Thread: QTextStream question

  1. #1
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextStream question

    Hello everyone,
    I had the following piece of code:

    Qt Code:
    1. int iCounter = 0;
    2. std::string str;
    3. std::stringstream sstr;
    4. sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
    5. str.append(sstr.str());
    To copy to clipboard, switch view to plain text mode 

    When i now output the string it looked like this:
    Number : 2 1 0
    instead of Number : 0 1 2
    and I even got a warning that iCounter may be undefined...I do not know whats wrong there, I also tried it out with standard c++ in a console application and it worked.

    So, I decided to switch to QTextStream instead of stringestream, which looks like this:
    Qt Code:
    1. int iCounter = 0;
    2. std::string str;
    3. sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
    4. str.append(sstr.string()->toStdString());
    To copy to clipboard, switch view to plain text mode 

    But at the line of 5 the application crashes...Why is both not working?

  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: QTextStream question

    But at the line of 5 the application crashes...
    QString * QTextStream::string () const
    Returns the current string assigned to the QTextStream, or 0 if no string has been assigned.
    You forgot to assign a string to QTextStream, so it returns 0 (and crashes when you try to dereference NULL pointer), this should work:
    Qt Code:
    1. int iCounter = 0;
    2. std::string str;
    3. QString string;
    4. QTextStream sstr(&string);
    5. sstr << "Number :" << "\t" << iCounter++ << "\t" << iCounter++ << "\t" << iCounter++;
    6. str.append(string.toStdString());
    To copy to clipboard, switch view to plain text mode 
    But I think this could be done easier (I dont know the purpose, but anyway...):
    Qt Code:
    1. int iCounter = 0;
    2. std::string str;
    3. QString string = "Number :";
    4. for(int i=0 ; i<3 ; ++i) string += QString("\t%1").arg(iCounter++);
    5. str.append(string.toStdString());
    To copy to clipboard, switch view to plain text mode 
    No need for QTextStream and operations on iCounter are well defined now.

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

    seux (14th August 2011)

  4. #3
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextStream question

    I got it working with your second solution, even with your first one i got the following output:
    Number : 2 1 0
    I also tried it out with
    Qt Code:
    1. string = QString("Number: %1\t%2\t%3").arg(iCounter++).arg(iCounter++).arg(iCounter++);
    To copy to clipboard, switch view to plain text mode 
    with just the above output

    I know that I got it, but I want to know why the other code is not working...Does somebody know what is going on there?

  5. #4
    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: QTextStream question

    My compiler issues a warning when compiling that line:
    main.cpp:32: warning: operation on ‘iCounter’ may be undefined
    main.cpp:32: warning: operation on ‘iCounter’ may be undefined
    which is a flag that something is potentially broken.

    You are assuming that the compiler executes the post-increment operators from left to right. That is clearly not what the compiler is doing: it assumes the three arguments to arg() are independent, therefore can be evaluated in any order, and evaluates them in 'reverse' order. I get a different (unexpected) result if I use the pre-increment operator. Generally, relying on side-effects and evaluation order within a single compound statement is fragile. The only guarantee the you get with that statement is that iCounter will have been incremented three times before the next statement is executed. Read about "sequence points" for more detail.

    Try:
    Qt Code:
    1. string = QString("Number: %1\t%2\t%3").arg(iCounter).arg(iCounter+1).arg(iCounter+2);
    2. iCounter += 3;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTextStream
    By lahmitia in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2011, 21:50
  2. Question about using QFile and QTextStream
    By liuliuwang in forum Newbie
    Replies: 3
    Last Post: 13th October 2010, 23:33
  3. QTextStream under Mac OS X
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 14th September 2009, 09:49
  4. QTextStream
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 16:43
  5. QTextStream help pls
    By munna in forum Newbie
    Replies: 5
    Last Post: 16th May 2006, 18:45

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.