[Interest] QSqlDatabase and Multithreading

Soroush Rabiei soroush.rabiei at gmail.com
Wed Jan 22 16:18:53 CET 2014


Hi

I'm writing a data-intensive application which must wait for large chunks
of data from a remote database, sometimes about 10 seconds. So I want to
keep UI in main thread (which one is running event loop) and move all data
operations to other threads. I can see how to connect signals/slots from UI
to waiter/worker threads, though I can't find an elegant way to properly
change my data classes.

I'm using a singleton-pattern class, Database, which holds QSqlDatabase.
Everybody else should use this class to pass queries, see if database is
still connected, and other tasks. The problem is that:

A connection can only be used from within the thread that created it.
Moving connections between threads or creating queries from a different
thread is not supported.
My question is how can I make connections between UI and second thread? All
I can think of is to derive Database class from QThread. But then, what
would run() be? And then other data processor classes (which all do some
queries from db) should be derived from QObject and moved to the only
instance of Database class? That violates the rule: Don't mix moveToThread
and derivation ways.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140122/6a25abe7/attachment.html>


More information about the Interest mailing list