Important: Please read the Qt Code of Conduct -

ItemsSource = ObservableCollection<T> databinding in QML

  • Hi,

    I have 2 viewmodels, FooViewModel and BarViewModel
    FooViewModel has a property QList<BarViewModel*> FooViewModel::items

    My problem is, I want to bind items to a Repeater::model, but all I get is:

    @QMetaProperty::read: Unable to handle unregistered datatype 'QList<BarViewModel*>' for property 'FooViewModel::items'@

    Does QList<T> fire up notifications when the collection changes, or do I need to do it myself?
    Raising a Foo:itemsChangedEvent manually on QList<T>::append or QList<T>::removeOne seems a bit ineffective as it would cause Repeater to recreate the entire scene graph for the collection when only 1 item is added or removed from the "observed" QList<T>.

    any thoughts how to implement this?

  • Have u ever figured this out? If so, how did u manage to expose an "ObservableCollection" from your ViewModel?

  • @milasch there's a sample project that demonstrates the binding plus a bit Caliburn Microey View resolution using a custom 'ContentControl' class for 'Repeater::ItemTemplate' (actually it is called 'Repeater::delegate'):

    I wouldn't use it in a project though, mainly because when 'itemsChanged' is signalled, the 'ItemsControl' (in this case it is 'Repeater') destroys all Views ('ContentControl') bound through data binding and recreates them. Personally I'd prefer a proper 'INotifyCollectionChange' mechanism that allows signaling changes per individual item inside the collection, but I wasn't compelled enough to implement one.

Log in to reply