Unsolved QML ListView currentIndex Not Working With c++
-
I'm trying to create a voltage selector in QML using a ListView as a base. The list itself just needs to be a list of text elements but the number of items in the list needs to be able to be changed at run time. If I set the model to a number (model: 4) or a mathematical expression (model: (1200-800)/100) then everything works as expected. When I try to set the model based on a c++ integer variable exposed to the QML (model: myCppClass.intProperty) it seemes to work since I get the correct number of elements. However setting the currentIndex does not seem to do anything, it will always start at index 0. If I increment the currentIndex during run time (by a button press for example) it changes correctly. However I need have a default setting larger than index 0 which doesn't seem to work once I add a c++ integer to the model number calculation.
My voltage selector:
// Using ListView Rectangle { width: 540; height: 94 x: 711 y: 117 color: "transparent" clip: true ListView { id: voltageList width: 540; height: 94 // property var itemCount: (mainScreen.maxVoltage - mainScreen.minVoltage) / 100 + 1 model: (mainScreen.maxVoltage - mainScreen.minVoltage) / 100 + 1 // model: (1200 - 800) / 100 // model: itemCount // model: 5 preferredHighlightBegin: 270 - 50 preferredHighlightEnd: 270 + 50 highlightRangeMode: PathView.StrictlyEnforceRange currentIndex: 2 // currentIndex: (manager.mode == 0) ? (manager.getVoltageDefaultWave()-mainScreen.minVoltage)/100 : (manager.getVoltageDefaultFlex()-mainScreen.minVoltage)/100 orientation: Qt.Horizontal delegate: Item { width: 100 height: 100 property bool currentItem: ListView.view.currentIndex == index property alias text : textNumList.text Text { id: textNumList anchors.centerIn: parent // text: ((index + 12) / 10).toFixed(1) text: ((index*manager.getVoltageResolution() + mainScreen.minVoltage) / 1000).toFixed(1) // text: mainScreen.maxVoltage opacity: currentItem? 1 : 0.5 font.pixelSize: currentItem ? 65 : 34 font.family: nizzoli.name color: "#7ED1F2" } } // onCurrentIndexChanged: mainScreen.setVoltage(currentIndex*manager.getVoltageResolution() + mainScreen.minVoltage) onMovementEnded: mainScreen.setVoltage(currentIndex*manager.getVoltageResolution() + mainScreen.minVoltage) } }
Here's what I expose to QML in my C++ mainScreen header:
Q_PROPERTY(int voltage READ voltage NOTIFY voltageChanged) Q_PROPERTY(int minVoltage READ minVoltage NOTIFY minVoltageChanged) Q_PROPERTY(int maxVoltage READ maxVoltage NOTIFY maxVoltageChanged)
-
When model changes it will reload whole
ListView
and dropcurrentIndex
to a default value (0). You can save current index somehere else before updating model and then restore it:onModelChanged: currentIndex = someSavedValue