QString::sprintf question



  • Hi, I have a silly question regarding QString::sprintf. It is not a static function, so I need to call it on an instance of QString, but this instance does not appear to be used at all, causing me to have to create dummy variables all the time, like this:
    @
    QString stringFromQVector3D(const QVector3D &vector)
    {
    QString dummy;

    return dummy.sprintf("%.3f, %.3f, %.3f", vector.x(), vector.y(), vector.z());
    

    }@

    This works perfectly, but it doesn't seem very elegant. I'm wondering if I'm misunderstanding something.

    I should mention that I prefer the sprintf style formatting as this is way easier for me to read and edit than glueing pieces of strings together.


  • Moderators

    Have a look at the arg function of QString. If you ever need translations it's your best choice

    From the docs :

    @ QString i; // current file's number
    QString total; // number of files to process
    QString fileName; // current file's name

     QString status = QString("Processing file %1 of %2: %3")
                     .arg(i).arg(total).arg(fileName);@
    

  • Moderators

    I guess you have seen the warning in "sprintf description":http://doc.qt.nokia.com/4.7/qstring.html#sprintf Because of that I would twice before using sprintf. Therefore, I could only repeat Eddy's advice.



  • bq. I guess you have seen the warning in sprintf description [doc.qt.nokia.com] Because of that I would twice before using sprintf. Therefore, I could only repeat Eddy’s advice.

    Thanks, I would use something else if there was another "safe" function with sprintf style formatting. Doesn't arg() only work with strings?


  • Moderators

    Afaik arg is only used with QString

    Edit : i didn't mean as argument, because it has several overloaded variants. I meant it's a function that you can find in the QString class only.


  • Moderators

    You got a whole of overlaid methods. Just take your pick.
    This is for "int for example ":http://doc.qt.nokia.com/4.7/qstring.html#arg-10


  • Moderators

    [quote author="Eddy" date="1309451732"]Afaik arg is only used with QString[/quote]

    It writes only to strings, but the actual argument may be different.

    There was an overlap of our replies. :-)



  • Ah, I saw all the overloads now for all the other types.

    arg() could be the next best thing :) Thanks a lot!


  • Moderators

    [quote author="koahnig" date="1309451893"]
    [quote author="Eddy" date="1309451732"]Afaik arg is only used with QString[/quote]

    It writes only to strings, but the actual argument may be different.

    There was an overlap of our replies. :-)
    [/quote]

    Yes i was just editing my post because i tought eonz could have not seen all the overloaded functions


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.