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. Problem with QString and it arg()
Forum Updated to NodeBB v4.3 + New Features

Problem with QString and it arg()

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 2.9k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aalderweireldt
    wrote on 20 Jan 2015, 10:48 last edited by
    #1

    If I reorder my "%"-placeholders, QString with arg can't process them correctly.
    I can't find any documentation on what I am doing wrong. This happens on Qt 4.8.5 and Qt 5.2
    Code below demonstrates the issue:
    Test 4 fails.. it fails to replace a placeholder..

    @ void UnitTests::test_stringArgs()
    {
    QString expected = QString("Area52");
    QString test1 = QString("%1%2%3").arg("Area").arg(5).arg(2);
    QString test2 = QString("%1%3_%2_").arg("Area").arg(2).arg(5);
    QString test3 = QString("%1%3%2").arg("Area").arg(2);
    QString test4 = QString("%1%3%2").arg("Area").arg(2).arg(5);

        QCOMPARE(expected, test1);
        QCOMPARE(QString("_Area_5_2_"), test2);
        // code block remove my %3, hence the " ' " after it 
        QCOMPARE(QString("_Area%3'2_"), test3);  
        QCOMPARE(expected, test4);
    }
    
    FAIL!  on line 13: test_stringArgs() Compared values are not the same
      Actual (expected): _Area52_
      Expected (test4): _Area5_@
    

    Is this a QString bug?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      roseicollis
      wrote on 20 Jan 2015, 12:14 last edited by
      #2

      Hi, dunno how this works exactly but could the problem be in the order?

      u have:
      QString test4 = QString("%1%3%2").arg("Area").arg(2).arg(5);
      it should be:
      QString test4 = QString("%1%3%2").arg("Area").arg(5).arg(2);

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aalderweireldt
        wrote on 20 Jan 2015, 15:59 last edited by
        #3

        Swapping the arguments is done on purpose..
        Notice that %3 is place in front of %2

        Constructing a "%1%2%3" string works fine
        Constructing a "%1%3%2" fails.. %2 gets lost somehow?

        [quote author="roseicollis" date="1421756070"]Hi, dunno how this works exactly but could the problem be in the order?

        u have:
        QString test4 = QString("%1%3%2").arg("Area").arg(2).arg(5);
        it should be:
        QString test4 = QString("%1%3%2").arg("Area").arg(5).arg(2);[/quote]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on 20 Jan 2015, 19:59 last edited by
          #4

          I can confirm that on Windows Qt 4.8.6 VS 2010 I see the following behavior:
          @
          QString test4 = QString("%1%3%2").arg("Area").arg(20).arg(50);
          QString test4m = QString("%1%2%3").arg("Area").arg(50).arg(20);
          Q_ASSERT (test4 == test4m);

          Assert fails due to:

          • test4 "Area500" QString
          • test4m "Area5020" QString
            @
            It looks like first digit of the %2 is skipped
            I suggest to report bug.
            It is funny bug was not found yet.

          Alex

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hskoglund
            wrote on 20 Jan 2015, 20:12 last edited by
            #5

            Hi, I see the bug also on my Mac with Qt 5.4: if the first character in %2 is a digit 0-9 it gets eaten. Good find!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SysTech
              wrote on 20 Jan 2015, 21:58 last edited by
              #6

              I to can confirm this on Qt 5.4.x on Win 8.1 using MSVC2013 32 bit:

              "Area500"
              "Area5020"

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aalderweireldt
                wrote on 21 Jan 2015, 14:16 last edited by
                #7

                Seems to be wanted behavior.....

                The %1%3%2 with arg("Area") becomes Area%3%2
                Area%3%2 with arg(20) becomes Area%3 20
                Now note how the documentation says that it will replace % up to 99, so...
                Area%3 20 with arg(50) becomes Area500 as it will only replace 2, not %3

                Although I think this is unwanted behavior for most of the people using the qt library. We heavily use placeholders for translations and thus don't control the arg input. I don't want arg to dynamicly change my placeholders..

                https://bugreports.qt.io/browse/QTBUG-44044

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hskoglund
                  wrote on 21 Jan 2015, 15:01 last edited by
                  #8

                  Aha, thanks for posting this, I've seen in the docs for QString that there's actually 2 ways you can use the arg() function:

                  string.arg(1).arg(2).arg(3);     or
                  string.arg(1,2,3);

                  and the docs says that the difference is that the 2nd version "replaces the arguments in one pass". Until know I didn't really pay attention to what that meant. So this implies that the 1st version iterates the replacements, so indeed this is wanted (designed) behavior.
                  But it sure looked like a bug :-)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on 22 Jan 2015, 02:32 last edited by
                    #9

                    I would say it seems reasonable if you count that arg is just a function,
                    Qt documentation should have warning about usage of the string.arg(1).arg(2).arg(3); pattern

                    I just hope that in my Qt career I have not introduced such disaster. The only hope is that my strings always had some kind of additional separators.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 25 Jan 2015, 23:40 last edited by
                      #10

                      Documentation improvement is on its way

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      6/10

                      20 Jan 2015, 21:58

                      • Login

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