<?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[ListModel in C++ and append continuously from a thread]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">From the Object ListModel Example <a href="http://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html</a><br />
I Want to continuously append rows to the list from a thread.</p>
<p dir="auto">After<br />
ctxt-&gt;setContextProperty("myModel", QVariant::fromValue(dataList));<br />
I can not append more.</p>
<p dir="auto">some help had been appreciated!</p>
<p dir="auto">/MJ</p>
]]></description><link>https://forum.qt.io/topic/84019/listmodel-in-c-and-append-continuously-from-a-thread</link><generator>RSS for Node</generator><lastBuildDate>Sun, 25 Oct 2026 19:24:56 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/84019.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Oct 2017 12:55:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Tue, 10 Oct 2017 10:16:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/grecko">@<bdi>GrecKo</bdi></a> said in <a href="/post/419863">ListModel in C++ and append continuously from a thread</a>:</p>
<blockquote>
<p dir="auto">The correct solution is to not modify a model from an other thread.<br />
You could either use <code>invokeMethod</code> or emit a signal and connect it to a function in the main thread to add data to your model.</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a> Consider also whether you need a thread at all. If you need timed actions use QTimer. It's not a good programming practice to use while loop and sleep in a Qt application.</p>
]]></description><link>https://forum.qt.io/post/419870</link><guid isPermaLink="true">https://forum.qt.io/post/419870</guid><dc:creator><![CDATA[Eeli K]]></dc:creator><pubDate>Tue, 10 Oct 2017 10:16:49 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Tue, 10 Oct 2017 10:09:25 GMT]]></title><description><![CDATA[<p dir="auto">The correct solution is to not modify a model from an other thread.<br />
You could either use <code>invokeMethod</code> or emit a signal and connect it to a function in the main thread to add data to your model.</p>
]]></description><link>https://forum.qt.io/post/419863</link><guid isPermaLink="true">https://forum.qt.io/post/419863</guid><dc:creator><![CDATA[GrecKo]]></dc:creator><pubDate>Tue, 10 Oct 2017 10:09:25 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Tue, 10 Oct 2017 09:40:48 GMT]]></title><description><![CDATA[<p dir="auto">I found another example at</p>
<p dir="auto"><a href="https://arunpkqt.wordpress.com/2016/12/08/qml-list-view-sort-and-filter/" target="_blank" rel="noopener noreferrer nofollow ugc">https://arunpkqt.wordpress.com/2016/12/08/qml-list-view-sort-and-filter/</a></p>
<p dir="auto">To my model I added from the example</p>
<p dir="auto">class FilterProxyModel : public QSortFilterProxyModel</p>
<p dir="auto">and in my main.cpp I changed to following code</p>
<pre><code>FilterProxyModel filterModel;
filterModel.setSourceModel(&amp;model);

QQmlApplicationEngine engine;
QQmlContext* ctxt = engine.rootContext();
ctxt-&gt;setContextProperty("myModel", &amp;filterModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
</code></pre>
<p dir="auto">And in my QML I changed to ApplicationWindow and put my ListView in a ColumnLayout.</p>
<p dir="auto">And now when it works as I wanted, I can continue and try to understand why...</p>
<p dir="auto">Thanks for all help and if you think this solution is good, I can mark this post as solved<br />
and continue with my project?</p>
<p dir="auto">/MJ</p>
]]></description><link>https://forum.qt.io/post/419854</link><guid isPermaLink="true">https://forum.qt.io/post/419854</guid><dc:creator><![CDATA[qtMJ]]></dc:creator><pubDate>Tue, 10 Oct 2017 09:40:48 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Tue, 10 Oct 2017 06:22:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eeli-k">@<bdi>Eeli-K</bdi></a> Ok thanks,<br />
I will try positionViewAtIndex()<br />
but just so you did understand me right...<br />
It is not that I can't see the items because they comes outside the window and don't scroll down<br />
automaticly.<br />
There are no item att all in the windows until I grab it or resize it.</p>
]]></description><link>https://forum.qt.io/post/419789</link><guid isPermaLink="true">https://forum.qt.io/post/419789</guid><dc:creator><![CDATA[qtMJ]]></dc:creator><pubDate>Tue, 10 Oct 2017 06:22:48 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 20:26:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a> Then there's nothing wrong, that's expected behavior. You have to scroll the view, it's not scrolled automatically. You can catch ListView's add() signal and use positionViewAtIndex() or positionViewAtEnd().</p>
]]></description><link>https://forum.qt.io/post/419735</link><guid isPermaLink="true">https://forum.qt.io/post/419735</guid><dc:creator><![CDATA[Eeli K]]></dc:creator><pubDate>Mon, 09 Oct 2017 20:26:52 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 19:17:06 GMT]]></title><description><![CDATA[<p dir="auto">When I scroll the list by grab it with the mouse or resize, the list will update the new items.</p>
<p dir="auto">The only code I added to the example is my thread that I start in main.cpp before<br />
QQuickView view;</p>
<p dir="auto">And run like this (Here model is moved as a global, even tryed to pass it as pointer)</p>
<p dir="auto">void myThread::run()<br />
{<br />
int i=0;<br />
while (1) {<br />
model.addAnimal(Animal("Animal" + QString::number(++i), "Medium"));<br />
msleep(1000);<br />
}<br />
}</p>
<p dir="auto">And in my QML I just added</p>
<p dir="auto">Connections{<br />
target: myModel<br />
onRowsInserted: console.log("rows were inserted")<br />
}</p>
<p dir="auto">and the text shows up in my consol every second.</p>
]]></description><link>https://forum.qt.io/post/419727</link><guid isPermaLink="true">https://forum.qt.io/post/419727</guid><dc:creator><![CDATA[qtMJ]]></dc:creator><pubDate>Mon, 09 Oct 2017 19:17:06 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 14:26:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a> Another different solution could be to send signal from C++ when new item is added. You can catch it in QML, get the item from C++ in some compatible data form and add to the list using append(). But a C++ model is better IMO.</p>
]]></description><link>https://forum.qt.io/post/419671</link><guid isPermaLink="true">https://forum.qt.io/post/419671</guid><dc:creator><![CDATA[Eeli K]]></dc:creator><pubDate>Mon, 09 Oct 2017 14:26:32 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 14:22:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a> Do you mean that when you scroll your list view you can't see your the newly added items but when you resize the window you can see them? Sounds like a problem in QML side. The AnimalModel example has this code:</p>
<pre><code>void AnimalModel::addAnimal(const Animal &amp;animal)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals &lt;&lt; animal;
    endInsertRows();
}
</code></pre>
<p dir="auto">and if you call it to add animals to list it should send signals automatically and the view should be updated. You can try to catch the signal in QML:</p>
<pre><code>Connections{
target: myModelObjectWhichWasSetAsContextProperty
onRowsInserted: console.log("rows were inserted")
}
</code></pre>
<p dir="auto">If it works there's something wrong with the view.</p>
]]></description><link>https://forum.qt.io/post/419669</link><guid isPermaLink="true">https://forum.qt.io/post/419669</guid><dc:creator><![CDATA[Eeli K]]></dc:creator><pubDate>Mon, 09 Oct 2017 14:22:02 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 14:08:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a><br />
seems like you are missing some necessary signals in your model implementation when adding new rows</p>
]]></description><link>https://forum.qt.io/post/419665</link><guid isPermaLink="true">https://forum.qt.io/post/419665</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Mon, 09 Oct 2017 14:08:10 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 13:32:08 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for you reply!</p>
<p dir="auto">I found this AbstractItemModel Example,<br />
<a href="http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html</a></p>
<p dir="auto">I passed<br />
AnimalModel model;<br />
to my thread, and I managed to add to the list, but the view only update my ListView if I resize the window.<br />
Is this right way to go, and how can I update my view?</p>
<p dir="auto">/MJ</p>
]]></description><link>https://forum.qt.io/post/419656</link><guid isPermaLink="true">https://forum.qt.io/post/419656</guid><dc:creator><![CDATA[qtMJ]]></dc:creator><pubDate>Mon, 09 Oct 2017 13:32:08 GMT</pubDate></item><item><title><![CDATA[Reply to ListModel in C++ and append continuously from a thread on Mon, 09 Oct 2017 13:04:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qtmj">@<bdi>qtMJ</bdi></a><br />
thats not possible. Using a list as a model is rather more static.<br />
You will need to implement a custom <code>QAbstractListModel</code> for example.<br />
Or assign a whole new list as a model.</p>
]]></description><link>https://forum.qt.io/post/419651</link><guid isPermaLink="true">https://forum.qt.io/post/419651</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Mon, 09 Oct 2017 13:04:26 GMT</pubDate></item></channel></rss>