February 10, 2012

owenzhao owenzhao
Lab Rat
60 posts

How to put a long string to ListElement?

 

I have to use a long string to ListElements, such as

  1. ListElement {
  2.  myColor: "red"
  3.  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"
  4. }

Firstly I think I could use something like
  1. ListElement {
  2.  myColor: "red"
  3.  myText: "This is a very long paragaph This is a very long paragaph"
  4.    +"This is a very long paragaphThis is a very long paragaph"
  5.    +" This is a very long paragaphThis is a very long paragaph"
  6.    +"This is a very long paragaph"
  7. }

But The ListElement can not recongnize the + parts. only knows the first line.

Then I try do

  1. Rectangle {
  2.  id:myRect
  3.  property string longText: "This is a very long paragaph This is a very long paragaph"
  4.         +"This is a very long paragaphThis is a very long paragaph"
  5.         +" This is a very long paragaphThis is a very long paragaph"
  6.         +"This is a very long paragaph"
  7.  ListModel {
  8.   ListElement {
  9.    myColor: "red"
  10.    myText: myRect.longText
  11.   }
  12.  }
  13. }

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

February 10, 2012

billouparis billouparis
Ant Farmer
142 posts

I am not sure I understand your issue, but did you try this:

  1. Rectangle {
  2.  id:myRect
  3.  
  4.  ListModel {
  5.   ListElement {
  6.    myColor: "red"
  7.    myText: "This is a very long paragaph This is a very long paragaph \
  8.        This is a very long paragaphThis is a very long paragaph \
  9.        This is a very long paragaphThis is a very long paragaph \
  10.        This is a very long paragaph"
  11.   }
  12.  }
  13. }

Regards,
Bill

February 10, 2012

owenzhao owenzhao
Lab Rat
60 posts

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.

  1.     Rectangle {
  2.      id:myRect
  3.      
  4.      ListModel {
  5.       ListElement {
  6.        myColor: "red"
  7.        myText: "This is a very long paragaph This is a very long paragaph \
  8. This is a very long paragaphThis is a very long paragaph \
  9. This is a very long paragaphThis is a very long paragaph \
  10. This is a very long paragaph"
  11.       }
  12.      }
  13.     }

However, this is ugly when looks at the codes. Any more ideas?

billouparis wrote:
I am not sure I understand your issue, but did you try this:

  1. Rectangle {
  2.  id:myRect
  3.  
  4.  ListModel {
  5.   ListElement {
  6.    myColor: "red"
  7.    myText: "This is a very long paragaph This is a very long paragaph \
  8.        This is a very long paragaphThis is a very long paragaph \
  9.        This is a very long paragaphThis is a very long paragaph \
  10.        This is a very long paragaph"
  11.   }
  12.  }
  13. }

Regards,
Bill

February 10, 2012

billouparis billouparis
Ant Farmer
142 posts

Why don’t you create your list model in C++?

March 12, 2012

vabo vabo
Lab Rat
3 posts

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

March 12, 2012

Damian Jansen Damian Jansen
Lab Rat
18 posts

Here’s a possible solution.

  1. Rectangle {
  2.     id: myrect
  3.     width: 360
  4.     height: 360
  5.     ListView { anchors.fill: parent; model: mymodel;
  6.         delegate: Rectangle {
  7.             height: 100
  8.             width: 360
  9.             Text {
  10.                 anchors.centerIn: parent; text: model.myText; width: parent.width
  11.                 wrapMode: Text.WordWrap
  12.             }
  13.         }
  14.     }
  15.     ListModel {
  16.         id: mymodel
  17.     }
  18.     Component.onCompleted: {
  19.  
  20.     mymodel.append({"myColor":"red", "myText":
  21.         "This is a very long paragaph This is a very long paragaph"
  22.         +"This is a very long paragaphThis is a very long paragaph"
  23.         +" This is a very long paragaphThis is a very long paragaph"
  24.         +"This is a very long paragaph"});
  25.     }
  26. }

 Signature 

QtQuick Quality Engineer / Lab Monkey
Nokia Brisbane

March 12, 2012

vabo vabo
Lab Rat
3 posts

Very good:). Now the long text is separated in a script block.

March 12, 2012

Damian Jansen Damian Jansen
Lab Rat
18 posts
vabo wrote:
Very good:). Now the long text is separated in a script block.

I’m not sure if this is appreciation or sarcasm.

 Signature 

QtQuick Quality Engineer / Lab Monkey
Nokia Brisbane

March 12, 2012

vabo vabo
Lab Rat
3 posts

Of course admiration! You have added new aspects to the topic.
Regards

March 13, 2012

owenzhao owenzhao
Lab Rat
60 posts

Thank you all for the posts.
I currently use a tricky method that I create a empty listelement part and adding the real part in javascript.

 
  ‹‹ Qml Image : bad performance opengl backend      [solved]how to use "buttonClicked" in Qt Quick Symbian Component, CommonDialog ››

You must log in to post a reply. Not a member yet? Register here!