Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiline strings



  • I tryed to do the following to break a long string:

        ListElement {
            name: "Mercury"
            imageSource: "images/mercury.jpeg"
            facts: "Mercury is the smallest planet in the Solar System. " +
                   "It is the closest planet to the sun. It makes one trip " +
                   "around the Sun once every 87.969 days."
        }
    

    However I get complains that i cannot use script for property value.

    So what is the correct way to break longer strings on several lines for readability in qml?



  • What works:

        ListElement {
            name: "Mercury"
            imageSource: "images/mercury.jpeg"
            facts: "Mercury is the smallest planet in the Solar System. \
                   It is the closest planet to the sun. It makes one trip \
                   around the Sun once every 87.969 days."
        }
    

    Is that the best way?



  • "What is the best way?", - I think that is subjective, if it works, then....it works.

    You could also do;

        ListElement {
            name: "Mercury"
            imageSource: "images/mercury.jpeg"
            facts: "Mercury is the smallest planet in the Solar System.</p>
                    <p>It is the closest planet to the sun. It makes one trip</p>
                    <p>around the Sun once every 87.969 days.</p>"
        }
    


  • @sandro4912 said in Multiline strings:

    So what is the correct way to break longer strings on several lines for readability in qml?

    Hello, this is not a problem about line break, but the way you construct the final string which is not compatible with ListElement limitation. Read this extract from documentation:

    The names used for roles must begin with a lower-case letter and should be common to all elements in a given model. Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).

    You can only use constants with ListElement
    So you have to use \ to continue the string on next line, or write all in one line. You cannot use string concatenation in this special case.


Log in to reply