Results 1 to 5 of 5

Thread: Problem with QString .arg()

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem with QString .arg()

    Can anyone tell me the error within the following lines:

    QString dT;
    dT.clear();
    dT.append("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0);

    returns

    Force = 10 +/- 2

    properly on stdout. But when writing to formatted file using

    QFile file( repfname );
    QTextStream ts( &file );
    const char *foo = dT.toStdString().c_str();
    ts << foo << endl;
    file.close();


    file contains

    Force = %1 +/- %2

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QString .arg()

    QString::arg() returns a modified copy of the string, it doesn't modify the string in place so you have to assign the result to some variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Problem with QString .arg()

    Quote Originally Posted by ChrisN View Post
    Can anyone tell me the error within the following lines:

    QString dT;
    dT.clear();
    dT.append("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0);

    returns

    Force = 10 +/- 2

    properly on stdout.
    I suspect you are wrong there. What you tested was probably slightly different code. I believe this would work:
    Qt Code:
    1. dT.append("Force = %1 +/- %2\n".arg(F,0,'f',0).arg(tolF,0,'f',0));
    To copy to clipboard, switch view to plain text mode 
    or perhaps
    Qt Code:
    1. dT.append(QString("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0));
    To copy to clipboard, switch view to plain text mode 
    (I have trouble predicting when Qt is going to allow auto-conversion of C strings to QStrings.)

  4. #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: Problem with QString .arg()

    I have trouble predicting when Qt is going to allow auto-conversion of C strings to QStrings.
    This a function of C++ and nothing to do with Qt. If the compiler cannot make sense of a parameter as written it will try a single type conversion, using a non-explicit conversion constructor for the allowable classes in that parameter position, and try to make sense of the result. It will never do more than one type conversion and will fail if there is more than one possible option.

    Your first example fails because the constant C string has no associated class and hence no arg() method. It is not a parameter in its own right so no attempt at conversion is made.

    I am absolutely certain that I have explained this poorly and missed nuances.

  5. #5
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QString .arg()

    Thank you all!

    dT.append(QString("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0));

    does it.

Similar Threads

  1. QString problem...
    By Nefastious in forum Newbie
    Replies: 17
    Last Post: 29th September 2009, 16:29
  2. problem in QString
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2008, 13:28
  3. QString problem?
    By raphaelf in forum Newbie
    Replies: 7
    Last Post: 24th June 2008, 09:12
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. QString problem
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 26th October 2006, 19: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
  •  
Qt is a trademark of The Qt Company.