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

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:
    @ListModel
    {
    ListElement
    {
    image:"a.png"
    text: qsTr("Text 0")
    }

           ListElement
           {
                   image:"b.png"
                   text: qsTr("Text 1")
           }
    

    }@

    how can i solve this problem using the ListModel of qml

    tnx



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



  • tnx for your replay.

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





  • You can try something like this:
    @
    ListView {
    //...
    model: ListModel {
    ListElement {image:"a.png"; name: QT_TR_NOOP("Text 0")}
    //...
    }
    delegate: Text {
    //...
    text: qsTranslate("bleble",name)
    }
    }
    @

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



  • Just for history: this should be fixed as of Qt 5.4.



  • This post is deleted!


  • I can confirm that you can use qsTr directly in ListElement in QT5.4.

    But in my case, I cannot use QT5.4. I need to build my application using 5.2. Having this problem, i used MichK's suggestion. It seems to partially work. But only partially and I just cannot find out what is wrong.

    I have this model:

    ListModel {
        id: bottomPanelModel				
        ListElement {
            name: QT_TR_NOOP("show for 15 seconds")
        }				
        ListElement {
            name: QT_TR_NOOP("show for 3 minutes")
        }
        ListElement {
            name: QT_TR_NOOP("do not hide automatically")
        }				
    }
    

    The delegate that is in this case responsible for the translation looks like this:

    Component {
        id: translationDelegate
        Item{
            width: parent.width; height: 60
            Column{
                anchors.centerIn: parent
                Text { text: qsTr(name); font.family: openSansFont.name ; font.pointSize: 30}				
            }
        }
    }
    

    But this results in only first two items being translated and no the last one. Well, it does not make any sense right? It gets even better. If I build the code on Raspberry Pi (my dev machine is Win7 64 with VS QT add-in), the first item is translated, the second is not (but on my dev machine it is), and the last is translated (on my dev machine it is not). And even better, If I shuffle the ListElements and add some others I can make some different combinations of translated and not translated texts...

    Also when I do lupdate, I get this line (probably some warning) in the console:

    qsTr(): text to translate must be a literal string.
    

    ...which points to the line in the delegate, where qsTr() is called (see above).

    Any ideas? I am getting kind of desperate...


Log in to reply