Hi,

I am using model view for displaying large data (tree view with millions of rows), I have customized my model & view (QAbstractItemModel, QTreeView).
What I have observed is as number of rows (data) grows high, updating of headers & selection becomes too slow.

I am changing the header data each time I click an item (if row is even number one header set, if odd another header set).
If my model has 1 - 1000000 rows, the header is updating as expected (no delay in updating header on clicking item).
I test by returning 10000000 rows(top level items) in model & when I started clicking on items header data is updating very slowly & items selection also becomes very slow.
When I tested with 20000000 rows it becomes further late.

Here is My implementation:

Model:

Qt Code:
  1. Model::Model(QObject *parent) :
  2. {
  3.  
  4. }
  5.  
  6. Model::~Model()
  7. {
  8.  
  9. }
  10.  
  11. void Model::setHeaderData(QStringList& head)
  12. {
  13. m_head = head; //m_header is QString List for headers
  14. }
  15.  
  16. void Model::headerChanged(int row) //This is slot connected to view, will be called when an items clicked in view with index.row() as parameter.
  17. {
  18. if(row%2){
  19. head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
  20. }
  21. else{
  22. head << " Head11 " << " Head12 " << " Head13 " << " Head14 " << " Head15 " << " Head16 " << " Head17 " << " Head18 " ;
  23. }
  24. m_head = head;
  25. emit headerDataChanged(Qt::Horizontal, 0, head.size() - 1);
  26. }
  27.  
  28. QVariant Model::headerData ( int section, Qt::Orientation orientation, int role ) const
  29. {
  30. if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
  31. {
  32. return m_head.at(section);
  33. }
  34. else if ( orientation == Qt::Vertical && role == Qt::DisplayRole ){
  35. return section;
  36. }
  37. return QVariant();
  38. }
  39.  
  40. int Model::columnCount(const QModelIndex &parent) const
  41. {
  42. return 8;
  43. }
  44.  
  45. int Model::rowCount(const QModelIndex &parent) const
  46. {
  47. if(!parent.isValid()) return 20000000;
  48. if(parent.isValid() && !parent.parent().isValid()) return 2; // limit to two levels
  49. return 0;
  50. }
  51.  
  52. QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
  53. {
  54. if(parent.isValid())
  55. {
  56. // we only need id of the parent since it identifies the item
  57. return createIndex(row, column, parent.row());
  58. }
  59. return createIndex(row, column, -1); // top level item has invalid parent
  60. }
  61.  
  62. QModelIndex Model::parent(const QModelIndex &ind) const
  63. {
  64. if(!ind.isValid()) return QModelIndex();
  65. int id = ind.internalId();
  66. if( id == -1) return QModelIndex();
  67. return index(id, 0); // return top-level item
  68. }
  69.  
  70. QVariant Model::data(const QModelIndex &index, int role ) const
  71. {
  72. if(role == Qt::DisplayRole) return index.row()+1;
  73. return QVariant();
  74. }
To copy to clipboard, switch view to plain text mode 


Any Idea why updating headers is too slow when number of items becomes more ???


Thanks in advance.