Results 1 to 8 of 8

Thread: QString arg

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy QString arg

    I want to use QString to produce something like following:

    This is number 99.
    I should note that i want to be able to change it to the following if needed:
    This_is_number_99.
    or this one:
    This-is-number-99.

    in which spaces are dynamic, so I used this code:
    Qt Code:
    1. QString test=" ";
    2. QMessageBox::information(this,"Testing...",tr("This%1is%1number%199.").arg(test));
    To copy to clipboard, switch view to plain text mode 
    the problem is that, because %1 is followed by another number, it does not work as expected, please someone shows me a way to solve this,

    any help is appreciated.
    Last edited by PLUS; 29th January 2010 at 13:38.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString arg

    Qt Code:
    1. QMessageBox::information(this,"Testing...",tr(QString("This is number %1.").arg(99)));
    To copy to clipboard, switch view to plain text mode 

    But you can also do:
    Qt Code:
    1. QMessageBox::information(this,"Testing...",tr(QString("This is number ")+QString::number(99)));
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString arg

    Thanks for your reply, but I think you didn't understand what I mean,

    I want SPACES to be variable and that number is static!!

    please someone helps if there is a way...

    problem is not the number, problem is %1 followed by a number!

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString arg

    That's what high_flyer said applied to your code:
    Qt Code:
    1. QString test=" ";
    2. QMessageBox::information(this,"Testing...",tr("This%1is%1number%1%2.").arg(test).arg(99));
    3.  
    4. // or
    5.  
    6. QString test=" ";
    7. QMessageBox::information(this,"Testing...",tr("This%1is%1number%1.").arg(test)+QString::number(99));
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    PLUS (29th January 2010)

  6. #5
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString arg

    Quote Originally Posted by faldżip View Post
    That's what high_flyer said applied to your code:
    Qt Code:
    1. QString test=" ";
    2. QMessageBox::information(this,"Testing...",tr("This%1is%1number%1%2.").arg(test).arg(99));
    3.  
    4. // or
    5.  
    6. QString test=" ";
    7. QMessageBox::information(this,"Testing...",tr("This%1is%1number%1.").arg(test)+QString::number(99));
    To copy to clipboard, switch view to plain text mode 
    Thanks for your help,

    thats a nice way, but I was looking for a more convenience way, like specifying {%1} or %{1} or similar things.

    so there is no such way,

    again thanks for your reply.

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString arg

    Another thing which came to my mind is that maybe you can use QString::split() and QStringList::join():
    Qt Code:
    1. QString test("_");
    2. QString result = QString("This is number 99.").split(' ').join(test);
    3. // this should give result = "This_is_number_99"
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. The following user says thank you to faldzip for this useful post:

    PLUS (29th January 2010)

  9. #7
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString arg

    Why not just
    Qt Code:
    1. QString txt = "This is text with spaces";
    2. txt.replace(" ", "_");
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  10. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString arg

    Quote Originally Posted by franz View Post
    Why not just
    Qt Code:
    1. QString txt = "This is text with spaces";
    2. txt.replace(" ", "_");
    To copy to clipboard, switch view to plain text mode 
    You're right... -_-'
    sometimes the simplest solution is the last one you are thinking of :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. QString to hex
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2009, 12:21
  2. QString help
    By mabeeh in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 21:50
  3. Replies: 4
    Last Post: 31st January 2008, 20:44
  4. QString
    By krishbhala in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 11:31
  5. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05

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.