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. [SOLVED] String formating %1%2 vs %2%1
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] String formating %1%2 vs %2%1

Scheduled Pinned Locked Moved General and Desktop
23 Posts 4 Posters 6.7k 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.
  • G Offline
    G Offline
    gkavrecic
    wrote on last edited by
    #1

    I've encountered a strange issue while formatting QString.

    A very simplified description:
    format string was %2%1
    arg 1 was int (e.g. 1)
    arg 2 was QString (e.g. D)
    the result was "D" - the integer went missing

    When swapped the 2 params it formatted correctly.
    I use 4.8.1
    Is anyone aware of this issue?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Salvatello
      wrote on last edited by
      #2

      (QString("%1/%2").arg(intero).arg(licenza));

      intero is a qint16, and licenza is a qstring;

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gkavrecic
        wrote on last edited by
        #3

        Hi Salvatelo

        I know the syntax.

        If I use your case: (!I have not tested this specific code, just a concept)
        a) OK: QString(”%1%2”).arg(intero).arg(licenza)
        b) Fails: QString(”%2%1”).arg(licenza).arg(intero)
        Theoretically, this should give the same result. Correct?
        Note: There is no whitespave or any symbol between the two placeholders.
        In case I inserted a space between %2 & %1 case B would work.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Salvatello
          wrote on last edited by
          #4

          @ qint16 a=3;
          ui->LineEdit->setText((QString("%2/%1").arg(licenza).arg(a)));@

          the result is 3 / licenza;

          @ qint16 a=3;
          ui->LineEdit->setText((QString("%1/%2").arg(a).arg(licenza)));@

          the result is 3 / licenza;

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gkavrecic
            wrote on last edited by
            #5

            There is a misunderstanding.

            There is no space or any other symbol between %2 and %1 - "%2%1"

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Salvatello
              wrote on last edited by
              #6

              I did not understand

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gkavrecic
                wrote on last edited by
                #7

                As I'll have a few minutes of time I'll make minimalistic source to reproduce this

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  frankiefrank
                  wrote on last edited by
                  #8

                  Could you paste your code exactly as it is?

                  "Roads? Where we're going, we don't need roads."

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gkavrecic
                    wrote on last edited by
                    #9

                    Frankie

                    I'll put together a simple code to reproduce the issue on a smaller scale.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Saugglocke
                      wrote on last edited by
                      #10

                      Hi gkavrecic,

                      its because arg looks for the smallest place marker, like %1, %2 ... if you replace the first marker with a digit, e. g. 3 it will find the next marker as # and arg will replace the marker (#) with "D".

                      best regards

                      PS: why was the rest of my post missing?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gkavrecic
                        wrote on last edited by
                        #11

                        So, this means that the markers (when concatenated) have to be in ascending order?

                        Is this described anywhere?

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Saugglocke
                          wrote on last edited by
                          #12

                          Hm, whats wrong with the forum? % 23 gets replaced with #??

                          Look here: http://qt-project.org/doc/qt-4.8/qstring.html#arg

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gkavrecic
                            wrote on last edited by
                            #13

                            Nothing wrong with the forum.

                            According to docs (also your link):
                            ...each arg() will still replace the lowest numbered unreplaced place marker, no matter where it appears...

                            According to my experience this is not true.

                            I'm going to write the test appl now.
                            I still might find my bug doing it...

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              Saugglocke
                              wrote on last edited by
                              #14

                              Yeah, if you replace the first occurence in %2%1 with a digit the new marker will be %2x, where x is the digit. So the new smallest marker is 21, 22 depending on what digit x has been.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                gkavrecic
                                wrote on last edited by
                                #15

                                ok, here is the minimalistic appl to prove this.

                                @#include <QtCore/QCoreApplication>
                                #include <QDebug>

                                int main(int argc, char *argv[])
                                {
                                int i = 7;
                                QString s = "D";

                                QString a = QString("%1%2").arg(s).arg(i,-2);
                                QString b = QString("%2%1").arg(i,-2).arg(s);
                                
                                qDebug() << a;
                                qDebug() << b;
                                
                                return 0;
                                

                                }@

                                The output of this code is (copy/pasted from the console window):
                                @"D7 "
                                "D "@

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Saugglocke
                                  wrote on last edited by
                                  #16

                                  Its working as expected, just try it this way:

                                  @ QString b = QString("%2%1").arg(i,-2);
                                  qDebug() << b; // = "% 27"
                                  b = b.arg(s); // now arg finds 27 as smalles marker and will replace it
                                  qDebug() << b;@

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    gkavrecic
                                    wrote on last edited by
                                    #17

                                    Saugglocke,
                                    I was just thinking this before I refreshed the page...
                                    It seems it has a very simplistic logic while converting it - after each argument it reuses the new string.

                                    But with this logic one can't, for example, internationalise some monetary value like 5€ or $8 using the logic to switch places to value and currency...

                                    You come back to position aware programming as it is in sprintf...
                                    Not all the way, but still. You can't describe it as no matter where it appears

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      gkavrecic
                                      wrote on last edited by
                                      #18

                                      a reply to your sample code.
                                      It does not work.

                                      • the intermediate result is "%27 "

                                      • the final one is "D " - same as before, just need more code

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Saugglocke
                                        wrote on last edited by
                                        #19

                                        args return value is a new string, hence the next arg will use the copy.

                                        The sample was just to show the logic behind arg. It won't solve the problem. You could add a space character and replace it afterwards:

                                        @QString b = QString("%2 %1").arg(i,-2).arg(s);
                                        qDebug() << b.replace(" ", "");@

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          Salvatello
                                          wrote on last edited by
                                          #20

                                          @ QString a = QString("%1%2").arg(s).arg(i,-2);@
                                          is a correct form.

                                          @ QString b = QString("%2%1").arg(i,-2).arg(s);@
                                          not a correct form.

                                          %1 must precede %2.
                                          you can change the order of arg bat not the order of %n.

                                          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