Results 1 to 4 of 4

Thread: Good way to use QString.arg() to just format an int?

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Good way to use QString.arg() to just format an int?

    Hi,

    I want to convert some integer to hex-string and pad it with 0.

    So far I would do, e.g.

    Qt Code:
    1. QString().sprintf("%.8X ", quint32(111));
    To copy to clipboard, switch view to plain text mode 

    Now I am thinking about dropping use of format strings in new code, and Qt tells me to avoid Qstring::sprintf too and use arg() instead. So I could do

    Qt Code:
    1. QString("%1").arg(111, 8, 16, '0')
    To copy to clipboard, switch view to plain text mode 


    Question: Is there any way to use arg() to avoid having to parse the "%1" format string and just get the string value of arg() call?

    Is there any other Qt-way to achieve this with more performance?

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Good way to use QString.arg() to just format an int?

    Question: Is there any way to use arg() to avoid having to parse the "%1" format string and just get the string value of arg() call?
    Usually you would be using arg() with a variable argument, not a fixed value like 111. If you are formatting a hex string with a fixed value, then just create the string literal and be done with it. The "%1" syntax is just a way to put placeholders in a QString when using arg() with variable arguments to say "Put the result of the first arg() expression into this position". Usually, it is more common for the placeholders to be part of a longer formatted string, not just the placeholder all alone:

    Qt Code:
    1. int a = 111;
    2. QString( "The conversion of %1 to hex is %2" ).arg( a ).arg( a, 8, 16, '0' );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Good way to use QString.arg() to just format an int?

    I just need the sprintf'ing effect of arg().


    Essentially I want to string-ify a variable (or constant, doesnt matter) to a string of format "as hex, and padded with zeros up to 8 chars".


    Both sprintf and arg() require parsing a format string, that (as you said) i am not really using. So I am looking for the best way to do this via Qt, but it looks like there is no better way.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Good way to use QString.arg() to just format an int?

    I suppose QString::number() with a base = 16 would do it, but it won't give you any format or padding options. But basically, if you want to use QString::arg(), you also must use a placeholder argument in the QString literal that forms the left-hand-side of the expression. Not sure why you object to this so much - it's the way the function is defined. Not a whole lot different from Python's "{0} {1}" formatting convention (although supposedly Python 3.1 lets you get away with "{} {}", but the string literal with braces is still required to format the output).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Converting QString to QDateTime with format
    By dantom in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2018, 17:51
  2. How can i convert qstring to money format ?
    By newermind in forum Qt Programming
    Replies: 9
    Last Post: 17th April 2016, 23:19
  3. Need suggestion for to write QString to JSON format
    By wagmare in forum Qt Programming
    Replies: 1
    Last Post: 17th September 2014, 10:16
  4. format a QString like printf
    By hiotreaw in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2011, 12:07
  5. Replies: 16
    Last Post: 21st May 2011, 13:22

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.