Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Ignore unused args in QString::arg
QtWS25 Last Chance

Ignore unused args in QString::arg

Scheduled Pinned Locked Moved Unsolved General and Desktop
argqstring
3 Posts 2 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PhTe
    wrote on last edited by PhTe
    #1

    Hi,
    I have have a QString which will be set by the user and i want to replace some placeholders in the string.
    At the moment i try to replace the placeholders with the arg function, but it gives me warnings if the user does not use all placeholders that i replace.

    My code looks like this.

    QString str = getUserString();
    str = str.arg(getUsername(), getGroup(), getDate(),);
    

    If the function getUserString returns something like hello-%1-%2-%3 or %3-%2-hello-%1 it works, but if the string contains only two or less placeholders (for example hello-%1-%2 or only hello), i get a warning that arguments are missing.

    How can i ignore unused args?
    Or is there a better way to do this?

    1 Reply Last reply
    0
    • TheBadgerT Offline
      TheBadgerT Offline
      TheBadger
      wrote on last edited by TheBadger
      #2

      Although the idea looks good, it might not have the desired behaviour if I understand it correctly.
      So it seems like you want:
      %1 = User Name
      %2 = Group
      %3= Date

      That would work correctly for your example: hello-%1-%2
      But not at all for the following: hello-%2-%3
      Because if you look at the docs on the QString::arg() function you should be aware that the function does not actually care what the value is, it replaces the lowest number with the first argument. Thus if the lowest number is %2 it will replace it with getUsername().

      I suggest (actually what I do and it seems like QtCreator as well) is to define your own variables with some marker, like for instance:

      • %{USER}
      • %{GROUP}
      • %{DATE}

      Then inform the user to use those variables in his input string, for example: hello-%{USER}-%{DATE}
      Then use code to replace each one individually:

      QString str = getUserString();
      str.replace("%{USER}", getUsername());
      str.replace("%{GROUP}", getGroup());
      str.replace("%{DATE}", getDate());
      

      Another benefit of this is that a user can use the same variable more than once: hello-%{USER}-%{DATE}-%{DATE}

      Edit: Removed str = replace(...) from the example since I see QString::replace() modifies the existing string in place.


      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PhTe
        wrote on last edited by
        #3

        Thanks, i think i will do it so :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved