1

誰かが次のセットアップで何が間違っているのか説明してもらえますか?

アイデアは、ゲーム エンジンを engine.js で開発し、UI ロジックを qml ファイルで開発することです。

page.qml

Page {
    id: page
    SilicaGridView {
        id: listView
        model: ListModel {
            id: roleList
            Component.onCompleted: {
                Engine.addRoles(roleList);
            }
        }

        delegate: Rectangle {
            id: delegate
            border.color: model.selected ? Theme.highlightColor : Theme.primaryColor

            Label {
                text: qsTr(model.name)
            }
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    Engine.selectRole(model);
                }
            }
        }
    }
}

エンジン.js:

function Role() {
    this.name = "role"
    this.selected = false;
}

function addRoles(list) {
    list.append(new Role());
    list.append(new Role());
}

function selectRole(role) {
    console.log(role.selected) // <-- false
    role.selected = true;
    console.log(role.selected) // <-- false
}

そのため、ページ上の要素をクリックすると、エンジンから selectRole が呼び出されます。奇妙な理由により、モデルで選択したプロパティを更新しても、実際にはモデルが更新されません。なぜこのようになっているのですか?

4

1 に答える 1

0

Qt Quick のビューには、選択した項目を追跡する方法が既にあります。

つまり、オブジェクトにselected変数を含める必要はありません。RoleEngine.js は次のようになります。

function Role() {
    this.name = "role"
}

function addRoles(list) {
    list.append(new Role());
    list.append(new Role());
}

あなたのQMLコードは実行されません(実際の例を提供すると本当に役立ちます)ので、私は自由に実行させました:

import QtQuick 2.3
import QtQuick.Controls 1.1

import "Engine.js" as Engine

ApplicationWindow {
    width: 500
    height: 500

    ListView {
        id: listView
        width: 100
        height: 100
        anchors.centerIn: parent

        model: ListModel {
            id: roleList
            Component.onCompleted: {
                Engine.addRoles(roleList);
            }
        }

        delegate: Rectangle {
            id: delegateItem
            border.color: ListView.isCurrentItem ? "red" : "black"
            width: listView.width
            height: 30

            Label {
                id: label
                text: qsTr(model.name)
                anchors.centerIn: parent
            }
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    listView.currentIndex = index;
                    print(index, delegateItem.ListView.isCurrentItem)
                }
            }
        }
    }
}

ListView の添付プロパティisCurrentItemの使用に注意してください。これは、次と同等の便利なプロパティです。

delegate: Rectangle {
    border.color: listView.currentIndex == index ? "red" : "black"
}
于 2014-08-23T08:36:20.827 に答える