Data not visible when loaded through QSortFilterProxyModel
Unsolved
QML and Qt Quick
-
I am trying to display values in my
Listview
which are added in 2 ways- Directly values are inserted from the model constructor
- On click of a button from the view
The data is fetched to the view using a
QSortFilterProxyModel
.I am unable to figure out why only data loaded from the model constructor are displayed and not from the 2nd one? However, if I connect the model directly, everything works. Please point me what am I doing wrong
Model file
#include "qttest2.h" QtTest2::QtTest2(QObject *parent) :QObject(parent), counter(0) { // on the constructor it works addFilterList(new FilterList(0,"categorical 1", this)); addFilterList(new FilterList(1,"categorical 2", this)); } // With this the filter doenst work void QtTest2::newFilter(QString section ) { addFilterList(new FilterList(counter, section, this)); counter++; } int QtTest2::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return mFilter.size(); } QVariant QtTest2::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mFilter.count()) return QVariant(); FilterList * filterList = mFilter[index.row()]; if( role == FilterListIdRole) return filterList->filterId(); if( role == FilterListSectionRole) return filterList->section(); return QVariant(); } bool QtTest2::setData(const QModelIndex &index, const QVariant &value, int role) { FilterList * filterList = mFilter[index.row()]; bool somethingChanged = false; switch (role) { case FilterListIdRole: { if( filterList->filterId()!= value.toInt()){ filterList->setFilterId(value.toInt()); somethingChanged = true; } break; } case FilterListSectionRole: { if( filterList->section()!= value.toString()){ filterList->setSection(value.toString()); somethingChanged = true; } break; } } if( somethingChanged){ emit dataChanged(index,index,QVector<int>() << role); return true; } return false; } Qt::ItemFlags QtTest2::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; return Qt::ItemIsEditable; } QHash<int, QByteArray> QtTest2::roleNames() const { QHash<int, QByteArray> roles; roles[FilterListIdRole] = "filterId"; roles[FilterListSectionRole] = "section"; return roles; }
Display.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.3 Page { id : somepageid Button{ id: button1 height: 30 width : parent.width Text { id: btn_xt text: qsTr("Click to add filter") } onClicked: { // This doesnt come in QSortProxyModel QtTest2.newFilter("New section"); } } ListView{ id: lv2 anchors.top: lv1.bottom model: MyProxyFilter height: parent.height - button1.height delegate: Row{ Text{ text: filterId + "-" + section } } } }
MyProxyFilter.cpp
#include "myproxyfilter.h" MyProxyFilter::MyProxyFilter(QObject *parent) : QSortFilterProxyModel(parent) { setSourceModel(&m_qtTest2); setFilterRole(m_filterListModel.FilterListSectionRole); } // Haven't implemented this yet. void MyProxyFilter::setSearchString(QString string) { this->setFilterCaseSensitivity(Qt::CaseInsensitive); this->setFilterFixedString(string); }
-
Hi,
Your proxy has a member variable that you set as source model. This looks wrong as already suggested in your other thread.