Qml ListView currentIndex Issue
-
HI, I have created a combox using qml listview
There are three ways with which i can update the combox ui- click and select using a mouse
- directly modify the currentIndex of the ListView
- 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
-
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 -
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.
-
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.