メインファイルからIDでQML要素にアクセスしたいが、機能していない。私のテスト(ケース)環境では、私は2つのファイルがあります:Qt:QMLの任意のIDで要素にアクセス
- main.qmlを、これは私がID
- Accounts.qmlでアクセスしたい元のファイルがあり、これが私の要素が定義されているファイルであります
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
Item {
Accounts {
id: accounts
}
Component.onCompleted: {
console.log(accounts);
console.log(accounts.accounts_pane);
console.log(accounts.list_area);
console.log(accounts.accounts_pane.list_area.list_column.list_identities.model);
}
}
Accounts.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
Pane {
id: accounts_pane
anchors {
fill: parent
}
Rectangle {
id: list_area
border.color: "black"
border.width: 2
anchors {
left: parent.left
right: parent.right
top: parent.top
bottom: parent.bottom
}
Column {
id: list_column
ListView {
id: list_identities
width: list_area.width
height: 200
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
}
delegate: Text {
text: name + ',' + number
}
}
}
}
}
したがって、main.qmlファイルから、私はアカウントのモデルを変更したい(別の会社は異なるアカウントを使用する)ので、Javascriptでそれを行う必要があります。 (私のモデルはC++クラスであり、QMLエンジンに登録されていますが、別のモデルオブジェクトを切り替えるためにJavascriptを使用します)。モデルにアクセスするための表記、私はそれを理解し、このようなものでなければなりません:
console.log(accounts);
console.log(accounts.accounts_pane);
console.log(accounts.list_area);
console.log(accounts.accounts_pane.list_area.list_column.list_identities.model);
私は渡すことはできません:にconsole.log()で、それをテストする場合、しかし
accounts.accounts_pane.list_area.list_column.list_identities.model
Accounts.qmlで定義されているPane {}要素を過ぎてください。 qmlscene
の出力は私にこれらのエラーを示しています
hier $ qmlscene main.qml
qml: QQuickPane(0x13b85f0)
qml: undefined
qml: undefined
file:///QT_snippets/qmlscenes/hier/main.qml:13: TypeError: Cannot read property 'list_area' of undefined
どのようにmain.qml文書から任意のIDでQMLの要素にアクセスすることができますか?
ありがとうございます!インターフェイスについては、どのようにインターフェイスを定義しますか? JavaScript関数ですか? – Nulik
@Nulik - 必要なものは、プロパティ、関数、またはエイリアスの集合です。たとえば、モデルを設定できるようにする必要がある場合のみ、モデルをエイリアスとして公開します。 – dtech