How to put Delegates at work in *.ui.qml files?
-
So far, I managed to work on data selected from a ListView like that:
//file: myList.qml ListView { ... delegate: Item { ... MouseArea { id: mouseArea onClicked : doSomething(model.index, model.someData...) } }Now I want to split mylist.qml file in a myList.qml + myListForm.ui.qml pair.
The problem I am facing is that the UI designer does not support function calls (doSomething), nor a can put the delegate in a property of MyListForm.ui.qml with a signal to be used by MyList.qml (the delegate does not even appear in the UI navigator panel).Am I trying to do it the wrong way? What is the proper way to handle delegate actions in UI files?
-
This is the (admittedly messy) solution I've got so far.
As I was unable to send a signal from UI file (altough QtQuick Designer accepts its declaration), I resorted to a variable registering the index of last clicked item:// file: SettingsPageForm.ui.qml Item { property int clickIndex: -1 // .... ListView { // ... model: dbSettingsModel delegate: Item { // UI graphics stuff with model.name, model,value etc... // ... // the delegate sets the index, the handler will reset it to -1 MouseArea { onClicked: clickIndex = model.index } } } }The implementation logic watches for the change signal:
// file: settingsPage.qml SettingsPageForm { ... function launchEditor( index ) { // getRole( row, roleName) was added to C++ model because // model properties are available inside the delegate only editor.title = dbSettingsModel.getRole( index, "name" ) editor.value = dbSettingsModel.getRole( index, "value" ) editor.unit = dbSettingsModel.getRole( index, "unit" ) editor.minValue = dbSettingsModel.getRole( index, "minValue" ) editor.maxValue = dbSettingsModel.getRole( index, "maxValue" ) editor.open() } onClickIndexChanged: { if ( clickIndex >= 0 ) launchEditor( clickIndex ) clickIndex = -1 } }I found no other way but to expand the C++ model (dbSettingsModel) with getRole( index, roleName), as model properties are not available outside delegates.
Overall it works, but I feel I'm overcomplicating it, I fell there must be a much cleaner way taking advantage of delegates straight inside UI deisgner.