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. Create QString with QStringLiteral and arg
Forum Updated to NodeBB v4.3 + New Features

Create QString with QStringLiteral and arg

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 8.1k Views 2 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.
  • R Offline
    R Offline
    Rondog
    wrote on last edited by
    #2

    @tham said in Create QString with QStringLiteral and arg:

    QString const birth = QStringLiteral("%1/%2/%3").arg(year).arg(date).arg(time);

    I pretty sure you can't use arguments with a QStringLiteral so this won't work (not on my system at least).

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #3

      Hi
      It does seems to work

      alt text

      With QStringLiteral it makes a QStaticStringData
      else not.

      JonBJ 1 Reply Last reply
      2
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #4

        I also tried it and found it worked. My bad.

        I remember trying this at some point in the past and found it didn't work (compile error). For this reason I have always been in the habit of using QString instead of QStringLiteral for anything with arguments. From what I understand QStringLiteral is a macro wrapper that makes static text more memory and CPU friendly (it create a QString directly at compile time and not through a constructor from a char pointer) so I always assumed it was specifically aimed at static text.

        mrjjM 1 Reply Last reply
        2
        • R Rondog

          I also tried it and found it worked. My bad.

          I remember trying this at some point in the past and found it didn't work (compile error). For this reason I have always been in the habit of using QString instead of QStringLiteral for anything with arguments. From what I understand QStringLiteral is a macro wrapper that makes static text more memory and CPU friendly (it create a QString directly at compile time and not through a constructor from a char pointer) so I always assumed it was specifically aimed at static text.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Rondog

          Hi
          I also though it would not work as it not very static in nature but it seems
          to create this static struct regardless.
          But since we alter it a runtime, it cant be in read only memory so there is still something
          fishy here :)

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            It does seems to work

            alt text

            With QStringLiteral it makes a QStaticStringData
            else not.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #6

            I don't see the problem/issue.
            https://woboq.com/blog/qstringliteral.html, http://blog.qt.io/blog/2014/06/13/qt-weekly-13-qstringliteral/, https://www.kdab.com/qstringview-diaries-advances-qstringliteral/ may help.

            And I assume that while QStringLiteral("%1/%2/%3") is "a QStringLiteral" (sort of), once you go QStringLiteral("%1/%2/%3").arg(...) then what you get back is a QString, as per e.g. http://doc.qt.io/qt-5/qstring.html#arg

            ?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @JonB
              Hi
              good links. There is nothing wrong but clearly the whole expression cannot be read only so
              i guess only "%1/%2/%3" is in readonly section and rest is dynamic.

              JonBJ 1 Reply Last reply
              0
              • mrjjM mrjj

                @JonB
                Hi
                good links. There is nothing wrong but clearly the whole expression cannot be read only so
                i guess only "%1/%2/%3" is in readonly section and rest is dynamic.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #8

                @mrjj
                I believe:

                • "%1/%2/%3" => read-only/literal string
                • QStringLiteral("%1/%2/%3") => read-only/literal string
                • QStringLiteral("%1/%2/%3").arg(...) => function QString::arg() returns a plain QString => whatever rules for QString only, not per se read-only/literal string

                That's the point --- it's the use of the arg() function that changes the "type".

                1 Reply Last reply
                2
                • thamT Offline
                  thamT Offline
                  tham
                  wrote on last edited by
                  #9

                  I guess we can say QStringLiteral may offer some small optimization in this case(part of the string could construct at compile time, no dynamic allocation), unless it wouldn't worse than construct by QString directly?

                  kshegunovK 1 Reply Last reply
                  0
                  • thamT tham

                    I guess we can say QStringLiteral may offer some small optimization in this case(part of the string could construct at compile time, no dynamic allocation), unless it wouldn't worse than construct by QString directly?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #10

                    QStringLiteral will construct a QString object once in the program lifetime from the const char * you pass it. This is happening through a bit of static initialization magic. After that you can use that QString object wherever you feel like it. The idea is that shallow copies of QString (due to it having implicit sharing) is very cheap. If you use "write" operations on it the data will be detached, as it's the case here - arg will copy out the literal part to do the replacement. However you'd still get some small optimization for the initial string construction.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    5
                    • JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #11

                      Yes, the initial use of QStringLiteral may save you at least 1 nanosecond or 10 bytes in your overall program... :)

                      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