Results 1 to 9 of 9

Thread: Updating header data is too slow as data grows high in Model view architecture.

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Updating header data is too slow as data grows high in Model view architecture.

    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.
    Thanks :-)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    My guess, turn off automatic column resizing in your view:
    QHeaderView::setSectionResizeMode(), or QHeaderView::setResizeMode(), with something other than QHeaderView::ResizeToContents.

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Quote Originally Posted by ChrisW67 View Post
    My guess, turn off automatic column resizing in your view:
    QHeaderView::setSectionResizeMode(), or QHeaderView::setResizeMode(), with something other than QHeaderView::ResizeToContents.
    I have tried all the possible combinations of serResizeMode (interactive, fixed, stretch, resizeToColumnContent), I could see same amount of delay in updating headers & selection each time.
    Thanks :-)

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Any suggestions from Qt experts ??
    Thanks :-)

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Any attempt to identify the actual source of the slowness on your part?

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Quote Originally Posted by prasad_N View Post
    Any Idea why updating headers is too slow when number of items becomes more ???
    You are setting the vertical header with the row number, so yes, as you have more rows, you will have more vertical headers to set. Have you tried not using vertical headers to see if that is indeed the reason your app slows down as you increase the number of rows?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Quote Originally Posted by ChrisW67 View Post
    Any attempt to identify the actual source of the slowness on your part?
    Yes, for each mouse click on item it is calling row count for all the top level items (this can be one of the problem, My post regarding this & I did not get any solution for both of these problems http://www.qtcentre.org/threads/6303...ems?highlight= ).


    Added after 5 minutes:


    Quote Originally Posted by jefftee View Post
    You are setting the vertical header with the row number, so yes, as you have more rows, you will have more vertical headers to set. Have you tried not using vertical headers to see if that is indeed the reason your app slows down as you increase the number of rows?
    Not actually, I have 2 header sets each of them are 8 (there is no dependency on rows here), And when I remove variable headers or remove complete headers then there is no slowness in my app as soon as my clicking on items its getting selected (Probably you can experiment little by take my mode).

    One reason could be in my reply #7
    Last edited by prasad_N; 14th July 2015 at 06:22.
    Thanks :-)

  8. #8
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    Here is my code, I have tried to find a solution for this but could not find any.
    can some one suggest a way to get over this?

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


    View:
    Qt Code:
    1. treeview::treeview(Model* model)
    2. {
    3. setModel(model);
    4. connect(this, SIGNAL(clicked(QModelIndex)), model, SLOT( headerChanged(QModelIndex) ));
    5. setUniformRowHeights(true);
    6. }
    7.  
    8. treeview::~treeview()
    9. {
    10. }
    To copy to clipboard, switch view to plain text mode 

    main:
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4. Model model;
    5. treeview v(&model);
    6. v.setFixedSize(700, 800);
    7.  
    8. head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
    9. model.setHeaderData(head);
    10. v.setModel(&model);
    11. v.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.
    Thanks :-)

  9. #9
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Updating header data is too slow as data grows high in Model view architecture.

    got the solution finally, if any one has similar issue, please look at the below post replay's from #15 to #23

    http://www.qtcentre.org/threads/6303...554#post279554
    Thanks :-)

Similar Threads

  1. QSqlRelationalTableModel -> Updating model's data
    By toma in forum Qt Programming
    Replies: 5
    Last Post: 9th July 2014, 22:25
  2. Replies: 1
    Last Post: 11th July 2012, 23:46
  3. Replies: 1
    Last Post: 24th February 2011, 06:54
  4. Table view->model->set data
    By tulsi in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 09:36
  5. Replies: 1
    Last Post: 1st March 2006, 12:43

Tags for this Thread

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.