我正在用HsQML構建應用程序。這是我第一次遇到QML,我在Qt中的第二次工作,以及Haskell的第一個大型項目,所以請原諒我的無知。將特定項目的ListModel綁定到組件
在UI中,我有一個TabView。第一個Tab包含一個綁定到模型並顯示項目列表的ListView。雙擊ListView中的項目將打開一個新選項卡,其中包含正確顯示該項目詳細信息的組件(我的猜測是憑藉新選項卡繼承其點擊列表項目的上下文)的新選項卡。
現在,我的目標是打開一個選項卡,爲該模型創建一個新項目。這個想法是創建一個空白數據項(可選地將其添加到模型中),並將其「加載」到用於編輯現有項目的相同組件類型中。我搜索了QML的文檔,甚至找不到任何與遠程相關的東西,這讓我覺得這種方法是完全有缺陷的。
TabView {
id : rootTabs
Tab {
ListView {
model : AutoListModel {
source : workflowModel // this is sort of HsQML specific, data comes as a list from Haskell
}
delegate : Rectangle {
Text {
text : modelData.name
}
MouseArea {
anchors.fill : parent
// this part works because the new component inherits its modelData from the current context
// so the new tab has correct data
onDoubleClicked : {
rootTabs.addTab(modelData.name, Qt.createComponent("WorkflowView.qml"))
rootTabs.currentIndex = rootTabsCount - 1
}
}
}
}
Button {
text : "Create workflow"
// this is the part in question - how do I assign the newly appended data to comp?
onClicked : {
wModel.appendBlank()
comp = Qt.createComponent("WorkflowView.qml")
var tab = rootTabs.addTab("New workflow", comp)
comp.statusChanged.connect(tabLoaded)
}
}
}
}
WorkflowEdit.qml:
Rectangle {
TextField {
id : nameInput
text : modelData.name
Binding {
target : modelData
property : "name"
value : nameInput.text
}
}
}
你說的新標籤意味着有正確的數據?看起來你只是創建一個WorkflowView組件,就是這樣。你能發佈該qml文件的代碼嗎? –
正確,我只是創建一個WorkflowView。但是,通過雙擊ListView項目創建它時,其modelData似乎包含該項目的相應數據......這是完全的魔術。 現在,我希望能夠以相同的方式進行編程,以便WorkflowView的'modelData'將引用我想要的模型中的項目。 添加了一個WorkflowView示例,無論如何這真的很簡單。 – RecencyEffect