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 highlight does not seems to get updated.
Forum Updated to NodeBB v4.3 + New Features

QML ListView highlight does not seems to get updated.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 400 Views 1 Watching
  • 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.
  • I Offline
    I Offline
    ikuris
    wrote on last edited by
    #1

    Hello,
    I am trying to learn binding C++ models and QML Views toghether.
    I am having problem with the highlight on the element in the listview. It does get updated with mouse or arrows, but when fotter button is clicked it does not updated to the last element(it is basically appending to the model).

    Here is ListView qml and custom model implementations.

           ListView {
               id: clistView
               width: parent.width / 2
               height: parent.height
               model: cModel
               delegate: Item {
                   width: parent.width
                   height: 40
                   Text {
                       anchors.centerIn: parent
                       text: model.display
                   }
                   MouseArea {
                       anchors.fill: parent
                       onClicked: clistView.currentIndex = index
                   }
               }
               highlight: Rectangle {
                   color: "lightblue"
               }
               onCurrentIndexChanged: {
                   cModel.currentIndexUpdated(currentIndex)
               }
               footer: Item {
                       width: parent.width
                       height: 40
    
                       Row {
                           anchors.centerIn: parent
                           spacing: 10
    
                           Button {
                               text: "+"
                               onClicked: {
                                   cModel.addCItem("New Item")
                               }
                           }
                       }
               }
           }
    
    void CModel::addCItem(const QString& scnName)
    {
        qDebug() << "addCItem function called";
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        CItem scn;
        scn.scnName = scnName;
        scnList.push_back(scn);
        endInsertRows();
    }
    void CModel::currentIndexUpdated(unsigned int currIndex)
    {
    
        qDebug() << "Current Index Updated, index: " << currIndex;
    }
    

    I am quietly missing somehting or doesn`t seem to understand the logic, could you help me?

    jeremy_kJ 1 Reply Last reply
    0
    • I ikuris

      Hello,
      I am trying to learn binding C++ models and QML Views toghether.
      I am having problem with the highlight on the element in the listview. It does get updated with mouse or arrows, but when fotter button is clicked it does not updated to the last element(it is basically appending to the model).

      Here is ListView qml and custom model implementations.

             ListView {
                 id: clistView
                 width: parent.width / 2
                 height: parent.height
                 model: cModel
                 delegate: Item {
                     width: parent.width
                     height: 40
                     Text {
                         anchors.centerIn: parent
                         text: model.display
                     }
                     MouseArea {
                         anchors.fill: parent
                         onClicked: clistView.currentIndex = index
                     }
                 }
                 highlight: Rectangle {
                     color: "lightblue"
                 }
                 onCurrentIndexChanged: {
                     cModel.currentIndexUpdated(currentIndex)
                 }
                 footer: Item {
                         width: parent.width
                         height: 40
      
                         Row {
                             anchors.centerIn: parent
                             spacing: 10
      
                             Button {
                                 text: "+"
                                 onClicked: {
                                     cModel.addCItem("New Item")
                                 }
                             }
                         }
                 }
             }
      
      void CModel::addCItem(const QString& scnName)
      {
          qDebug() << "addCItem function called";
          beginInsertRows(QModelIndex(), rowCount(), rowCount());
          CItem scn;
          scn.scnName = scnName;
          scnList.push_back(scn);
          endInsertRows();
      }
      void CModel::currentIndexUpdated(unsigned int currIndex)
      {
      
          qDebug() << "Current Index Updated, index: " << currIndex;
      }
      

      I am quietly missing somehting or doesn`t seem to understand the logic, could you help me?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      @ikuris said in QML ListView highlight does not seems to get updated.:

      I am having problem with the highlight on the element in the listview. It does get updated with mouse or arrows, but when fotter button is clicked it does not updated to the last element(it is basically appending to the model).

      This may be down to my misunderstanding of what it is, but modifying a model doesn't necessarily impact a view's display. If you want the view's currentIndex to change when an item is added to the model, that needs to be explicitly requested. eg:

      ListView {
        onCountChanged: currentIndex = count - 1
      }
      

      @ikuris said in QML ListView highlight does not seems to get updated.:

      > Here is ListView qml and custom model implementations.
      >  ```
      >             onCurrentIndexChanged: {
      >                 cModel.currentIndexUpdated(currentIndex)
      >             }
      > ```
      
      
      This is an unusual thing to do, and may indicate an overly tight coupling between the view and model. To make a widely familiar analogy, imagine the model as a list of contacts, and the view as a dialer app. Modifying (or calling!) each contact as the user scrolls through the list is unlikely to be desirable. If the application needs to recover the current index after a crash, store that as part of the view rather than the model.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      I 1 Reply Last reply
      0
      • jeremy_kJ jeremy_k

        @ikuris said in QML ListView highlight does not seems to get updated.:

        I am having problem with the highlight on the element in the listview. It does get updated with mouse or arrows, but when fotter button is clicked it does not updated to the last element(it is basically appending to the model).

        This may be down to my misunderstanding of what it is, but modifying a model doesn't necessarily impact a view's display. If you want the view's currentIndex to change when an item is added to the model, that needs to be explicitly requested. eg:

        ListView {
          onCountChanged: currentIndex = count - 1
        }
        

        @ikuris said in QML ListView highlight does not seems to get updated.:

        > Here is ListView qml and custom model implementations.
        >  ```
        >             onCurrentIndexChanged: {
        >                 cModel.currentIndexUpdated(currentIndex)
        >             }
        > ```
        
        
        This is an unusual thing to do, and may indicate an overly tight coupling between the view and model. To make a widely familiar analogy, imagine the model as a list of contacts, and the view as a dialer app. Modifying (or calling!) each contact as the user scrolls through the list is unlikely to be desirable. If the application needs to recover the current index after a crash, store that as part of the view rather than the model.
        I Offline
        I Offline
        ikuris
        wrote on last edited by
        #3

        @jeremy_k
        Thank you it works fine. I did not quite the understand the last warnining that you gave there, the reason I did such a thing was to display the information of the current index on another ListView that has another model. I did not give the all of the implementation because it was irrelivant. The use of TreeView could have been nicer.

        jeremy_kJ 1 Reply Last reply
        0
        • I ikuris has marked this topic as solved on
        • I ikuris

          @jeremy_k
          Thank you it works fine. I did not quite the understand the last warnining that you gave there, the reason I did such a thing was to display the information of the current index on another ListView that has another model. I did not give the all of the implementation because it was irrelivant. The use of TreeView could have been nicer.

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @ikuris said in QML ListView highlight does not seems to get updated.:

          @jeremy_k
          I did not quite the understand the last warnining that you gave there, the reason I did such a thing was to display the information of the current index on another ListView that has another model.

          It's a question of style. I would have passed the currentIndex directly to the second view, rather than relaying it through cModel. Either way can work.

          I did not give the all of the implementation because it was irrelivant.

          No problem. It doesn't appear to be central to the question, and leaving it out makes the rest easier/faster to read.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          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