Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML ListElement with translation
Forum Updated to NodeBB v4.3 + New Features

QML ListElement with translation

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 10.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goli
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

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

      dmcr

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goli
        wrote on last edited by
        #3

        tnx for your replay.

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          task_struct
          wrote on last edited by
          #4

          Take a look "here":http://developer.qt.nokia.com/wiki/Qt_Quick_Carousel#70b4903abcb62ace84264ad0443ae759

          "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."

          • Linu...
          1 Reply Last reply
          0
          • M Offline
            M Offline
            MichK
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dvolosnykh
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dvolosnykh
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Aros
                  wrote on last edited by Aros
                  #8

                  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...

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved