Results 1 to 4 of 4

Thread: Refreshing a QTableView when QAbstractTableModel changes

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Refreshing a QTableView when QAbstractTableModel changes

    Hi

    I have a few QTableView widgets, all connected to custom QAbstractItemModel based models I created for the different tables. I'm not sure what the best way is to refresh both of them (QTableView and model connected to it) when the data change. At the moment I'm doing the following every time the data changes:

    Qt Code:
    1. realMatrixModel = new MatrixRealModel(internalRealMatrix);
    2. m_ui->MatrixTable->setModel(realMatrixModel);
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if it is optimal, but it sort of works. The problem is that the QTableView does not refresh until I click on it. I've tried repaint(), setFocus() etc. etc. but nothing thus has the same effect as clicking on the QTableView.

    How can I do this?

    Thanks in advance,
    Jaco

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Refreshing a QTableView when QAbstractTableModel changes

    You should use signal QAbstractItemModel::dataChanged(). Every time data change in your model, it has to emit that signal to notify views and/or proxy models that data has changed. Typicaly it is emitted in QAbstractItemModel::setData() after setting data, as it stands in Qt docs: "The dataChanged() signal should be emitted if the data was successfully set."
    All views will refresh changed items.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Refreshing a QTableView when QAbstractTableModel changes

    I did look at dataChanged(). The problem is not when the data change during a setData() call.
    The model has an internal pointer that points to a data structure. The application changes this data structure during runtime, thus setData is not called. The problem is that dataChanged() is protected so it does not work unless called inside the model itself. I can probably connect a data structure change signal to a slot in the model that emits the dataChanged() signal, however I thought there should be an easier way.

    For example: When I click anywhere on the QTableView after the data structure changed, it updates the QTableView.
    Another example: When I add a message box just after the code in my first post the QTableView refreshes.

    Does these operations emit the dataChanged() signal? Or is there an easier way?

    Thanks
    Jaco

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refreshing a QTableView when QAbstractTableModel changes

    Quote Originally Posted by JPNaude View Post
    The model has an internal pointer that points to a data structure. The application changes this data structure during runtime, thus setData is not called.
    Then make a custom method in your model to change that data so that the application can do it using that method and make that method emit dataChanged().

Similar Threads

  1. QTableView not refreshing
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2007, 19:37
  2. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.