Results 1 to 3 of 3

Thread: QAbstractTableModel SetRowCount problem

  1. #1
    Join Date
    Oct 2009
    Posts
    28
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QAbstractTableModel SetRowCount problem

    Hello.
    I have on class that subclass QAbstractTableModel, like this:
    Qt Code:
    1. class CTableModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CTableModel(QObject *parent=0);
    7. virtual ~CTableModel();
    8.  
    9. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    10. void setRowCount(int numRows);
    11. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    12. void setColumnCount(int numColumns);
    13.  
    14. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    15. bool setData(const QModelIndex &index, const QVariant & value, int role = Qt::EditRole );
    16.  
    17. void setHorToolTipList(QStringList lista);
    18. Qt::ItemFlags flags ( const QModelIndex & index ) const;
    19.  
    20. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    21. bool setHeaderData( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole);
    22.  
    23. private:
    24. QVector< QVector <QString> > items;
    25. QStringList horHeaders,verHeaders,horToolTip;
    26.  
    27. };
    To copy to clipboard, switch view to plain text mode 

    And the .cpp,
    Qt Code:
    1. int CTableModel::rowCount(const QModelIndex &parent) const
    2. {
    3. return items.count();
    4. }
    5.  
    6. void CTableModel::setRowCount(int numRows)
    7. {
    8. if (numRows > rowCount())
    9. {
    10. int tamAnt = rowCount();
    11. items.resize(numRows);
    12.  
    13. for (int i=tamAnt;i<numRows;i++)
    14. items[i].resize(columnCount());
    15. }
    16. else
    17. {
    18. if (numRows < rowCount())
    19. {
    20. items.resize(numRows);
    21. }
    22. }
    23.  
    24. }
    25.  
    26. int CTableModel::columnCount(const QModelIndex &parent) const
    27. {
    28. if (items.count() == 0)
    29. return 0;
    30. return items[0].count();
    31. }
    32.  
    33. void CTableModel::setColumnCount(int numColumns)
    34. {
    35. for(int i=0;i<items.count();i++)
    36. items[i].resize(numColumns);
    37. }
    38.  
    39. QVariant CTableModel::data(const QModelIndex &index, int role) const
    40. {
    41. if (index.isValid())
    42. {
    43. if (role == Qt::DisplayRole)
    44. return items[index.row()][index.column()];
    45. }
    46. return QVariant();
    47.  
    48. }
    49.  
    50. bool CTableModel::setData(const QModelIndex &index, const QVariant & value, int role)
    51. {
    52. if (index.isValid() && role == Qt::EditRole)
    53. {
    54. items[index.row()][index.column()] = value.toString();
    55. emit dataChanged(index,index);
    56. return true;
    57. }
    58. return false;
    59. }
    60.  
    61. void CTableModel::setHorToolTipList(QStringList lista)
    62. {
    63. horToolTip = lista;
    64. }
    65.  
    66. Qt::ItemFlags CTableModel::flags ( const QModelIndex & index ) const
    67. {
    68.  
    69. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    70. }
    71.  
    72. QVariant CTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    73. {
    74. if (role != Qt::DisplayRole)
    75. {
    76. if (orientation == Qt::Horizontal)
    77. {
    78. if(role == Qt::ToolTipRole)
    79. {
    80. return horToolTip[section];
    81. }
    82. }
    83. return QVariant();
    84. }
    85. if (role == Qt::DisplayRole)
    86. {
    87. if (orientation == Qt::Horizontal)
    88. {
    89. return horHeaders.at(section);
    90. }
    91. else
    92. {
    93. return verHeaders.at(section);
    94. }
    95. }
    96. }
    97.  
    98.  
    99. bool CTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role)
    100. {
    101. if (role != Qt::EditRole)
    102. return false;
    103. if (orientation == Qt::Horizontal)
    104. {
    105. if (section > horHeaders.count()-1)
    106. horHeaders.append(value.toString());
    107. else
    108. horHeaders[section] = value.toString();
    109. emit headerDataChanged(orientation,section,section);
    110. return true;
    111. }
    112. else
    113. {
    114. if (section > verHeaders.count()-1)
    115. verHeaders.append(value.toString());
    116. else
    117. verHeaders[section] = value.toString();
    118. emit headerDataChanged(orientation,section,section);
    119. return true;
    120. }
    121.  
    122. }
    To copy to clipboard, switch view to plain text mode 

    I set the numbers of rows with setRowCount and the first time it works fine. But when I try to change it again, it doesn't work anymore. It weird because when I call model->rowCount(), the result is ok, but the view doesn't update the numbers of row.

    Thanks a lot.
    Regards.

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

    Default Re: QAbstractTableModel SetRowCount problem

    If you change the number of rows emit the layoutChanged() signal in the setRowCount() function. The same goes for the setColumnCount() function.

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

    gutiory (25th November 2010)

  4. #3
    Join Date
    Oct 2009
    Posts
    28
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractTableModel SetRowCount problem

    Thanks a lot JPNaude!!.
    I works great.

Similar Threads

  1. Replies: 3
    Last Post: 18th April 2014, 03:37
  2. QAbstractTableModel and tooltip
    By mpi in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2010, 19:58
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 15:21
  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.