Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved]Property binding to pointer value



  • Hello,

    i have problems with a property binding. I expose a pointer to a QSortFilterProxyModel through a c++ class to qml, and bind it to a Listview's model property:
    C++:
    @
    class Test {
    Q_OBJECT
    Q_PROPERTY(QSortFilterProxyModel* activeMenu READ activeMenu NOTIFY activeMenuChanged)

    ...

    QSortFilterProxyModel* activeMenu() {
    m_filterModel->setSourceModel(getActiveMenu());
    return m_filterModel;
    }

    ...

    private:
    QSortFilterProxyModel* m_filterModel;
    }
    @
    QML:
    @
    ListView {
    ...
    model: testInstance.activeMenu
    @

    The problem is that the Listview only gets updated the first time. When the sourceModel of the QSortFilterProxyModel changes the Listview's model property stays the same, because it's a pointer to the a QSortFilterProxyModel.

    To get the property binding working on the Listview i changed the c++ class:
    @
    QSortFilterProxyModel* activeMenu() {
    m_filterModel1->setSourceModel(getActiveMenu());
    m_filterModel2->setSourceModel(getActiveMenu());

     m_filterToggle = !m_filterToggle;
     if(m_filterToggle)
       return m_filterModel1;
     else
       return m_filterModel2;
    

    }
    @

    This works as expected, but the solution isn't realy nice.
    Is there a better way to get the property binding working with pointer values?

    Thanks



  • Hello,

    i have tested the example again. I observed that the view correctly updates on the change of the sourceComponent from the QSortFilterProxyModel. The problem is that the ListModels onModelChanged signal is not emitted.

    The problem in my application was that i changed the index of the ListView in its onModelChanged handler. In the case of the QSortFilterProxyModel as model, the handler is not called when the sourceModel of the QSortFilterProxyModel changes, and the index was not set correctly.

    Thanks


Log in to reply