<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Create multiple instances of QSortFilterProxyModel for view]]></title><description><![CDATA[<p dir="auto">I have two <code>listviews</code> on a single <code>Page</code> component. The model for both is coming from a single <code>QSortFilterProxyModel</code>.  The problem is if I set data for one <code>ListView</code>, the other one is also changed. This happens as there is a single instance of the model.</p>
<p dir="auto"><strong>Will I have to create 2 different instances of the <code>QSortFilterProxyModel</code> or there is some other way around?</strong></p>
<p dir="auto">My Code</p>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>
int main(int argc, char *argv[])
{

    // Application basic initialization
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    QtWebEngine::initialize();
    QQuickStyle::setStyle("Default");

    FilterModel filterModel;
    FilterList filterList;

    // Set contexts for QML
    engine.rootContext()-&gt;setContextProperty("filterModel",&amp;filterModel);
    engine.rootContext()-&gt;setContextProperty("filterList",&amp;filterList);


    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

</code></pre>
<p dir="auto"><strong>filterlist.cpp</strong></p>
<pre><code>#include "filterlist.h"

FilterList::FilterList(QObject *parent) : QSortFilterProxyModel(parent)
{
    setSourceModel(&amp;m_filterListModel);
}

void FilterList::searchByCategory(QString filterSubCategory)
{

    setFilterRole(m_filterListModel.FilterListCategoryRole);
    this-&gt;setFilterCaseSensitivity(Qt::CaseInsensitive);
    this-&gt;setFilterFixedString(filterSubCategory);
}

</code></pre>
<p dir="auto"><strong>mypage.qml</strong></p>
<pre><code>import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3

Page {

    id : somepageid

    Column{
        Button{
            id: btn1
            text: "btn a"
            onClicked: {
                filterList.searchByCategory("category a")
            }
        }

        Button{
            id: btn2
            text: "btn b"
            onClicked: {
                filterList.searchByCategory("category b")
            }
        }
    }

    ListView{
        id: lv1
        model: filterList
        height: 100
        delegate: Row{
            Text{
                text: name
            }
        }
    }

    ListView{
        id: lv2
        anchors.top: lv1.bottom
        model: filterList
        height: 100
        delegate: Row{
            Text{
                text: name
            }
        }
    }

}

</code></pre>
]]></description><link>https://forum.qt.io/topic/117807/create-multiple-instances-of-qsortfilterproxymodel-for-view</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Mar 2026 02:47:39 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/117807.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 07 Aug 2020 12:05:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Create multiple instances of QSortFilterProxyModel for view on Fri, 07 Aug 2020 18:06:57 GMT]]></title><description><![CDATA[<p dir="auto">That's correct.</p>
<p dir="auto">What you are trying to do with only one model is like having two pilots in one car trying to drive into two different directions at the same time.</p>
]]></description><link>https://forum.qt.io/post/611135</link><guid isPermaLink="true">https://forum.qt.io/post/611135</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 07 Aug 2020 18:06:57 GMT</pubDate></item><item><title><![CDATA[Reply to Create multiple instances of QSortFilterProxyModel for view on Fri, 07 Aug 2020 13:27:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> do you mean that I will have to create 2 proxies for the same model for my respective listviews? And that we cannot anyhow manage from a single proxy?</p>
<p dir="auto">I think I get it now</p>
]]></description><link>https://forum.qt.io/post/611071</link><guid isPermaLink="true">https://forum.qt.io/post/611071</guid><dc:creator><![CDATA[chilarai]]></dc:creator><pubDate>Fri, 07 Aug 2020 13:27:20 GMT</pubDate></item><item><title><![CDATA[Reply to Create multiple instances of QSortFilterProxyModel for view on Fri, 07 Aug 2020 12:15:47 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">No there is not. However, you can have only one underlying model shared between your two proxies. Therefore, you should not have an instance of your custom model as member variable of your custom proxy.</p>
]]></description><link>https://forum.qt.io/post/611063</link><guid isPermaLink="true">https://forum.qt.io/post/611063</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 07 Aug 2020 12:15:47 GMT</pubDate></item></channel></rss>