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. How do I make a QSqlTableModel and/or the corresponding QTableView react on external changes to the database table?
Forum Updated to NodeBB v4.3 + New Features

How do I make a QSqlTableModel and/or the corresponding QTableView react on external changes to the database table?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 429 Views
  • 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.
  • C Offline
    C Offline
    ChefDeCambuse
    wrote on last edited by
    #1

    Hello,

    I'm evaluating PyQT5 for a project of mine and I'm struggling using the database related things.
    I hope this is the right place to ask, if not please point me in the right direction.

    I have a standalone process that displays the content of a MySQL table in a QTableView.
    There is another process, that displays one row of that table in QLineEdit fields and allows to edit and update these values, delete the row or insert a new one.

    The process with the view checks every 10 seconds for a change in update_time on that table. (Haven't found a better solution for this.)

    I've read, that if such a change is detected I should call QSqlTableModel .select(). Which rereads the data.

    But in doing this for example on a value update, a before selected row of the QTableView gets unselected and the position of the view changes.
    So apparently this is the wrong approach. What is the proper way to reflect changes in the database table and keep the states of the view otherwise unmodified?

    Thanks for your time!
    Frank

    JonBJ 1 Reply Last reply
    0
    • C ChefDeCambuse

      Hello,

      I'm evaluating PyQT5 for a project of mine and I'm struggling using the database related things.
      I hope this is the right place to ask, if not please point me in the right direction.

      I have a standalone process that displays the content of a MySQL table in a QTableView.
      There is another process, that displays one row of that table in QLineEdit fields and allows to edit and update these values, delete the row or insert a new one.

      The process with the view checks every 10 seconds for a change in update_time on that table. (Haven't found a better solution for this.)

      I've read, that if such a change is detected I should call QSqlTableModel .select(). Which rereads the data.

      But in doing this for example on a value update, a before selected row of the QTableView gets unselected and the position of the view changes.
      So apparently this is the wrong approach. What is the proper way to reflect changes in the database table and keep the states of the view otherwise unmodified?

      Thanks for your time!
      Frank

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

      @ChefDeCambuse
      This is the right way. In that, the databases don't support "notify", where they would let your app know if the data has changed. So polling from client is probably as good as it gets. In your case if you really wanted, when other process sends updates to database you could make it so it uses some mechanism (e.g. IPC) to let your reading process know it has done so, and that could read the database shortly thereafter, but it would be more work, and only if you are in control of the process which updates.

      Yes, QSqlTableModel .select() is required to re-read all data to refresh your view. If you want to do things like keep a certain row selected, or the view scrolled to a certain place, it is your job to read the state before the update and restore appropriately once the new data has arrived. Qt doesn't offer a "just this row has changed in the database", if you wish to recognize that for the view you'd have to write code to achieve.

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        See also https://forum.qt.io/topic/117274/update-multiple-models-based-on-an-update-of-data

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • C Offline
          C Offline
          ChefDeCambuse
          wrote on last edited by
          #4

          Thank you very much!
          I've got all in place and up and running.
          But I'm still puzzled, that these things are not already included.

          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