QML ListView highlight does not seems to get updated.
-
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?
-
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?
@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.
-
@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.
@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_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.@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.