<?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[Application crashes while adding rows in qml TableView]]></title><description><![CDATA[<p dir="auto">I have custom data model which inherits QAbstractTablemodel assigned to qml Tableview.<br />
Everything was working fine.</p>
<p dir="auto">But now I wanted searching and sorting feature in my tableview so I used SortFilterProxyModel for this. As I insert rows in my source model, application crashes. I am emitting proper signals viz, beginInsertRows() and endInsertRows(). Crash was not producible in debug mode.<br />
I have collected some stack trace of crash:</p>
<p dir="auto">0	QVariant::toString() const			0x68a461e6	<br />
1	QSortFilterProxyModel::lessThan(QModelIndex const&amp;, QModelIndex const&amp;) const			0x689f3a21	<br />
2	QSortFilterProxyModel::filterRole() const			0x689f4926	<br />
3	QSortFilterProxyModel::filterRole() const			0x689f4e88	<br />
4	QSortFilterProxyModel::setSortRole(int)			0x689f8ac6	<br />
5	QSortFilterProxyModel::setSortRole(int)			0x689f8ce5	<br />
6	QSortFilterProxyModel::qt_static_metacall(QObject *, QMetaObject::Call, int, void * *)			0x689fb70b	<br />
7	QMetaObject::activate(QObject *, int, int, void * *)			0x68a351b5	<br />
8	QAbstractItemModel::rowsInserted(QModelIndex const&amp;, int, int, QAbstractItemModel::QPrivateSignal)			0x68a9c9d7	<br />
9	QAbstractItemModel::endInsertRows()			0x689e2a39	<br />
10	??			0x4b0c69	<br />
11	??			0x4b230d	<br />
12	??			0x48958f	<br />
13	??			0x5649d2	<br />
14	QMetaObject::activate(QObject *, int, int, void * *)			0x68a351b5	<br />
15	??			0x56f9bd	<br />
16	QMetaObject::activate(QObject *, int, int, void * *)			0x68a351b5	<br />
17	??			0x570a4a	<br />
18	??			0x5179c9	<br />
19	??			0x5509cd	<br />
...	&lt;More&gt;</p>
<p dir="auto">Can someone help?</p>
]]></description><link>https://forum.qt.io/topic/85077/application-crashes-while-adding-rows-in-qml-tableview</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 19:54:22 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/85077.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Nov 2017 10:08:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 13:00:47 GMT]]></title><description><![CDATA[<p dir="auto">The issue is fixed.<br />
I just took one local variable for sortColumn. Initializing with -1 doesn't crash the application.<br />
Thanks.</p>
]]></description><link>https://forum.qt.io/post/426441</link><guid isPermaLink="true">https://forum.qt.io/post/426441</guid><dc:creator><![CDATA[Ashu_9791]]></dc:creator><pubDate>Thu, 16 Nov 2017 13:00:47 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 12:38:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> said in <a href="/post/426403">Application crashes while adding rows in qml TableView</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ashu_9791">@<bdi>Ashu_9791</bdi></a> said in <a href="/post/426400">Application crashes while adding rows in qml TableView</a>:</p>
<blockquote>
<p dir="auto">How can I set sortColumn through my SortFilterProxyModel class?</p>
</blockquote>
<p dir="auto">Just as you do the sort order.</p>
<p dir="auto">call <code>QSortFilterProxyModel::sort(columnYouWantToSort,sortOrder())</code></p>
</blockquote>
<p dir="auto">After setting sortColumn it crashed again.<br />
I think there is some problem with indexing after sorting which is making it crash. May be I am wrong.</p>
]]></description><link>https://forum.qt.io/post/426438</link><guid isPermaLink="true">https://forum.qt.io/post/426438</guid><dc:creator><![CDATA[Ashu_9791]]></dc:creator><pubDate>Thu, 16 Nov 2017 12:38:22 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 10:53:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ashu_9791">@<bdi>Ashu_9791</bdi></a> said in <a href="/post/426400">Application crashes while adding rows in qml TableView</a>:</p>
<blockquote>
<p dir="auto">How can I set sortColumn through my SortFilterProxyModel class?</p>
</blockquote>
<p dir="auto">Just as you do the sort order.</p>
<p dir="auto">call <code>QSortFilterProxyModel::sort(columnYouWantToSort,sortOrder())</code></p>
]]></description><link>https://forum.qt.io/post/426403</link><guid isPermaLink="true">https://forum.qt.io/post/426403</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 16 Nov 2017 10:53:54 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 10:46:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
Thanks. That fix doesn't crash application.<br />
But it doesn't sort the table on column header click because I have not set sortColumn.<br />
How can I set sortColumn through my SortFilterProxyModel class?</p>
]]></description><link>https://forum.qt.io/post/426400</link><guid isPermaLink="true">https://forum.qt.io/post/426400</guid><dc:creator><![CDATA[Ashu_9791]]></dc:creator><pubDate>Thu, 16 Nov 2017 10:46:02 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 10:33:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ashu_9791">@<bdi>Ashu_9791</bdi></a> said in <a href="/post/426388">Application crashes while adding rows in qml TableView</a>:</p>
<blockquote>
<p dir="auto">void SortFilterProxyModel::setSortOrder(Qt::SortOrder order)<br />
{<br />
QSortFilterProxyModel::sort(order);<br />
}</p>
</blockquote>
<p dir="auto">that should be <code>QSortFilterProxyModel::sort(sortColumn(),order);</code></p>
]]></description><link>https://forum.qt.io/post/426393</link><guid isPermaLink="true">https://forum.qt.io/post/426393</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 16 Nov 2017 10:33:46 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 10:18:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
<em><strong>try removing the proxy and see if it works</strong></em><br />
Yes. It works properly as earlier if I remove proxyModel.</p>
<p dir="auto"><em><strong>try implementing the QSortFilterProxyModel on the c++ side and see if you get a debug mode crash there</strong></em><br />
I have custom implemented this. Here is the code for it:</p>
<pre><code>#include "sortfilterproxymodel.h"
#include &lt;QtDebug&gt;
#include &lt;QtQml&gt;

SortFilterProxyModel::SortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{
    connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(countChanged()));
    connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(countChanged()));
}

int SortFilterProxyModel::count() const
{
    return rowCount();
}

QObject *SortFilterProxyModel::source() const
{
    return sourceModel();
}

void SortFilterProxyModel::setSource(QObject *source)
{
    setSourceModel(qobject_cast&lt;QAbstractItemModel *&gt;(source));
}

QByteArray SortFilterProxyModel::sortRole() const
{
    return roleNames().value(QSortFilterProxyModel::sortRole());
}

void SortFilterProxyModel::setSortRole(const QByteArray &amp;role)
{
    QSortFilterProxyModel::setSortRole(roleKey(role));
}

void SortFilterProxyModel::setSortOrder(Qt::SortOrder order)
{
    QSortFilterProxyModel::sort(order);
}

QByteArray SortFilterProxyModel::filterRole() const
{
    return roleNames().value(QSortFilterProxyModel::filterRole());
}

void SortFilterProxyModel::setFilterRole(const QByteArray &amp;role)
{
    QSortFilterProxyModel::setFilterRole(roleKey(role));
}

QString SortFilterProxyModel::filterString() const
{
    return filterRegExp().pattern();
}

void SortFilterProxyModel::setFilterString(const QString &amp;filter)
{
    setFilterRegExp(QRegExp(filter, filterCaseSensitivity(), static_cast&lt;QRegExp::PatternSyntax&gt;(filterSyntax())));
}

SortFilterProxyModel::FilterSyntax SortFilterProxyModel::filterSyntax() const
{
    return static_cast&lt;FilterSyntax&gt;(filterRegExp().patternSyntax());
}

void SortFilterProxyModel::setFilterSyntax(SortFilterProxyModel::FilterSyntax syntax)
{
    setFilterRegExp(QRegExp(filterString(), filterCaseSensitivity(), static_cast&lt;QRegExp::PatternSyntax&gt;(syntax)));
}

QJSValue SortFilterProxyModel::get(int idx) const
{
    QJSEngine *engine = qmlEngine(this);
    QJSValue value = engine-&gt;newObject();
    if (idx &gt;= 0 &amp;&amp; idx &lt; count()) {
        QHash&lt;int, QByteArray&gt; roles = roleNames();
        QHashIterator&lt;int, QByteArray&gt; it(roles);
        while (it.hasNext()) {
            it.next();
            value.setProperty(QString::fromUtf8(it.value()), data(index(idx, 0), it.key()).toString());
        }
    }
    return value;
}

int SortFilterProxyModel::roleKey(const QByteArray &amp;role) const
{
    QHash&lt;int, QByteArray&gt; roles = roleNames();
    QHashIterator&lt;int, QByteArray&gt; it(roles);
    while (it.hasNext()) {
        it.next();
        if (it.value() == role)
            return it.key();
    }
    return 0;
}

QHash&lt;int, QByteArray&gt; SortFilterProxyModel::roleNames() const
{
    if (QAbstractItemModel *source = sourceModel())
        return source-&gt;roleNames();
    return QHash&lt;int, QByteArray&gt;();
}

bool SortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &amp;sourceParent) const
{
    qDebug()&lt;&lt;"[SortProxyModel] Source Row "&lt;&lt;sourceRow;
    QRegExp rx = filterRegExp();
    if (rx.isEmpty())
        return true;
    QAbstractItemModel *model = sourceModel();
    if (filterRole().isEmpty()) {
        QHash&lt;int, QByteArray&gt; roles = roleNames();
        QHashIterator&lt;int, QByteArray&gt; it(roles);
        while (it.hasNext()) {
            it.next();
            QModelIndex sourceIndex = model-&gt;index(sourceRow, 0, sourceParent);
            QString key = model-&gt;data(sourceIndex, it.key()).toString();
            if (key.contains(rx))
                return true;
        }
        return false;
    }

    QModelIndex sourceIndex = model-&gt;index(sourceRow, 0, sourceParent);
    if (!sourceIndex.isValid())
        return true;
    QString key = model-&gt;data(sourceIndex, roleKey(filterRole())).toString();
    return key.contains(rx);
}

int SortFilterProxyModel::getSourceIndex(int idx)
{
    if (idx &gt;= 0 &amp;&amp; idx &lt; count())
    {
        QModelIndex index2 = mapToSource(index(idx, 0));
        return index2.row();
    }
    return -1;
}
</code></pre>
<p dir="auto"><em><strong>try replacing your custom model with a QStandardItemModel (just a test one, you don't need to do much)</strong></em><br />
What I need to change in my code for this?</p>
]]></description><link>https://forum.qt.io/post/426388</link><guid isPermaLink="true">https://forum.qt.io/post/426388</guid><dc:creator><![CDATA[Ashu_9791]]></dc:creator><pubDate>Thu, 16 Nov 2017 10:18:54 GMT</pubDate></item><item><title><![CDATA[Reply to Application crashes while adding rows in qml TableView on Thu, 16 Nov 2017 10:13:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ashu_9791">@<bdi>Ashu_9791</bdi></a> said in <a href="/post/426383">Application crashes while adding rows in qml TableView</a>:</p>
<blockquote>
<p dir="auto">Crash was not producible in debug mode</p>
</blockquote>
<p dir="auto">Ouch...</p>
<p dir="auto">Let's try to narrow it down manually then.</p>
<ul>
<li>try replacing your custom model with a QStandardItemModel (just a test one, you don't need to do much)</li>
<li>try removing the proxy and see if it works</li>
<li>try implementing the <code>QSortFilterProxyModel</code> on the c++ side and see if you get a debug mode crash there</li>
</ul>
]]></description><link>https://forum.qt.io/post/426384</link><guid isPermaLink="true">https://forum.qt.io/post/426384</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 16 Nov 2017 10:13:25 GMT</pubDate></item></channel></rss>