Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Data not visible when loaded through QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

Data not visible when loaded through QSortFilterProxyModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qsortfilterprox
2 Posts 2 Posters 332 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chilarai
    wrote on 7 Aug 2020, 19:30 last edited by chilarai 8 Jul 2020, 19:32
    #1

    I am trying to display values in my Listview which are added in 2 ways

    1. Directly values are inserted from the model constructor
    2. 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);
    }
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Aug 2020, 20:38 last edited by
      #2

      Hi,

      Your proxy has a member variable that you set as source model. This looks wrong as already suggested in your other thread.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      2/2

      7 Aug 2020, 20:38

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved