Results 1 to 5 of 5

Thread: QSqlTableModel, how to recognize new records from dirty records

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel, how to recognize new records from dirty records

    Hi,
    In my project I am using a QtableView and a QSqlTableModel connected to a database table.

    The view should be able to distinguish new inserted records from modified (dirty) records, because I need to apply 2 different background color.

    Is it possible to do that?

    Franco
    Franco Amato

  2. #2
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel, how to recognize new records from dirty records

    It seems that's not possible
    Franco Amato

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,153
    Thanks
    295
    Thanked 854 Times in 841 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlTableModel, how to recognize new records from dirty records

    It seems that's not possible
    If you have derived your own model from QSqlTableModel, then I assume you are using data() and setData() to modify the database. If you haven't derived your own class, then do so and reimplement the data() and setData() methods. The setData() method is called whenever the table is edited with the QModelIndex of the cell that was modified. Use this index to retrieve the row number that was modified, and push it into a QSet. In the data() method, each time it is called with Qt::BackgroundRole or Qt::ForegroundRole, look up the row number in the set. If it is found, then return a QBrush with the "modified" color, otherwise return nothing and the default brush will be used.

    When the modified rows have been committed to the database, clear the set.

    Instead of deriving from QSqlTableModel, you could also use a QIdentityProxyModel and keep the QSet there. You will still have to implement data() and setData() as I described above. This is more flexible because you can use the SQL model without the proxy when you don't want row coloring, and use it with the proxy in other places. Or use a different proxy for other purposes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    franco.amato (20th March 2022)

  5. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel, how to recognize new records from dirty records

    Hi,
    but I need to distinguish edited rows from new rows.
    Edited rows => color 1
    New rows => color 2
    Unchanged rows => default color
    Last edited by franco.amato; 20th March 2022 at 04:02.
    Franco Amato

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,153
    Thanks
    295
    Thanked 854 Times in 841 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlTableModel, how to recognize new records from dirty records

    Then you need to look into handling the QAbstractItemModel signals that give notification of changes in the model - dataChanged(), rowsInserted(), layoutChanged(), etc.

    It sounds like you are using your table view to drive your model - that is, things get modified in the table view and then get pushed to the model rather than the other way around. If that's the case, then you are going to have to do a lot of the bookkeeping about what has changed yourself, because the model will not know until you have committed the changes. This approach is sort of backwards, I think. Usually the model is what gets updated, and it then tells the views what has changed using the signals described above.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. The following user says thank you to d_stranz for this useful post:

    franco.amato (20th March 2022)

Similar Threads

  1. Replies: 2
    Last Post: 2nd September 2010, 17:34
  2. Updating QAbstractItemModel with new records
    By psih128 in forum Qt Programming
    Replies: 0
    Last Post: 10th May 2010, 02:02
  3. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 15:54
  4. Combobox Delegate 25.000 records
    By aekilic in forum Qt Programming
    Replies: 9
    Last Post: 29th July 2008, 13:26
  5. Writing Log Records To A Log File
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2007, 14:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.