Callback system between two threads
-
I'm looking for a way to create a callback system between two threads. I have a main thread that handles the GUI and most instructions. The second thread is responsible for handling the uploading and downloading to a SQL database.
I have found it cumbersome to request data from the database. For every request, I use a connect statement on the "requesting class" to a "intermediary class". I have to add additional signals and slots for the "intermediary class" to the "database class" to request the specific data. I then have to add another function on the "database class" to download from the database and add a signal to relay the data from the "database class" back to the "intermediary class" which in turn passes it back to the "requesting class".
I need the "intermediary class" because it setups up signals and slots with the "database class" before it moves the database class is moved to another thread.
In my C# experience, I would have handled this situation by passing a callback, so that the "intermediary class" could just invoke the callback to let the "requesting class" know that the data has been retrieved. However, I am not finding a Qt/C++ to do this. I have looked into QAction, passing a slot as a parameter, and function pointers. I was not unable to get any of these to work for me.
Does anyone have any suggestions? I'm trying to minimize the amount of signals and slots required to pass just one request. Thanks!
-
Hi,
Which pattern are you following ?
Model View ? Model Controller View ?
Or do you have a class directly modeled on your database objects ? -
On the main program, I am implementing a Model Controller View. For the database classes, I am not using any sort of view.
I have one class on the main thread called "Database Controller". It creates a class called "Database Manager" (I know, names are bad, I plan to rename soon). I connect various signals and slots between "Database Controller" and "Database Manager". Then, "Database Manager" is moved into its own thread where it communicates with queries to a SQLDatabase through "QSqlDatabase::database()".
-
From your description, it seems you are looking to implement something a bit like QNetworkAccessManager. Send a query, get a reply object and connect the reply to do whatever you want with the returned data.