Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Callback system between two threads
Qt 6.11 is out! See what's new in the release blog

Callback system between two threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 515 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nekreg45
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which pattern are you following ?
      Model View ? Model Controller View ?
      Or do you have a class directly modeled on your database objects ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nekreg45
        wrote on last edited by
        #3

        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()".

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved