How to put a long string to ListElement?
I have to use a long string to ListElements, such as
- ListElement {
- myColor: "red"
- myText: "This is a very long paragaph This is a very long paragaphThis is a very long paragaphThis is a very long paragaphThis is a very long paragaphThis is a very long paragaphThis is a very long paragaph"
- }
Firstly I think I could use something like
- ListElement {
- myColor: "red"
- myText: "This is a very long paragaph This is a very long paragaph"
- +"This is a very long paragaphThis is a very long paragaph"
- +" This is a very long paragaphThis is a very long paragaph"
- +"This is a very long paragaph"
- }
But The ListElement can not recongnize the + parts. only knows the first line.
Then I try do
- Rectangle {
- id:myRect
- property string longText: "This is a very long paragaph This is a very long paragaph"
- +"This is a very long paragaphThis is a very long paragaph"
- +" This is a very long paragaphThis is a very long paragaph"
- +"This is a very long paragaph"
- ListModel {
- ListElement {
- myColor: "red"
- myText: myRect.longText
- }
- }
- }
And I find ListElement don’t allow properties in there data.
Is there a special technique to write a long String in multi-lines, or I have to use a javascript variable?
Thank you.
9 replies
I am not sure I understand your issue, but did you try this:
- Rectangle {
- id:myRect
- ListModel {
- ListElement {
- myColor: "red"
- myText: "This is a very long paragaph This is a very long paragaph \
- This is a very long paragaphThis is a very long paragaph \
- This is a very long paragaphThis is a very long paragaph \
- This is a very long paragaph"
- }
- }
- }
Regards,
Bill
Thank you for you post. However, the spaces are still recognized. But it is much better than before.
The code below show what I want.
- Rectangle {
- id:myRect
- ListModel {
- ListElement {
- myColor: "red"
- myText: "This is a very long paragaph This is a very long paragaph \
- This is a very long paragaphThis is a very long paragaph \
- This is a very long paragaphThis is a very long paragaph \
- This is a very long paragaph"
- }
- }
- }
However, this is ugly when looks at the codes. Any more ideas?
I am not sure I understand your issue, but did you try this:
Rectangle { id:myRect ListModel { ListElement { myColor: "red" myText: "This is a very long paragaph This is a very long paragaph \ This is a very long paragaphThis is a very long paragaph \ This is a very long paragaphThis is a very long paragaph \ This is a very long paragaph" } } }Regards,
Bill
Hi,
If we substitute the ListElement myText with the property longText we get the error “ListElement: cannot use script for property value”. Indeed, the property needs a processing that is not allowed. According to documentation [qt-project.org] “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). “.
We can use JavaScript arrays for multiline text. See for example here [qt-project.org].
Regards
Here’s a possible solution.
- Rectangle {
- id: myrect
- width: 360
- height: 360
- ListView { anchors.fill: parent; model: mymodel;
- delegate: Rectangle {
- height: 100
- width: 360
- Text {
- anchors.centerIn: parent; text: model.myText; width: parent.width
- wrapMode: Text.WordWrap
- }
- }
- }
- ListModel {
- id: mymodel
- }
- Component.onCompleted: {
- mymodel.append({"myColor":"red", "myText":
- "This is a very long paragaph This is a very long paragaph"
- +"This is a very long paragaphThis is a very long paragaph"
- +" This is a very long paragaphThis is a very long paragaph"
- +"This is a very long paragaph"});
- }
- }
You must log in to post a reply. Not a member yet? Register here!

