Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML ListView currentIndex Not Working With c++
Forum Updated to NodeBB v4.3 + New Features

QML ListView currentIndex Not Working With c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khislop
    wrote on last edited by
    #1

    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)
    
    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      When model changes it will reload whole ListView and drop currentIndex to a default value (0). You can save current index somehere else before updating model and then restore it:

      onModelChanged: currentIndex = someSavedValue
      
      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved