Results 1 to 4 of 4

Thread: QString equivalent to sprint( str, "%hd",...)

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString equivalent to sprint( str, "%hd",...)

    Is there an equivalent to sprintf( str, "%hd", intToShort) for QString::arg? I noticed in the docs that QString::sprintf does not support the %h flag, but is there a way to write the short?

    Thanks
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString equivalent to sprint( str, "%hd",...)

    just use QString("....%1....").arg(x); while this will probably use the int overload, this will work.

  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Post Re: QString equivalent to sprint( str, "%hd",...)

    Qt Code:
    1. signed short int shi = 10;
    2. unsigned short int usi = 11;
    3. signed long int sli = 12;
    4. unsigned long int uli = 13;
    5. QString qs = "Hello World";
    6.  
    7. QString mystring = QString("%1 - %2 - %3 - %4 - %5").arg(shi).arg(usi).arg(sli).arg(uli).arg(qs);
    To copy to clipboard, switch view to plain text mode 
    This will result in:
    10 - 11 - 12 - 13 - Hello World

    Just as easy as that. If you need to make sure data is short and not long, double or other, you should typecast or convert instead of relying on %h from sprintf.

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

    Default Re: QString equivalent to sprint( str, "%hd",...)

    Quote Originally Posted by luf View Post
    If you need to make sure data is short and not long, double or other, you should typecast or convert instead of relying on %h from sprintf.
    ... or use additional parameters to QString::arg().
    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.


Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

Tags for this Thread

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.