[solved] Doubts using QStringLiteral and QStringBuilder



  • 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 ;-)


  • Moderators

    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()



  • 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?


  • Moderators

    [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 :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.