Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString::sprintf question
Forum Updated to NodeBB v4.3 + New Features

QString::sprintf question

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 27.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • EddyE Offline
    EddyE Offline
    Eddy
    wrote on last edited by
    #2

    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);@
    

    Qt Certified Specialist
    www.edalsolutions.be

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #3

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eonz
        wrote on last edited by
        #4

        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?

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #5

          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.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #6

            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

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              [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. :-)

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Eonz
                wrote on last edited by
                #8

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

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

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #9

                  [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

                  Qt Certified Specialist
                  www.edalsolutions.be

                  M 1 Reply Last reply
                  0
                  • EddyE Eddy

                    [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

                    M Offline
                    M Offline
                    Morbius
                    wrote on last edited by Morbius
                    #10

                    @Eddy:
                    To answer your ? as asked, without trying to sell someone on the Qt way of doing arguments:

                    return QString().asprintf("%.3f, %.3f, %.3f", vector.x(), vector.y(), vector.z());

                    Note that the Qt arguments version lacks the three decimal points specifications given in the original question. Note also that while three decimal points can be done in the Qt arguments way of doing it, there is no single table that pulls together all the args() specifications conveniently, like a printf table does. The .args() way of doing it isn't always more convenient.

                    Note that this doesn't limit the size of the result; and can result in buffer overflows, if you e.g. are printing a string with %s or %ls. While it is argued that you should use .arg() because it supports Unicode unlike QString::asprintf(), this is untrue. The documentation says that if your format specifier is %ls, it will print a Unicode string. The documentation claims that asprintf() will safely build a string, but that sprintf() is obsolete - although I am unclear on any real difference.

                    The argument has been made that snprintf should not be used, since different compilers and locales may cause differing lengths of strings; but I'd say it is more important not to overflow buffers, or even use huge, unintended chunks of memory with a %s specifier, than it is to get every character in.

                    You can use Unicode in such a context. Here's a £337 way of doing it:

                    wchar_t buf[100];
                    std::swprintf(buf, 100, L"%.3f, %.3f, %.3f, %s", vector.x(), vector.y(), vector.z(), someWCharString);
                    return QString().fromWCharArray(buf);

                    Chris KawaC 1 Reply Last reply
                    1
                    • M Morbius

                      @Eddy:
                      To answer your ? as asked, without trying to sell someone on the Qt way of doing arguments:

                      return QString().asprintf("%.3f, %.3f, %.3f", vector.x(), vector.y(), vector.z());

                      Note that the Qt arguments version lacks the three decimal points specifications given in the original question. Note also that while three decimal points can be done in the Qt arguments way of doing it, there is no single table that pulls together all the args() specifications conveniently, like a printf table does. The .args() way of doing it isn't always more convenient.

                      Note that this doesn't limit the size of the result; and can result in buffer overflows, if you e.g. are printing a string with %s or %ls. While it is argued that you should use .arg() because it supports Unicode unlike QString::asprintf(), this is untrue. The documentation says that if your format specifier is %ls, it will print a Unicode string. The documentation claims that asprintf() will safely build a string, but that sprintf() is obsolete - although I am unclear on any real difference.

                      The argument has been made that snprintf should not be used, since different compilers and locales may cause differing lengths of strings; but I'd say it is more important not to overflow buffers, or even use huge, unintended chunks of memory with a %s specifier, than it is to get every character in.

                      You can use Unicode in such a context. Here's a £337 way of doing it:

                      wchar_t buf[100];
                      std::swprintf(buf, 100, L"%.3f, %.3f, %.3f, %s", vector.x(), vector.y(), vector.z(), someWCharString);
                      return QString().fromWCharArray(buf);

                      Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Morbius Both asprintf and fromWCharArray are static methods. Don't create temporary instances just to call them. Just use directly: QString::asprintf(... and QString::fromWCharArray(...

                      You don't need std to use Unicode (and by Unicode I mean UTF-16, just to be clear, because there's more then one type of Unicode).
                      QString is UTF-16 internally so there's no point in doing it via std::swprintf. Format string in QString::asprintf is expected to be UTF-8 so it can hold whatever UTF-16 can. If you have arguments that are UTF-16 strings then these are also supported directly via %lc and %ls.

                      1 Reply Last reply
                      3

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved