Results 1 to 5 of 5

Thread: QTableView not refreshing

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView not refreshing

    Hi,

    I'm using my own model derived from QAbstractTableModel. My class looks something like :

    Qt Code:
    1. class DATreeModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DATreeModel(const QStringList &strings, QObject *parent = 0)
    7. : QAbstractTableModel(parent), stringList(strings) {}
    8.  
    9. DATreeModel(QObject *parent = 0);
    10. ~DATreeModel();
    11.  
    12. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    13. int columnCount(const QModelIndex &) const;
    14. QVariant data(const QModelIndex &index, int role) const;
    15. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    16. void setCanData( const QString& strData );
    17. void SetHeaders( QStringList& strHeaders );
    18.  
    19. private:
    20. QStringList stringList;
    21. QList<QString> m_Data;
    22. QStringList strHeaderList;
    23. };
    To copy to clipboard, switch view to plain text mode 

    This model is just read only so no need to override setData. Now the following snippet of code is used to set the data :

    Qt Code:
    1. QVariant DATreeModel::data( const QModelIndex& index, int role) const
    2. {
    3. QVariant data;
    4.  
    5. if (!index.isValid())
    6. return QVariant();
    7.  
    8.  
    9. if (index.row() > (m_Data.size() / 4) )
    10. {
    11. return QVariant();
    12. }
    13.  
    14. if(index.column() > 3 )
    15. {
    16. return QVariant();
    17. }
    18.  
    19. if( role == Qt::DisplayRole )
    20. {
    21. // just return data at given column
    22. int nPos = index.column() + ( 4 * index.row() );
    23. if( nPos < m_Data.size() )
    24. data = m_Data.at( nPos );
    25.  
    26. }
    27.  
    28. return data;
    29. }
    To copy to clipboard, switch view to plain text mode 

    And the following is used to insert new data :
    Qt Code:
    1. void DATreeModel::setCanData( const QString& strData )
    2. {
    3. m_Data.append(strData);
    4. QModelIndex nindex = index(m_Data.size(),0);;
    5. emit dataChanged(nindex, nindex );
    6. }
    To copy to clipboard, switch view to plain text mode 

    Alas, the table view is not 'refreshed' unless I click on the cells within it, I thought this would be automatic?

    Any help would be much appreciated.

    Steve

  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 not refreshing

    Quote Originally Posted by steg90 View Post
    Alas, the table view is not 'refreshed' unless I click on the cells within it, I thought this would be automatic?
    Make the model aware of the insertion with:
    J-P Nurmi

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

    steg90 (8th May 2007)

  4. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView not refreshing

    Thanks,

    Do I need to implement these functions in my model?

    Regards,
    Steve

  5. #4
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView not refreshing

    Hi again,

    I've now done this, thanks JPN

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

    Default Re: QTableView not refreshing

    An alternative is to emit layoutChanged() after inserting new data.

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. 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.