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. 1 Database + 2 Models + 2 Views
Forum Updated to NodeBB v4.3 + New Features

1 Database + 2 Models + 2 Views

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k 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.
  • K Offline
    K Offline
    kreyszig
    wrote on last edited by
    #1

    Hi all

    How do I get two QSqlTableModels, both using the same database, to update based on the other? I may be doing this wrong, but I am using a QDataWidgetMapper to drive a set of widgets on one dialog. So that means I need a model that is always looking at the same table in the database.

    I have another view that I want to be able to change tables, so that requires a different model, as far as I know. I am using the same database connection for both models...perhaps this is wrong.

    I presume that I can't use the same model to display a different table in two different views.

    When the mapped ui items update their model, I would like the values to be updated in the second model (if it has the same table selected, obviously). And vice-versa. However this doesn't happen.
    How do I accomplish this?

    I have tried reselecting both the models but this doesn't seem to help.

    Many thanks for your help!

    Edit:
    Both the models are set to use QSqlTableModel::OnFieldChange as their editStrategy
    The mapper is set to DataWidgetMapper::AutoSubmit
    Interestingly when the non-mapped tableview is edited, the results are seen in the mapped widgets, but only if I use the previous and next buttons I've set up to call toNext and toPrevious. I.e if I'm at the first record, I hit next, but it takes me to the new first record rather than the second.
    Edit Two:
    I've created two slots for the dataChanged signal of each model, and neither gets hit..?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      [quote]
      When the mapped ui items update their model, I would like the values to be updated in the second model (if it has the same table selected, obviously). And vice-versa. However this doesn’t happen.
      How do I accomplish this?
      [/quote]

      Unfortunately, there's no standard way a SQL model can know when the underlying table was modified (by another model, or in general by some external modification). You have to write a workaround.

      For example:

      • If your DB supports notifications, you can use QSqlDriver::subscribeToNotification and call "NOTIFY" in a trigger, which will cause the QSqlDriver::notification signal emission; in a slot, you can simply ask the model to select() again.
      • If your DB doesn't, you can for instance make some trigger to add a row in a table, and from your Qt code you poll that table.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kreyszig
        wrote on last edited by
        #3

        OK. Thanks for the those. Notify sounds like a way forwards.
        I've been trying a few other options, following is the simplest:

        If I am using the mapper I now set the other view to use the same model. So now there's one model, two views. Simples, right?
        One view is a tableview, the other a set of widgets using a mapper.

        Still, things are not as I expect. I followed the books example, and connected the tableview rowchanged signal to the mappers setcurrentmodelindex. This changes the selection in the mapper if I select a row in the tableview.

        If I alter anything in the tableview, it is reflected in the mapper widgets, when I click in a different cell (not when I hit enter)
        But if I change anything in the mapper widgets, only the first change is reflected in the tableview. Ideas?

        Edit: working now. I changed to QSqlRelationalTableModel from QSqlTableModel and all seems fine.

        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