Results 1 to 8 of 8

Thread: What is best for concatenating values in QString: arg(...) or +?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default What is best for concatenating values in QString: arg(...) or +?

    Hello!

    Suppose I want to create a string that shows a progress status bar, something like:

    "Processing file 1 of 211 files: 5% done".

    I know of two methods one could do that by working with QStrings: one is to pick the numbers and convert them to QStrings by using QString::number(...) and concatenate them using +:

    Qt Code:
    1. "Processing file " + QString::number(currFile) + " of " + QString::number(totalFiles) + " files: " + QString::number(currProgress) + "% done";
    To copy to clipboard, switch view to plain text mode 

    and the other is by using args:

    Qt Code:
    1. "Processing file %1 of %2 files: %3% done".arg(currFile).arg(totalFiles).arg(currProgress);
    To copy to clipboard, switch view to plain text mode 


    So two questions:
    * First, is there another method to do this?
    * Second, which of them is the best for situations like the one mentioned above? - I know that if someone wants to guarantee a minimal amount of numbers to be displayed, arg() is better since number(...) don't provide that functionality, but what about speed and memory efficiency? - Not forgetting to consider in the evaluation some extra options such as to use QStringBuilder to make the + concatenation faster.


    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    First, is there another method to do this?
    Sure, you can use append() or push_back(), operator +=, or even sprintf, but I really doubt you will notice any change in performance with such micro-optimizations.
    Second, which of them is the best for situations like the one mentioned above?
    The most readable one (for me the "arg()" method, but it is a matter of personal preference).
    what about speed and memory efficiency?
    The thing you are doing in the background and stuff related to gui will have much greater impact on the performance than actual string composition.
    Anyway, if you are unsure, then measure, do some performance tests.

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

    Momergil (2nd April 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    The second option (using arg()) is the only one that works for translations.

    Cheers,
    _

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

    Momergil (6th November 2015)

  6. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    Quote Originally Posted by stampede View Post
    Sure, you can use append() or push_back(), operator +=, or even sprintf, but I really doubt you will notice any change in performance with such micro-optimizations.
    Ok, it seems I knew all the other methods all along then Now about performance optimization, well, if one is using a computer with a Intel i7 3.5Ghz or something, I really agree that no one will actually feel any difference, but what about software in embedded system, when processors and memory are far less available? And what about the pride of a programmer in doing not simply "code that works", but "good code that works better", even if just slightly better?


    Now anda_skoa, you mentioned something that really caught my attention... How exactly that only arg() works with translations? I mean, if I change the first example code to

    Qt Code:
    1. tr("Processing file ") + QString::number(currFile) + tr(" of ") + QString::number(totalFiles) + tr(" files: ") + QString::number(currProgress) + tr("% done");
    To copy to clipboard, switch view to plain text mode 

    wouldn't translations works anyway (with the only probable unhappiness of having to translated small, incomplete and uncontextualized strings in QtTranslator?) And, in fact, how exactly one would use the QObject::tr() method using the arg() way? (Sorry if the answer is in QtAssistant; I haven't read all Qt's translation API's documents yet :T)
    May the Lord be with you. Always.

  7. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    wouldn't translations works anyway (with the only probable unhappiness of having to translated small, incomplete and uncontextualized strings in QtTranslator?)
    Exactly, maybe slightly more difficult for a translator do to her job.
    And, in fact, how exactly one would use the QObject::tr() method using the arg() way?
    Qt Code:
    1. //: a special kind of comment that will appear in linguist
    2. tr("Somestring %1").arg(something)
    To copy to clipboard, switch view to plain text mode 
    One of our coding guidelines is to keep the translatable strings separated from string arguments, special markers, parameters etc, so very often you can see code like
    Qt Code:
    1. tr("Some string:") + " " + QString::number(value) + "\n" + tr("Another string") + ...;
    To copy to clipboard, switch view to plain text mode 
    Personally not my favourite style, but its for the sake of people working on translations. From what I've heard, it makes things simpler for them (together with those special kind of comments to describe context etc.)
    what about software in embedded system, when processors and memory are far less available?
    I don't have much experience with that, but I think if it can run Qt gui, then it most definitely can run few string concats
    And what about the pride of a programmer in doing not simply "code that works", but "good code that works better", even if just slightly better?
    For the critical parts of the app, of course. For the rest I'd say "clean code" >> "smart code"

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

    Momergil (2nd April 2014)

  9. #6
    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: What is best for concatenating values in QString: arg(...) or +?

    There is also a third method:

    Qt Code:
    1. #include <QStringBuilder>
    2.  
    3. QString string = "abc" % QString::number(currFile) % ... ;
    To copy to clipboard, switch view to plain text mode 

    This solution is fastest yet less powerful than when using arg() since the latter is better when using tr() for translations.
    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.


  10. The following user says thank you to wysota for this useful post:

    Momergil (3rd April 2014)

  11. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    Quote Originally Posted by Momergil View Post
    Now anda_skoa, you mentioned something that really caught my attention... How exactly that only arg() works with translations? I mean, if I change the first example code to

    Qt Code:
    1. tr("Processing file ") + QString::number(currFile) + tr(" of ") + QString::number(totalFiles) + tr(" files: ") + QString::number(currProgress) + tr("% done");
    To copy to clipboard, switch view to plain text mode 

    wouldn't translations works anyway (with the only probable unhappiness of having to translated small, incomplete and uncontextualized strings in QtTranslator?)
    That only works for languages with exactly the same order of arguments.
    The positional arguments used by QString::arg() allow translators to shft the position of these arguments depending in their target language's needs.

    Quote Originally Posted by Momergil View Post
    And, in fact, how exactly one would use the QObject::tr() method using the arg() way? (Sorry if the answer is in QtAssistant; I haven't read all Qt's translation API's documents yet :T)
    Qt Code:
    1. tr("Processing file %1 of %2 files: %3% done").arg(currFile).arg(totalFiles).arg(currProgress);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    Momergil (3rd April 2014)

  13. #8
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What is best for concatenating values in QString: arg(...) or +?

    Well, thanks both anda_skoa, wysota and stampede for you replies. They will all give me somethings to think about for the next days
    May the Lord be with you. Always.

Similar Threads

  1. Replies: 6
    Last Post: 1st February 2013, 09:32
  2. How to access QString characters values
    By rdelgado in forum Newbie
    Replies: 7
    Last Post: 15th November 2010, 15:01
  3. Replies: 4
    Last Post: 1st February 2010, 15:21
  4. concatenating path QStrings
    By rbp in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 05:46
  5. Concatenating two binary files
    By nbkhwjm in forum Newbie
    Replies: 13
    Last Post: 23rd April 2007, 04:30

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.