QML处的复杂阵列

QML处的复杂阵列,qml,Qml,我在使用非常简单的代码时遇到了麻烦: property var pagesAllModels: { ru: [ { title: qsTr("New"), url: "http://bash.im" }, { title: qsTr("Random"), url: "http://bash.im/random" }, { title: qsTr("Best"),

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

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:[”行出现错误,错误为“expected lexem”,但在纯javascript中,一切都正常。怎么了?

试试这个

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"    }
    ]
}

希望这能解决您的问题

以分号“分隔的关联数组

试试这个

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"    }
    ]
} 

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

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

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"    }
    ]
}

这是有效的!

我记得数组中最后一个元素后的逗号在JS中没有被请求,也没有解决我的问题。我检查并编辑了答案,但是你在第一个数组中使用了额外的,这可能是原因。我仍然得到错误:[W]未知:19-file:///usr/share/bashReaderOnlyQML/qml/pages/Menu.qml:19:12: 应为标记“,”。注意这一点。这些不是QML对象,也不是JavaScript对象。尝试修改例如
pagesAllModels.ru[0]
将不起作用。