簡體   English   中英

如何在單例中動態更新子ListModel?

[英]How to dynamically update a child ListModel within a singleton?

我嘗試了解共享的qml對象如何被其他qml和javascript文件全局使用。 我有一個QML應用程序,在一個窗口中有兩個組合框。 一個是ApplicationWindow父(國家)組合框,另一個是Page1.qml子(城市)組合框。 選擇國家/地區時,子組合框應同時具有該國家/地區的城市。 當使用組合框並且函數在ApplicationWindow ,這是可能的,我在這里問了一個問題。 我以前的問題的答案的幫助下,我想的內外胎組合框連接ApplicationWindow父組合框在ApplicationWindow使用qmldir,辛格爾頓和JavaScript。 我嘗試將子組合框的列表模型作為單例對象,但它不起作用,並且給出錯誤qrc:/script.js:11: Error: Cannot assign QJSValue to QQmlListModel*

main.qml:

import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import "script.js" as JS
ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    property int currentindex: comboBox2.currentIndex
    Settings{
        property alias country : comboBox2.currentIndex
    }
    Dialog {
        id: dialog
        title: "Select Country"
        implicitWidth: parent.width
        implicitHeight: parent.height/2
        Column{
        ComboBox {
            id: comboBox2
            x: 199
            y: 176
            width: 277
            height: 48
            currentIndex: 0
            model:
             ListModel {
             ListElement { text: qsTr("USA") }
             ListElement { text: qsTr("Russia") }
             ListElement { text: qsTr("Iran") }
            }
            onCurrentIndexChanged:{
                currentindex = currentIndex
                JS.coord_combo_changed()
            }
        }
        }
    }
    ColumnLayout{
        anchors.horizontalCenter: parent.horizontalCenter
        spacing:20
        width: parent.width
    Button{
        anchors.horizontalCenter: parent.horizontalCenter
        id:select_country
        text:"select country"
        onClicked: dialog.open()
    }
    Page1{
        anchors.horizontalCenter: parent.horizontalCenter
    }
    }
}

Page1.qml:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
Page{
    RowLayout{
        anchors.horizontalCenter: parent.horizontalCenter
        spacing:5
        width: parent.width
   Text{
            text:"Select City: "
        }
   ChildCombo{
   id:comboBox1
   model: Shared.childmodel
   }
    }
}

qmldir:

singleton Shared 1.0 Shared.qml

Shared.qml:

pragma Singleton
import QtQuick 2.9
import QtQuick.Controls 2.2
QtObject {
    property ListModel childmodel: ChildModel{}
}

ChildModel.qml:

import QtQuick 2.0
ListModel {}

script.js:

function coord_combo_changed(){
    if(currentindex === 0){
        Shared.childmodel = ["New York", "Washington", "Houston"]
    return Shared.childmodel
}
else if (currentindex === 1){
    Shared.childmodel = ["Moscow","Saint Petersburg","Novosibirsk"]
    return Shared.childmodel
}
else if (currentindex === 2){
    Shared.childmodel = ["Tehran","Tabriz","Shiraz"]
    return Shared.childmodel
}
}

我還嘗試將combobox設為共享對象,它也無法正常工作,並且也沒有出現任何錯誤。 是否可以更新子組合框?

謝謝。

在QML中,模型可以是列表,ListModel,QAbstractItemModel等,但是這些對象不是等效的。 那就是您的錯誤,您試圖指出ListModel是一個列表,在這種情況下,解決方案是先清理模型,然后使用append方法添加元素:

function coord_combo_changed(){
    var values = [];
    if(currentindex === 0){
        values = ["New York", "Washington", "Houston"]
    }
    else if (currentindex === 1){
        values = ["Moscow","Saint Petersburg","Novosibirsk"]
    }
    else if (currentindex === 2){
        values = ["Tehran","Tabriz","Shiraz"]
    }
    Shared.childmodel.clear()
    for(var i in values){
        Shared.childmodel.append({"text": values[i]});
    }
}

另一方面,我看到您的代碼引發警告,指示如果項目在布局內,則不應使用錨點,在這種情況下,應更改為:

ColumnLayout{
    anchors.horizontalCenter: parent.horizontalCenter
    spacing:20
    width: parent.width
    Button{
        Layout.alignment: Qt.AlignHCenter // <---
        id:select_country
        text:"select country"
        onClicked: dialog.open()
    }
    Page1{
        Layout.alignment: Qt.AlignHCenter // <---
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM