Results 1 to 3 of 3

Thread: Update QAbstractTableModel?

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Update QAbstractTableModel?

    I made use of QAbstractTableModel to show data retrieved through QNetworkAccessManager in a QTableView, it works well, the only issue is that if the data is changed the model doesn't "refresh" until I click on the tableview or scroll through the table.

    header:
    Qt Code:
    1. class ModelLClienti:public QAbstractTableModel{
    2. Q_OBJECT
    3. public:
    4. ModelLClienti(QObject *parent=0);
    5.  
    6. int rowCount(const QModelIndex &parent) const;
    7. int columnCount(const QModelIndex &parent) const;
    8. QVariant data(const QModelIndex &index, int role) const;
    9. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    10. void setLista(QList<QString> lista);
    11. private:
    12. QList<QString> listaCli;
    13. };
    To copy to clipboard, switch view to plain text mode 

    source:
    Qt Code:
    1. ModelLClienti::ModelLClienti(QObject *parent):QAbstractTableModel(parent){
    2. }
    3.  
    4. int ModelLClienti::rowCount(const QModelIndex &parent) const{
    5. Q_UNUSED(parent);
    6. return listaCli.size()-1;
    7. }
    8. int ModelLClienti::columnCount(const QModelIndex &parent) const{
    9. Q_UNUSED(parent);
    10. return 2;
    11. }
    12. QVariant ModelLClienti::data(const QModelIndex &index, int role) const{
    13.  
    14. if (!index.isValid())
    15. return QVariant();
    16.  
    17. if (index.row() >= listaCli.size() || index.row() < 0)
    18. return QVariant();
    19.  
    20. if (role == Qt::DisplayRole) {
    21. l=listaCli.at(index.row()).split(',');
    22. if(index.column()==0)
    23. return l[0];
    24. if (index.column() == 1)
    25. return l[1].toUpper();
    26. }
    27. return QVariant();
    28. }
    29. QVariant ModelLClienti::headerData(int section, Qt::Orientation orientation, int role) const{
    30. if (role != Qt::DisplayRole)
    31. return QVariant();
    32.  
    33. if (orientation == Qt::Horizontal) {
    34. switch (section) {
    35. case 0:
    36. return tr("clientId");
    37. case 1:
    38. return tr("Client");
    39. default:
    40. return QVariant();
    41. }
    42. }
    43. return QVariant();
    44. }
    45. void ModelLClienti::setLista(QList<QString> lista){
    46. listaCli=lista;
    47. }
    To copy to clipboard, switch view to plain text mode 

    I need to change the data inside the model and afterwards, show in the tableview.

  2. #2
    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: Update QAbstractTableModel?

    When you change data in the model, you have to emit dataChanged() signal for the index range that gets changed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    adutzu89 (4th June 2014)

  4. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Update QAbstractTableModel?

    Thank you, I managed it.

Similar Threads

  1. QFileSystemModel - Incremental update/pre-emptive update
    By johnnyturbo3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd September 2011, 13:56
  2. Replies: 2
    Last Post: 29th September 2010, 17:44
  3. Slow update in QAbstractTableModel in setData
    By cevou in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2010, 14:04
  4. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 09:09

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.