Results 1 to 5 of 5

Thread: how convert numbers into text

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default how convert numbers into text

    How do you convert numbers into text for QString.
    For example, I want to build a word 2house4 using append(). I know how to make such a string but I want the string to be all text (the numbers should also be treated as text):

    Qt Code:
    1. QString test;
    2. test.append(2);
    3. test.append("house");
    4. test.append(4);
    To copy to clipboard, switch view to plain text mode 

    This doesn't work:
    Qt Code:
    1. QString test;
    2. test.append(2).text();
    3. test.append("house");
    4. test.append(4).text();
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how convert numbers into text

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    tommy (3rd August 2009)

  4. #3
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how convert numbers into text

    Quote Originally Posted by tommy View Post
    How do you convert numbers into text for QString.
    For example, I want to build a word 2house4 using append(). I know how to make such a string but I want the string to be all text (the numbers should also be treated as text):

    Qt Code:
    1. QString test;
    2. test.append(2);
    3. test.append("house");
    4. test.append(4);
    To copy to clipboard, switch view to plain text mode 

    This doesn't work:
    Qt Code:
    1. QString test;
    2. test.append(2).text();
    3. test.append("house");
    4. test.append(4).text();
    To copy to clipboard, switch view to plain text mode 

    thanks
    You can do it like this :

    Qt Code:
    1. QString test;
    2. test = QString ( "%1%2%3" ).arg ( 2 ).arg ( "house" ).arg ( 4 );
    To copy to clipboard, switch view to plain text mode 
    If all three paarams where strings ( up to 10 ), you can use alternative syntax :

    Qt Code:
    1. test = QString ( "%1%2%3" ).arg ( "2", "house", "4" );
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    80
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: how convert numbers into text

    or simply do something like:

    Qt Code:
    1. QString test;
    2. test.append( QString(%1).arg(2) );
    3. test.append("house");
    4. test.append( QString(%1).arg(4) );
    To copy to clipboard, switch view to plain text mode 

    that could also be:

    Qt Code:
    1. int a;
    2. int b;
    3.  
    4. a = 2;
    5. b = 4;
    6.  
    7. QString test;
    8. test.append( QString(%1).arg(a) );
    9. test.append("house");
    10. test.append( QString(%1).arg(b) );
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how convert numbers into text

    you missed quotes in your example
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    should be
    Qt Code:
    1. QString("%1")
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 0
    Last Post: 10th April 2009, 15:28
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  5. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 11:10

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.