<?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[GUI thread and non-GUI thread operate the same database]]></title><description><![CDATA[<p dir="auto">The GUI thread is a QWidget, which contains some QLineEdit for setting parameters and QLabel for displaying information. These QLineEdit and QLabel have been mapped to the database through QDataWidgetMapper.<br />
Non-GUI threads also need to read and write the contents of these QLineEdit and QLabel.<br />
For example, the QLineEdit used to set the speed can be entered by the user with a certain speed value, but the speed value may be inappropriate. The non-GUI thread will obtain the speed value and correct the speed value during execution. The non-GUI thread will frequently generate some information when it is running, and this information needs to be displayed in the GUI thread and saved to the database.</p>
<p dir="auto"><strong>Solution A</strong><br />
The solution I tried is this: create a database connection separately in the GUI thread and the non-GUI thread, and any modification will be written to the database immediately, but the model data of these two threads will not be updated synchronously. Because there is no way to know that QSqlDatabase has been refreshed.<br />
Due to the large amount of data (some display information is refreshed frequently), it is very inefficient to re-query model data (using model-&gt;select()) by sending update signals. Is there any way to make GUI thread and non-GUI thread operate the same database synchronously?</p>
<p dir="auto"><strong>Solution B</strong><br />
There is another solution: connect GUI threads and non-GUI threads through signal slots.<br />
Due to the rapid data update, the queue connection may affect the performance of the GUI thread.</p>
]]></description><link>https://forum.qt.io/topic/123622/gui-thread-and-non-gui-thread-operate-the-same-database</link><generator>RSS for Node</generator><lastBuildDate>Fri, 09 Oct 2026 10:11:53 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/123622.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Feb 2021 10:39:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Fri, 19 Feb 2021 02:34:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Hi, since these days are the Chinese New Year, I am sorry to see your reply so late, and I will seriously consider your solution.<br />
Happy New Year!</p>
]]></description><link>https://forum.qt.io/post/645339</link><guid isPermaLink="true">https://forum.qt.io/post/645339</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Fri, 19 Feb 2021 02:34:51 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Wed, 10 Feb 2021 20:41:28 GMT]]></title><description><![CDATA[<p dir="auto">Solution E: use <a href="https://sqlite.org/c3ref/update_hook.html" target="_blank" rel="noopener noreferrer nofollow ugc">update_hook</a> from SQLite, but this will require some compilation...</p>
]]></description><link>https://forum.qt.io/post/643734</link><guid isPermaLink="true">https://forum.qt.io/post/643734</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 10 Feb 2021 20:41:28 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Wed, 10 Feb 2021 01:07:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
<strong>Solution C</strong><br />
The UI mapped to the custom data model, and the database model runs in a backend thread. In this way, communication between other control threads and database thread will use cross-thread database connections, which should not be allowed.<br />
<strong>Solution D</strong><br />
The UI mapped to the database model, and the custom data model runs in a backend thread. After the custom data model is locked, it can be safely read and written by other control threads.</p>
]]></description><link>https://forum.qt.io/post/643498</link><guid isPermaLink="true">https://forum.qt.io/post/643498</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Wed, 10 Feb 2021 01:07:12 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Wed, 10 Feb 2021 00:06:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Hi, thank you very much for your reply, it's SQLITE.<br />
Multiple threads operate on the same database. When one thread writes to the database, other threads cannot know that the database has been updated without a signal trigger. In other words, QSqlDatabase will not notify QSqlTableModel to query the database again. Is my understanding correct, please?</p>
]]></description><link>https://forum.qt.io/post/643496</link><guid isPermaLink="true">https://forum.qt.io/post/643496</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Wed, 10 Feb 2021 00:06:29 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 23:54:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Hi, your reply helped me a lot. I will try to customize a UI data model to connect to a separate database thread in the form of Signals/Slots.<br />
Best regards!</p>
]]></description><link>https://forum.qt.io/post/643494</link><guid isPermaLink="true">https://forum.qt.io/post/643494</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Tue, 09 Feb 2021 23:54:16 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 18:15:19 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">What database are you using ?</p>
]]></description><link>https://forum.qt.io/post/643421</link><guid isPermaLink="true">https://forum.qt.io/post/643421</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 09 Feb 2021 18:15:19 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 12:49:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tovax">@<bdi>tovax</bdi></a><br />
Well I wouldn't do <strong>A</strong>, I would do <strong>B</strong>, in some shape or form.</p>
<blockquote>
<p dir="auto">I have to consider whether it is appropriate to map the GUI data directly to the database.</p>
</blockquote>
<p dir="auto">I don't quite know what this means.  Like we said, all db ops would be done by db backend thread.  UI would send signal to it.  So if UI value for something is not good/right, backend (or UI in signal) can still map that to appropriate for the db?</p>
]]></description><link>https://forum.qt.io/post/643368</link><guid isPermaLink="true">https://forum.qt.io/post/643368</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 09 Feb 2021 12:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 11:10:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/643330">GUI thread and non-GUI thread operate the same database</a>:</p>
<blockquote>
<p dir="auto">Solution B, with a separate database thread and signals being sent from UI to database thread when necessary, keeping UI thread responsive.  I don't know how that interacts with QDataWidgetMapper.</p>
</blockquote>
<p dir="auto">Thank you for your answers, I have to consider whether it is appropriate to map the GUI data directly to the database.</p>
]]></description><link>https://forum.qt.io/post/643334</link><guid isPermaLink="true">https://forum.qt.io/post/643334</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Tue, 09 Feb 2021 11:10:14 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 11:03:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/643330">GUI thread and non-GUI thread operate the same database</a>:</p>
<blockquote>
<p dir="auto">My 2 cents/understanding.  I wouldn't want to create multiple database connections, or try to sync.  Database operations should only be done in the thread where Q...Database is created/owned/moved to.</p>
</blockquote>
<p dir="auto">In other words, I should not continue to consider Solution A?</p>
]]></description><link>https://forum.qt.io/post/643332</link><guid isPermaLink="true">https://forum.qt.io/post/643332</guid><dc:creator><![CDATA[tovax]]></dc:creator><pubDate>Tue, 09 Feb 2021 11:03:18 GMT</pubDate></item><item><title><![CDATA[Reply to GUI thread and non-GUI thread operate the same database on Tue, 09 Feb 2021 10:48:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tovax">@<bdi>tovax</bdi></a><br />
My 2 cents/understanding.  I wouldn't want to create multiple database connections, or try to sync.  Database operations should only be done in the thread where <code>Q...Database</code> is created/owned/moved to.  <strong>Solution B</strong>, with a separate database thread and signals being sent from UI to database thread when necessary, keeping UI thread responsive.  I don't know how that interacts with <code>QDataWidgetMapper</code>.</p>
]]></description><link>https://forum.qt.io/post/643330</link><guid isPermaLink="true">https://forum.qt.io/post/643330</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 09 Feb 2021 10:48:19 GMT</pubDate></item></channel></rss>