Results 1 to 5 of 5

Thread: QTableView update

  1. #1
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default QTableView update

    I subclassed QAbstractTableModel and implemented rowCount(), columnCount() and data(). The actual data is stored in a QList outside the model and the model has a pointer to the QList. Data manipulation of the QList occurs outside the model (I'm not using setData).

    In order to update the view, I implemented a method in the model that emits the dataChanged signal. The problem is that the view is not being updated.

    Qt Code:
    1. void ClipListTableModel::updateViews(QModelIndex start, QModelIndex end)
    2. {
    3. emit dataChanged(start, end);
    4. }
    To copy to clipboard, switch view to plain text mode 

    The start and end values are retrieved as follows. The table has 5 rows, hence the 4 in the argument.

    Qt Code:
    1. QModelIndex in1 = clipListModel->index(row, 0, QModelIndex());
    2. QModelIndex in2 = clipListModel->index(row, 4, QModelIndex());
    3. clipListModel->updateViews( in1, in2 );
    To copy to clipboard, switch view to plain text mode 

    To make sure that the model is working correctly, I used setModel() each time I wanted an update. Everything worked fine this way.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView update

    Notice that whenever you add/remove rows/columns to/from the model you need to call beginInsertRows() and endInsertRows() (or the corresponding ones depending on the action).
    J-P Nurmi

  3. #3
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: QTableView update

    Quote Originally Posted by jpn View Post
    Notice that whenever you add/remove rows/columns to/from the model you need to call beginInsertRows() and endInsertRows() (or the corresponding ones depending on the action).
    Does this mean that modification to the data has to be done through the model? If some data is tied to more than one model, then it doesn't make sense to modify the data through all the models.

    Please correct me if I'm wrong, and thank you for your reply.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView update

    Quote Originally Posted by inktomi View Post
    Does this mean that modification to the data has to be done through the model?
    It doesn't HAVE to be. You can do similar workarounds as you did for emitting dataChanged(). In a way or other, beginInsertRows() needs to be called before you start inserting the rows, and endInsertRows() needs to be called when you're done inserting the rows. BUT these all are protected methods of the model and it sounds like thing are getting out of your hands if you intend to control all this from outside the model.

    If some data is tied to more than one model, then it doesn't make sense to modify the data through all the models.
    To me it sounds like a good moment to take the design back on the drawing board... At least to me it would sound like a better idea to hide the data list inside a model. For controlling the data, you could have as convenient API as you want, you wouldn't have to use QAbstractItemModel's insertRows() or other methods from outside the model. Your data model class would just implement QAbstractItemModel interface as a bonus, and it could be attached to a number of views thanks to that.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    inktomi (16th July 2009)

  6. #5
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: QTableView update

    I made the neccesary changes so that the data is modified through the model.

    Thank you.

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2009, 10:47
  2. Problem in QProgressBar update
    By nikhilqt in forum Qt Programming
    Replies: 0
    Last Post: 10th March 2009, 10:20
  3. Qt Update project - Opinions wanted
    By pvdk in forum Qt Programming
    Replies: 0
    Last Post: 8th November 2008, 08:41
  4. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 09:42
  5. Replies: 9
    Last Post: 7th November 2006, 15:10

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.