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. GUI thread and non-GUI thread operate the same database

GUI thread and non-GUI thread operate the same database

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.1k 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.
  • tovaxT Offline
    tovaxT Offline
    tovax
    wrote on last edited by
    #1

    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.
    Non-GUI threads also need to read and write the contents of these QLineEdit and QLabel.
    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.

    Solution A
    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.
    Due to the large amount of data (some display information is refreshed frequently), it is very inefficient to re-query model data (using model->select()) by sending update signals. Is there any way to make GUI thread and non-GUI thread operate the same database synchronously?

    Solution B
    There is another solution: connect GUI threads and non-GUI threads through signal slots.
    Due to the rapid data update, the queue connection may affect the performance of the GUI thread.

    JonBJ 1 Reply Last reply
    0
    • tovaxT tovax

      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.
      Non-GUI threads also need to read and write the contents of these QLineEdit and QLabel.
      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.

      Solution A
      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.
      Due to the large amount of data (some display information is refreshed frequently), it is very inefficient to re-query model data (using model->select()) by sending update signals. Is there any way to make GUI thread and non-GUI thread operate the same database synchronously?

      Solution B
      There is another solution: connect GUI threads and non-GUI threads through signal slots.
      Due to the rapid data update, the queue connection may affect the performance of the GUI thread.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @tovax
      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. 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.

      tovaxT 2 Replies Last reply
      0
      • JonBJ JonB

        @tovax
        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. 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.

        tovaxT Offline
        tovaxT Offline
        tovax
        wrote on last edited by
        #3

        @JonB said in GUI thread and non-GUI thread operate the same database:

        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.

        In other words, I should not continue to consider Solution A?

        1 Reply Last reply
        0
        • JonBJ JonB

          @tovax
          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. 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.

          tovaxT Offline
          tovaxT Offline
          tovax
          wrote on last edited by
          #4

          @JonB said in GUI thread and non-GUI thread operate the same database:

          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.

          Thank you for your answers, I have to consider whether it is appropriate to map the GUI data directly to the database.

          JonBJ 1 Reply Last reply
          0
          • tovaxT tovax

            @JonB said in GUI thread and non-GUI thread operate the same database:

            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.

            Thank you for your answers, I have to consider whether it is appropriate to map the GUI data directly to the database.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @tovax
            Well I wouldn't do A, I would do B, in some shape or form.

            I have to consider whether it is appropriate to map the GUI data directly to the database.

            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?

            tovaxT 2 Replies Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              What database are you using ?

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

              tovaxT 1 Reply Last reply
              0
              • JonBJ JonB

                @tovax
                Well I wouldn't do A, I would do B, in some shape or form.

                I have to consider whether it is appropriate to map the GUI data directly to the database.

                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?

                tovaxT Offline
                tovaxT Offline
                tovax
                wrote on last edited by
                #7

                @JonB 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.
                Best regards!

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  What database are you using ?

                  tovaxT Offline
                  tovaxT Offline
                  tovax
                  wrote on last edited by
                  #8

                  @SGaist Hi, thank you very much for your reply, it's SQLITE.
                  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?

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @tovax
                    Well I wouldn't do A, I would do B, in some shape or form.

                    I have to consider whether it is appropriate to map the GUI data directly to the database.

                    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?

                    tovaxT Offline
                    tovaxT Offline
                    tovax
                    wrote on last edited by
                    #9

                    @JonB
                    Solution C
                    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.
                    Solution D
                    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.

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

                      Solution E: use update_hook from SQLite, but this will require some compilation...

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

                      tovaxT 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Solution E: use update_hook from SQLite, but this will require some compilation...

                        tovaxT Offline
                        tovaxT Offline
                        tovax
                        wrote on last edited by
                        #11

                        @SGaist 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.
                        Happy New Year!

                        1 Reply Last reply
                        0

                        • Login

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