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. why to use QStringLiteral instead of QString

why to use QStringLiteral instead of QString

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

    please look for follwing code

    //your code here
     void setupUi(QMainWindow *Analyzer)
        {
            if (Analyzer->objectName().isEmpty())
                Analyzer->setObjectName(QStringLiteral("Analyzer"));
    

    why we can't use Analyzer->setObjectName(QString("Analyzer"));
    or
    Analyzer->setObjectName("Analyzer");

    JKSHJ 1 Reply Last reply
    0
    • G guru007

      please look for follwing code

      //your code here
       void setupUi(QMainWindow *Analyzer)
          {
              if (Analyzer->objectName().isEmpty())
                  Analyzer->setObjectName(QStringLiteral("Analyzer"));
      

      why we can't use Analyzer->setObjectName(QString("Analyzer"));
      or
      Analyzer->setObjectName("Analyzer");

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @guru007 said in why to use QStringLiteral instead of QString:

      why we can't use Analyzer->setObjectName(QString("Analyzer"));
      or
      Analyzer->setObjectName("Analyzer");

      See the QString documentation at http://doc.qt.io/qt-5/qstring.html#more-efficient-string-construction :

      "Many strings are known at compile time. But the trivial constructor QString("Hello"), will copy the contents of the string, treating the contents as Latin-1. To avoid this one can use the QStringLiteral macro to directly create the required data at compile time. Constructing a QString out of the literal does then not cause any overhead at runtime."

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        A detailed explanation is also available here: https://woboq.com/blog/qstringliteral.html (oddly enough it's the result #1 on my Google for the qstringliteral query)

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • G Offline
          G Offline
          guru007
          wrote on last edited by
          #4

          Thanks for ur replies....
          Is there any situation where we have to specifically used QStringliteral over to QString.

          mrjjM 1 Reply Last reply
          0
          • G guru007

            Thanks for ur replies....
            Is there any situation where we have to specifically used QStringliteral over to QString.

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

            @guru007
            Hi
            Its an optimization. You use QStringLiteral to avoid to constructing a QString and then copy data.
            So it can make a difference in loops where lots of strings are used and other cases with
            lots of access.
            For a small program, it won't really matter but why not just use in all cases where it fits.
            It won't hurt performance and there is no reason not to.

            I cant think of a case where you HAVE to use QStringliteral.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              The only downside I found is that, being a macro, it breaks inline static code analysis in Visual Studio. Definitely a minor thing that should not stop you using it.

              The only case where it'd avoid it is if there is an explicit overload for const char* or QLatin1String. for example:

              QString base("base");
              base.prepend("prepended"); //better
              base.prepend(QStringLiteral("prepended")); //worse
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1

              • Login

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