PDA

View Full Version : Incrementing and decrementing ListView



Vysero
17th February 2020, 21:34
I have an element that is a delegate for a ListView (https://doc.qt.io/qt-5/qml-qtquick-listview.html) and it is loading this or that component depending on certain properties of the model like so:


Component.onCompleted:
{
...
else if(typeof(modelData.value) === "string" && modelData.optionsList.length === 0)
{
_firstLoader.sourceComponent = _textEntryComponent
_valueContainerItem.objectName = "textbox"
}
else if(modelData.is_boolean)
{
_firstLoader.sourceComponent = _booleanComponent
_valueContainerItem.objectName = "boolean switch"
}
...
}

The model is essentially just various properties defined in the back end. Here is the ListView (https://doc.qt.io/qt-5/qml-qtquick-listview.html) (wizard):


ListView
{
id: setupList

onCurrentIndexChanged: vehicleSetupTitle.text = model[setupList.currentIndex].label
model: currentSelectedVehicle.propertiesForVehicle
delegate: Item
{
id: buffer

HardwareSettingsDisplay
{
title: modelData.label
onProgressWizard:
{
setupList.incrementCurrentIndex()
currentSelectedImplement.setLastConfigurationResum eStep(setupList.currentIndex)
}
}
}
}

Where ProgressWizard is a signal created in HardwareSettingsDisplay that fires whenever the user makes an acceptable selection for one of the different components:


signal progressWizard

Alright, so I am having trouble with increment/decrement. So when I want to increment the wizard the user makes a selection and that selection fires the progressWizard() function which moves the wizard forward, in theory. However, I am getting odd behavior so I threw in some print statements:


onModelChanged:
{
console.info("Model just changed.")
console.info("Current index: ", setupList.currentIndex)
console.info("Last configuration step: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)
}

onCurrentIndexChanged:
{
console.info("Index just changed.")
console.info("Current index: ", setupList.currentIndex)
console.info("Last configuration step: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)

visible ? vehicleSetupTitle.text = model[setupList.currentIndex].label : ""
}

onProgressWizard:
{
console.info("Progress wizard called. Current index value: ", setupList.currentIndex)
console.info("Last configuration step: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)
setupList.incrementCurrentIndex()
console.info("Progress wizard called. Index was just incremented: ", setupList.currentIndex)
console.info("Last configuration step: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)
presentationManager.settingsPresenter.currentSelec tedImplement.setLastConfigurationResumeStep(setupL ist.currentIndex)
console.info("Last configuration step just set too current index: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)
}

Here is the output:

//First thing I do is create a new item. It seems to print everything twice :confused:
qml: Index just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Index just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 0

//Now I enter in a name. The wizard progresses and all seems well but the last configuration step isn't being set properly for some reason :confused:
qml: Progress wizard called. Current index value: 0
qml: Last configuration step: 0
qml: Index just changed.
qml: Current index: 1
qml: Last configuration step: 0
qml: Progress wizard called. Index was just incremented: 1
qml: Last configuration step: 0
qml: Last configuration step just set too current index: 0

//Now I hit the boolean switch and the wizard goes backwards :confused:
qml: Current index: 0
qml: Last configuration step: 0
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 0

How is this possible it's not calling: setupList.decrementCurrentIndex() it's not even calling progressWizard() the only thing I can think is that changing the model is somehow reloading the entire Item (https://doc.qt.io/qt-5/qml-qtquick-item.html) or ListView (https://doc.qt.io/qt-5/qml-qtquick-listview.html). If that's the case then maybe I can do something like this:


onModelChanged:
{
setupList.currentIndex = presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep
setupList.positionViewAtIndex(setupList.currentInd ex, ListView.Beginning)
}

but until I can figure out why my setter isn't working... but that might not even be the case or the best solution here. So I wanted to post my problem here in the hopes that you guys could take a look and help me out if you can think of something I am not doing or am doing wrong please let me know.


EDIT:

I figured out why the last configuration step wasn't being set so I tried the onModelChanged idea:


onModelChanged:
{
console.info("Model just changed.")
console.info("Current index: ", setupList.currentIndex)
console.info("Last configuration step: ", presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep)

setupList.currentIndex = presentationManager.settingsPresenter.currentSelec tedVehicle.lastConfigurationStep
setupList.positionViewAtIndex(setupList.currentInd ex, ListView.Beginning)
}

it worked... for the second component. However, the third component is now sending me back to the second component :confused:

Output

//First creation nothing changed
qml: Index just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Index just changed.
qml: Current index: 0
qml: Last configuration step: 0
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 0

//Text element component entry now reports everything correctly and all seems well. wizard progresses:
qml: Progress wizard called. Current index value: 0
qml: Last configuration step: 0
qml: Index just changed.
qml: Current index: 1
qml: Last configuration step: 0
qml: Progress wizard called. Index was just incremented: 1
qml: Last configuration step: 0
qml: Last configuration step just set too current index: 1

//Second boolean component is no longer sending me backwards but the index is definitely being reset for some reason:
qml: Index just changed.
qml: Current index: 0 //index reset
qml: Last configuration step: 1
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 1
qml: Index just changed.
qml: Current index: 1 //index set to last configuration onModelChanged
qml: Last configuration step: 1

//So I hit next and come to another component which is a List Selection component but it's sending me backwards
qml: Index just changed.
qml: Current index: 0 //Got reset to zero
qml: Last configuration step: 1
qml: Model just changed.
qml: Current index: 0
qml: Last configuration step: 1
qml: Index just changed.
qml: Current index: 1 //frustrating...
qml: Last configuration step: 1