如何在qml ListModel 里国际化文字

Donven 2012-01-13

翻译qml里的文字是,一般使用的 qsTr(),

但是如果在ListModel 里用,

比如:

ListModel {
    ListElement { title: qsTr( "Calendar" ) ; iconSource: "icons/calendar.png" }
 

则会报错

ListElement: cannot use script for property value

解决方案:

import QtQuick 1.0
     
    ListModel {
        ListElement { iconSource: "icons/calendar.png" }
        ListElement { iconSource: "icons/develop.png" }
        ListElement { iconSource: "icons/globe.png" }
        ListElement { iconSource: "icons/mail.png" }
        ListElement { iconSource: "icons/music.png" }
        ListElement { iconSource: "icons/phone.png" }
        function title( index) {
            if ( title[ "text" ] === undefined) {
                title.text = [
                    qsTr( "Calendar" ) ,
                    qsTr( "Setup" ) ,
                    qsTr( "Internet" ) ,
                    qsTr( "Messages" ) ,
                    qsTr( "Music" ) ,
                    qsTr( "Call" )
                ]
            }
            return title.text [ index]
        }
    }
 

然后在listview里设置

text: view.model.title(view.currentIndex)

相关推荐

SuiKaSan的自学室 / 0评论 2017-12-08