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

Jérôme Godbout jerome at bodycad.com
Wed Mar 23 15:11:33 CET 2016


Hi,
You have a binding problem here inside the combobox:
*currentIndex: getCurrentIndex(mymodel.items, mymodel.item)*
this binding will get override when the user change the current value of
the combobox, the combo box will assign an int to this value. You need to
set the model onCurrentIndexChanged: setModelIndex(currentIndex)
And do the same with the model changed value.

You can wrap this up inside a synchronizer that take both object and
property and set value on respectively changed (if value is not the same to
avoid binding loop). You can also see
http://imaginativethinking.ca/bi-directional-data-binding-qt-quick/

On another note, take care if the combobox  model property change on a
combo box, the current index is reset too (cause of the property list way
of working, only have clear and push under, so assigning clear it then copy
is a lot of push, but when clear append, the current index is no more). You
can assign it by onChange of another property, backup currentIndex, set the
list to the model and try to apply backup current index if still valid
value.

Jerome


On Wed, Mar 23, 2016 at 2:44 AM, Fabian Sturm <f at rtfs.org> wrote:

> Hello list,
>
> I hope this is the right forum to write, even though my sample code is
> in Python but I think the problem lies deeper in the event propagation
> or similar.
>
> What I want to do looks simple at first. I want to show a ComboBox with
> a list of items in it and one of them is selected. On a push of a
> button the list ost extended by one element and that should be
> reflected by the ComboBox. The selected item should of course always
> stay the same. Unfortunately this is what does not work and the
> selected item is reset to 0 even.
>
> Any help is greatly appreciated!!!
>
> Thanks Fabian
>
> The sample source is here:
> https://github.com/sturmf/python_samples/tree/master/pyqt_combobox
>
> main.qml excerpt:
>
> And the important parts are this:
>
>     function getCurrentIndex(list, element) {
>         console.log('getCurrentIndex')
>         if (list && element) {
>             for (var i = 0; i < list.length; i++) {
>                 if (list[i].name === element.name) {
>                     console.log('Found item at pos: ' + i)
>                     return i
>                 }
>             }
>         }
>         return -1
>     }
>
>
> [...]
>
>     ComboBox {
>         id: combo
>         width: parent.width
>         currentIndex: getCurrentIndex(mymodel.items, mymodel.item)
>         model: mymodel.items
>         textRole: 'name'
>     }
>
>
>
> The python model is this:
>
> class MyModel(QObject):
>
>     itemChanged = pyqtSignal()
>     itemsChanged = pyqtSignal()
>
>     def __init__(self, parent=None):
>         QObject.__init__(self, parent)
>         self._items = [MyItem('one'), MyItem('two'), MyItem('three')]
>         self._item = self._items[2]
>
>     @pyqtProperty(MyItem, notify=itemChanged)
>     def item(self):
>         return self._item
>
>     @pyqtProperty(QQmlListProperty, notify=itemsChanged)
>     def items(self):
>         print('Query for items')
>         return QQmlListProperty(MyItem, self, self._items)
>
>     @pyqtSlot()
>     def new_item(self):
>         print('Append new item')
>         self._items.append(MyItem('new'))
>         self.itemsChanged.emit()
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160323/dca37108/attachment.html>


More information about the Interest mailing list