October 5, 2011

goli goli
Ant Farmer
111 posts

QML ListElement with translation

 

hi,

i need to create a ListModel that include translation text.
i want to use qsTr() in the element, but i get the error:
“ListElement: cannot use script for property value”

this is the code:

  1. ListModel
  2.    {
  3.            ListElement
  4.            {
  5.                    image:"a.png"
  6.                    text: qsTr("Text 0")
  7.            }
  8.  
  9.            ListElement
  10.            {
  11.                    image:"b.png"
  12.                    text: qsTr("Text 1")
  13.            }
  14.  
  15. }

how can i solve this problem using the ListModel of qml

tnx

4 replies

October 5, 2011

dmcr dmcr
Ant Farmer
161 posts

you can :
i- create a C++ QObject myO, use the setContextProperty fonction to be able to reach it from QML
ii- in myO, create a Q_INVOKABLE function (for example my_qsTr)

and then use myO.my_qsTr(“Text1”)

October 5, 2011

goli goli
Ant Farmer
111 posts

tnx for your replay.

i don’t want to use a c++(Qt Object)…
is there any other solution for the problem?

October 5, 2011

task_struct task_struct
Hobby Entomologist
344 posts

Take a look here [developer.qt.nokia.com]

 Signature 

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

October 5, 2011

michal.k michal.k
Lab Rat
70 posts

You can try something like this:

  1. ListView {
  2. //...
  3. model: ListModel {
  4.            ListElement {image:"a.png"; name: QT_TR_NOOP("Text 0")}
  5.    //...
  6.    }
  7. delegate: Text {
  8.    //...
  9.    text: qsTranslate("bleble",name)
  10.    }
  11. }

“bleble” is a context argument it is needed because qsTranslate needs two arguments.

 
  ‹‹ [Solved] specify different context to tr?      [SOLVED] Changing QTabWidget’s tab Name at runtime.. ››

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