QML的复杂数组

时间:2014-08-06 20:16:48

标签: qml

我用非常简单的代码遇到了麻烦:

property var pagesAllModels: {
    ru: [
        { title: qsTr("New"),           url: "http://bash.im"           },
        { title: qsTr("Random"),        url: "http://bash.im/random"    },
        { title: qsTr("Best"),          url: "http://bash.im/best"      },
        { title: qsTr("By rating"),     url: "http://bash.im/byrating"  },
        { title: qsTr("Abyss"),         url: "http://bash.im/abyss"     },
        { title: qsTr("Abyss top"),     url: "http://bash.im/abysstop"  },
        { title: qsTr("Abyss best"),    url: "http://bash.im/abyssbest" },
    ],
    eng: [
        { title: "Latest",              url: "http://bash.org/?latest" },
        { title: "Browse",              url: "http://bash.org/?browse" },
        { title: "Random",              url: "http://bash.org/?random" },
        { title: "Top",                 url: "http://bash.org/?top"    }
    ]
}

QML中的代码在行“eng:[”给出错误,错误“预期lexem”,但在纯javascript中一切正常。怎么了?

3 个答案:

答案 0 :(得分:0)

试试这个

property var pagesAllModels: {
    ru: [
        { title: qsTr("New"),           url: "http://bash.im"           },
        { title: qsTr("Random"),        url: "http://bash.im/random"    },
        { title: qsTr("Best"),          url: "http://bash.im/best"      },
        { title: qsTr("By rating"),     url: "http://bash.im/byrating"  },
        { title: qsTr("Abyss"),         url: "http://bash.im/abyss"     },
        { title: qsTr("Abyss top"),     url: "http://bash.im/abysstop"  },
        { title: qsTr("Abyss best"),    url: "http://bash.im/abyssbest" }
    ],
    eng: [
        { title: "Latest",              url: "http://bash.org/?latest" },
        { title: "Browse",              url: "http://bash.org/?browse" },
        { title: "Random",              url: "http://bash.org/?random" },
        { title: "Top",                 url: "http://bash.org/?top"    }
    ]
}

希望这能解决您的问题

答案 1 :(得分:0)

关联数组以分号" ; "

分隔

试试这个

property var pagesAllModels: {
    ru: [
        { title: qsTr("New"),           url: "http://bash.im"           },
        { title: qsTr("Random"),        url: "http://bash.im/random"    },
        { title: qsTr("Best"),          url: "http://bash.im/best"      },
        { title: qsTr("By rating"),     url: "http://bash.im/byrating"  },
        { title: qsTr("Abyss"),         url: "http://bash.im/abyss"     },
        { title: qsTr("Abyss top"),     url: "http://bash.im/abysstop"  },
        { title: qsTr("Abyss best"),    url: "http://bash.im/abyssbest" },
    ];
    eng: [
        { title: "Latest",              url: "http://bash.org/?latest" },
        { title: "Browse",              url: "http://bash.org/?browse" },
        { title: "Random",              url: "http://bash.org/?random" },
        { title: "Top",                 url: "http://bash.org/?top"    }
    ]
} 

答案 2 :(得分:0)

那是因为你的代码是非法的JSON数组定义。 Web浏览器正在接受它,因为它们对JS语法并不严格,但QML引擎非常严格:

在关联数组中的键:值对中,键必须是字符串,因此它必须有引号,否则会与(不存在的)变量名混淆。

property var pagesAllModels: {
    "ru": [
        { "title": qsTr("New"),           "url": "http://bash.im"           },
        { "title": qsTr("Random"),        "url": "http://bash.im/random"    },
        { "title": qsTr("Best"),          "url": "http://bash.im/best"      },
        { "title": qsTr("By rating"),     "url": "http://bash.im/byrating"  },
        { "title": qsTr("Abyss"),         "url": "http://bash.im/abyss"     },
        { "title": qsTr("Abyss top"),     "url": "http://bash.im/abysstop"  },
        { "title": qsTr("Abyss best"),    "url": "http://bash.im/abyssbest" }
    ],
    "eng": [
        { "title": "Latest",              "url": "http://bash.org/?latest" },
        { "title": "Browse",              "url": "http://bash.org/?browse" },
        { "title": "Random",              "url": "http://bash.org/?random" },
        { "title": "Top",                 "url": "http://bash.org/?top"    }
    ]
}

它有效!