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] Doubts using QStringLiteral and QStringBuilder
Forum Updated to NodeBB v4.3 + New Features

[solved] Doubts using QStringLiteral and QStringBuilder

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.2k 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.
  • L Offline
    L Offline
    luisborlido
    wrote on last edited by
    #1

    Hi!

    I've been reading the Qt documentation on "QString":http://qt-project.org/doc/qt-5/qstring.html to see how to correctly use it and I am a little bit confused.
    In the documentation there is a macro called "QStringLiteral":http://qt-project.org/doc/qt-5/qstring.html#QStringLiteral, For what I understood, we should use it, depending of the cases, to prevent memory allocation, copy and conversion of const char*. Also that we should only use it when does not exist an overload to use "QLatin1String":http://qt-project.org/doc/qt-5/QLatin1String.html. But in the example for this case it is used QString::operator==, but the available overloads are:
    @bool operator==(const QByteArray & other) const
    bool operator==(const char * other) const@

    So instead of
    @if (attribute.name() == QLatin1String("http-contents-length")) //...@

    should not be written
    @if (attribute.name() == "http-contents-length") //... ?@

    Also, what of the following best describes an optimized initialization of a QString?
    @QString first = "ångström";
    QString second = QLatin1String("ångström");
    QString third = QStringLiteral("ångström");@

    There is also another topic where I have some doubts: "More Efficient String Construction":http://qt-project.org/doc/qt-5/qstring.html#more-efficient-string-construction.
    In this topic one refers that can be used QStringBuilder and gives the following example:
    @QString foo;
    QString type = "long";

    foo->setText(QLatin1String("vector<") + type + QLatin1String(">::iterator"));

    if (foo.startsWith("(" + type + ") 0x"))
    ...@

    But one does not shows the alternate version using the QStringBuilder. Is this the best (optimized) way to use it?
    @
    #include <QStringBuilder>
    ...
    QString foo;
    QString type = "long";

    foo->setText("vector<" % type % ">::iterator");

    if (foo.startsWith("(" % type % ") 0x"))
    ...@

    And finally, is this correct?
    @QString testString = tempString % " minutes";
    if (executeThisCondition)
    testString = testString & " or seconds";@

    Or should we use QString::operator+= and QString::append?

    Thanks for your time and patiente ;-)

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      This post is much better suited for Interest mailing list, where the developers who wrote QString are present.

      I'll give you some answers, but treat them with caution, as I am far from being an expert on this.

      I think in your first example, the comparison with const char is fine

      I think the best is a fourth option, where you don't create any temporary value: @ QString third("ångström"); @

      QStringBuilder is used automatically by QString in Qt 5.x and Qt 4.8+. AFAIK, you do not need to use any special syntax for it to work

      If you want other developers to understand your code, use operator+= or ::append()

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luisborlido
        wrote on last edited by
        #3

        Thanks for your reply. I'm sorry for the the delay; I've been busy with other projects and hadn't the time to check out the forum.

        I think there is no need to send to mailing list since you have fulfilled all my doubts.

        Do I need to change the subject to close this discussion?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          [quote author="luisborlido" date="1413201000"]Thanks for your reply. I'm sorry for the the delay; I've been busy with other projects and hadn't the time to check out the forum.

          I think there is no need to send to mailing list since you have fulfilled all my doubts.

          Do I need to change the subject to close this discussion?[/quote]

          OK, no problem.

          I've marked the thread as solved for you. Have fun :-)

          (Z(:^

          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