[Interest] Setting currentIndex in Qt Quick ComboBox does not work

Fabian Sturm f at rtfs.org
Thu Mar 24 07:37:54 CET 2016


Hi,

here a short update, I think I finally cracked my problem! Somehow
setting currentIndex initially seems to break the model loading. But if
I leave that out and connect to onModelChanged it works. Final problem
was that the model loading was overriding my selected item. I solved
that by no longer updating the model in onCurrentIndexChanged but in
onActivated.

Here the qml binding code:

ComboBox {
    id: combo

    width: parent.width

    model: mymodel.items
    textRole: 'name'

    onActivated: {
        console.log('Activated')
        mymodel.item = mymodel.items[index]
    }

    onModelChanged: {
        console.log('modelChanged')
        currentIndex = getCurrentIndex(mymodel.items, mymodel.item)
    }

}

Thanks a lot,
Fabian


Am Mittwoch, den 23.03.2016, 22:24 +0100 schrieb Fabian Sturm:
> Hi Jerome!
> 
> Thanks a lot for your help and I already read the article and tried
> for
> another several hours. Unfortunately I still can only get one half
> working. Here is the new code for the event propagation:
> 
> ComboBox {
>     id: combo
>     width: parent.width
>     model: mymodel.items // order is important
>     currentIndex: getCurrentIndex(mymodel.items, mymodel.item)
>     onCurrentIndexChanged: mymodel.item = model[currentIndex]
>     textRole: 'name'
>     Component.onCompleted: {
>         combo.modelChanged.connect(modelUpdated)
>     }
>     function modelUpdated() {
>         console.log('model updated')
>         // tried to set the index here but the vaue is already
> overriden
>     }
> }
> 
> With this the model is loaded first and then the index is set to the
> correct initial value. But with this solution I can only append to
> the
> model once and after the append, the index will be wrong.
> 
> I also don't seem to be able to save the currentIndex at the right
> time. Therefore a small working example would really be great!
> 
> My updated code is again at: 
> https://github.com/sturmf/python_samples/tree/master/pyqt_combobox
> 
> Thanks a lot,
> Fabian
> 
> Am Mittwoch, den 23.03.2016, 10:11 -0400 schrieb Jérôme Godbout:
> > Hi,
> > You have a binding problem here inside the combobox: 
> > currentIndex: getCurrentIndex(mymodel.items, mymodel.item)
> 
> > > 
> > > The sample source is here:
> > > https://github.com/sturmf/python_samples/tree/master/pyqt_combobo
> > > x
> > > 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list