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 Issue

Qml ListView currentIndex Issue

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.5k 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.
  • D Offline
    D Offline
    danielgeorgy
    wrote on last edited by
    #1

    HI, I have created a combox using qml listview
    There are three ways with which i can update the combox ui

    1. click and select using a mouse
    2. directly modify the currentIndex of the ListView
    3. have a qproperty in my class, which modifies the currentIndex and the ui updates.

    @ Q_PROPERTY(int cIndex READ cIndex WRITE setCIndex NOTIFY cIndexChanged STORED true);

    void writeCIndex(int index)
    {
    m_cIndex = index;
    emit cIndexChanged(m_cIndex); ===> emit the notify signal
    }@

    and in my qml
    @ property alias selectedIndex: listView.currentIndex;
    …..
    selectedIndex: myclassData.cIndex@

    updating the ui with this qproperty works as expected but
    the issue is when i update the ui using click and select or by directly assigning currentIndex with a value in qml.
    afterwards with the qproperty it is not possible to update the ui.
    but still the click and select or by directly assigning currentIndex with a value in qml works fine
    because o f this i am not able to update the combobox from a c++ file.

    Please take a look at the issue and suggest a solution

    Thanks in Advance

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      Hello and welcome to devnet,

      please tag your code with the @(At) -sign. Also is there any console-output? And have you tried to display the index trough console with print(myclassData.cIndex)?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        danielgeorgy
        wrote on last edited by
        #3

        In my qml i have this
        @ selectedIndex: myclassData.cIndex@

        After this issue occurs when i print myclassData.cIndex it prints the updated value but selectedIndex does not print the updated value so @ selectedIndex: myclassData.cIndex@
        is not working

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          You could go ahead and change the listView directly by just setting this property in your ListView:
          @currentIndex: myclassData.cIndex@

          1 Reply Last reply
          0
          • D Offline
            D Offline
            danielgeorgy
            wrote on last edited by
            #5

            I have a solution, I found that the notify signal for the qproperty is not updating the ui(qml), so i have written this handler at the qml side
            @ Component.onCompleted: {
            _globalSource.temperatureUnitChanged.connect(temperatureUnitChangedHandler);
            }

            function temperatureUnitChangedHandler(){
            temperatureCombobox.comboboxCurrentItem = _globalSource.temperatureUnit;
            }@

            If notify signal cannot update the ui then i am forcfully updating the ui with another handler connected to the notify signal.

            Also whenever combo box item is selected through ui, current item selected index should be conveyed to the c++ side, otherwise c++ side will have the old value.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Brit7
              wrote on last edited by
              #6

              I was having this same problem, and I had trouble following the solution posted. I eventually figured out a different solution, so I thought I'd post to help out anyone else seeing this problem. Here's what I did:

              @property int iCurrentSelection: 0
              ...
              function setCurrentSelection( iComboIndex )
              {
              iCurrentSelection = iComboIndex;
              }
              ...
              ComboBox {
              id: imageCombo
              width: 150
              currentIndex: iCurrentSelection
              model: ListModel {
              id: cbSettingsItems3
              ListElement { text: "Lung"; }
              ListElement { text: "Bone"; }
              }
              // onActivated is called when the user
              // sets the combo via the dropdown
              onActivated:
              {
              onImageChanged( index );
              iCurrentSelection = currentIndex;
              }
              // onCurrentIndexChanged is called whenever the
              // combo is changed (dropdown or code),
              // but code changes stop calling it
              // when the ui is changed.
              onCurrentIndexChanged:
              {
              iCurrentSelection = currentIndex;
              }@

              The important part of the code is the "iCurrentSelection = currentIndex;" lines inside the onActivated and onCurrentIndexChanged calls. This re-connects the "iCurrentSelection" property to the currentIndex. This means that if you change the iCurrentSelection value, the comboBox will change as well, even after you've interacted with the UI to change the ComboBox.

              1 Reply Last reply
              0

              • Login

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