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. So after QString::asprintf was removed, are we supposed to implement printf patterns by ourselves? Why?

So after QString::asprintf was removed, are we supposed to implement printf patterns by ourselves? Why?

Scheduled Pinned Locked Moved General and Desktop
qt5.5qstringformatting
4 Posts 4 Posters 10.8k Views
  • 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.
  • M Offline
    M Offline
    MXXIV
    wrote on 19 Oct 2015, 15:10 last edited by
    #1

    So QString::asprintf known in Qt4 was removed in Qt5. Now we're advised to use the arg method of QString. It has many features allowing application extensibility and whatewer, but doesn't support printf format.

    I have a program where user can specify table-cell formatting using printf format in formatter variable. Since you removed asprintf without replacement, I ended up with about two options:

    1. Do insane things like this:

         QByteArray FSA = formater.toLatin1();
         const char*  fs = FSA.data();
         // Convert format to std::string - See locale pseudo-singleton below
         boost::format f( fs, STDLocaleSingleton::Instance() );
         // And convert back to QString
         return QString::fromLatin1( boost::str( f % valueToBeFormated->toDouble() ).c_str() );
         
         // Probably saves some memory, I think
         class STDLocaleSingleton {
             STDLocaleSingleton(){};
         public:
             static const std::locale &Instance(){
                 static std::locale loc("");
                 return loc;
             }
         };
      
    2. Write the printf code interpreter myself?

    How do you guys format strings using printf codes now, when Qt decided they know better?

    PS.: Nice quotation from docs, underlining how awesome the new functions are:

    If fillChar is '0' (the number 0, ASCII 48), this function will use the locale's zero to pad. For negative numbers, the zero padding will probably appear before the minus sign.

    J 1 Reply Last reply 20 Oct 2015, 02:14
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 19 Oct 2015, 15:32 last edited by
      #2

      Hi, welcome to devnet.

      Since you removed asprintf (...)

      We didn't. This forum is mostly used by users, not creators of Qt ;)

      I have a program where user can specify table-cell formatting using printf format

      That's your choice of a format. Qt doesn't have to support everything you need in every possible situation. My guess is Qt moved away from sprintf-like functions because they are already provided by std::, with which Qt's implementation wasn't fully compatible anyway, they are also i18n and unicode unfriendly.

      If you need sprintf formatting why not just use sprintf?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 19 Oct 2015, 17:58 last edited by
        #3

        If you really do not want to use .arg,
        There is also the option to take qstring.cpp
        from version that has it and make it work again.

        QString &QString::vsprintf(const char* cformat, va_list ap)
        seems not so long.
        and

        QString &QString::sprintf(const char *cformat, ...)
        {
            va_list ap;
            va_start(ap, cformat);
            QString &s = vsprintf(cformat, ap);
            va_end(ap);
            return s;
        }
        
        1 Reply Last reply
        0
        • M MXXIV
          19 Oct 2015, 15:10

          So QString::asprintf known in Qt4 was removed in Qt5. Now we're advised to use the arg method of QString. It has many features allowing application extensibility and whatewer, but doesn't support printf format.

          I have a program where user can specify table-cell formatting using printf format in formatter variable. Since you removed asprintf without replacement, I ended up with about two options:

          1. Do insane things like this:

               QByteArray FSA = formater.toLatin1();
               const char*  fs = FSA.data();
               // Convert format to std::string - See locale pseudo-singleton below
               boost::format f( fs, STDLocaleSingleton::Instance() );
               // And convert back to QString
               return QString::fromLatin1( boost::str( f % valueToBeFormated->toDouble() ).c_str() );
               
               // Probably saves some memory, I think
               class STDLocaleSingleton {
                   STDLocaleSingleton(){};
               public:
                   static const std::locale &Instance(){
                       static std::locale loc("");
                       return loc;
                   }
               };
            
          2. Write the printf code interpreter myself?

          How do you guys format strings using printf codes now, when Qt decided they know better?

          PS.: Nice quotation from docs, underlining how awesome the new functions are:

          If fillChar is '0' (the number 0, ASCII 48), this function will use the locale's zero to pad. For negative numbers, the zero padding will probably appear before the minus sign.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 20 Oct 2015, 02:14 last edited by
          #4

          @MXXIV said:

          So QString::asprintf known in Qt4 was removed in Qt5.

          It's QString::sprintf() that was in Qt 4 but removed in Qt 5.

          Qt 5.5 introduced QString::asprintf() (and QString::vasprintf()), in fact: http://code.woboq.org/qt5/qtbase/src/corelib/tools/qstring.cpp.html#_ZN7QString8asprintfEPKcz (Hmm... there seems to be a documentation bug, and these functions aren't showing up. However, they are there for you to use)

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0

          1/4

          19 Oct 2015, 15:10

          • Login

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