Results 1 to 4 of 4

Thread: QTreeView + QSortFilterProxyModel take a lot of system resources when scrolling

  1. #1
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView + QSortFilterProxyModel take a lot of system resources when scrolling

    Hello. I've got problem with QTreeView. I need to insert max 45000 rows to this widged, but when i exceed 5000 my program uses almost 100% CPU power.

    Creating QStandardItemModel, QTreeView and QSortFilterProxyModel:
    Qt Code:
    1. model = new QStandardItemModel(0, COLUMN_COUNT, parent);
    2. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Lp."));
    3. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Data"));
    4. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Czas"));
    5. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Opis modułu"));
    6. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Zdarzenie"));
    7. model->setHeaderData(5, Qt::Horizontal, QObject::tr("Priorytet"));
    8. model->setHeaderData(6, Qt::Horizontal, QObject::tr("0/1"));
    9. model->setHeaderData(7, Qt::Horizontal, QObject::tr("Adres"));
    10. model->setHeaderData(8, Qt::Horizontal, QObject::tr("Kod"));
    11. model->setHeaderData(9, Qt::Horizontal, QObject::tr("Typ"));
    12.  
    13. proxyModel = new QSortFilterProxyModel(this);
    14. proxyModel->setDynamicSortFilter(false); // dynamic sort filter is off
    15. proxyModel->setSourceModel(model);
    16. proxyModel->setFilterRegExp("[0-3]");
    17. proxyModel->setFilterKeyColumn(5);
    18.  
    19. proxyView = new QTreeView(this);
    20. proxyView->setRootIsDecorated(false);
    21. proxyView->setAlternatingRowColors(true);
    22. proxyView->setModel(proxyModel);
    23. proxyView->setSortingEnabled(true); //sorting enabled
    24. proxyView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    25. proxyView->setContentsMargins(0,0,0,0);
    26. proxyView->sortByColumn(2, Qt::AscendingOrder);
    27.  
    28. QVBoxLayout *mainLayout = new QVBoxLayout;
    29. mainLayout->addWidget(proxyView);
    30. mainLayout->setContentsMargins(0,0,0,0);
    31.  
    32. setLayout(mainLayout);
    To copy to clipboard, switch view to plain text mode 

    Adding row to QTreeView:
    Qt Code:
    1. void MainEvents::addDeviceEvent(QStandardItemModel *model, const QDate &date, const QTime &time,
    2. const QString &field, const QString &devEvent, const quint8 &priority, const qint8 &bits,
    3. const qint8 &address, const qint8 &code, const qint8 &type)
    4. {
    5.  
    6. model->insertRow(0);
    7. model->setData(model->index(0,0), model->rowCount());
    8. model->setData(model->index(0, 1), date);
    9. model->setData(model->index(0, 2), time.toString("hh:mm:ss.zzz"));
    10. model->setData(model->index(0, 3), field);
    11. QStandardItem *eventItem = new QStandardItem(devEvent);
    12. QFont font;
    13. if (bits != 0) font.setBold(true);
    14. eventItem->setFont(font);
    15. eventItem->setForeground(QBrush(eventNameColor[priority]));
    16.  
    17. model->setItem(0,4,eventItem);
    18. model->setData(model->index(0, 5), priority);
    19. model->setData(model->index(0, 6), bits);
    20. model->setData(model->index(0, 7), address);
    21. model->setData(model->index(0, 8), code);
    22. model->setData(model->index(0, 9), type);
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    I get data from serial device or from file in offline mode. In both modes problem exists so i include only reading data from file source:

    Qt Code:
    1. void MainEvents::openEventsFile(const QString &fileName)
    2. {
    3. QFile file(fileName);
    4. if (!file.open(QIODevice::ReadOnly))
    5. {
    6. QMessageBox::warning(this,tr("ENAP"),tr("Nie można otworzyć pliku %1:\n%2.").arg(file.fileName()).arg(file.errorString()));
    7. return;
    8. }
    9. QDataStream in(&file);
    10. in.setVersion(QDataStream::Qt_4_6);
    11.  
    12. quint32 header;
    13. in >> header;
    14. if (header != fileHeader)
    15. {
    16. QMessageBox::warning(this, tr("ENAP"),tr("Otwarty plik nie jest prawidłowym plikiem zdarzeń."));
    17. file.close();
    18. return;
    19. }
    20. QApplication::setOverrideCursor(Qt::WaitCursor);
    21. do
    22. {
    23. QDate date;
    24. in >> date;
    25. QTime time;
    26. in >> time;
    27. QString field;
    28. in >> field;
    29. QString event;
    30. in >> event;
    31. quint8 priority;
    32. in >> priority;
    33. quint8 bit;
    34. in >> bit;
    35. quint8 address;
    36. in >> address;
    37. quint8 code;
    38. in >> code;
    39. quint8 type;
    40. in >> type;
    41. addDeviceEvent(model, date, time, field, event, priority, bit, address, code, type);
    42. }
    43. while (!in.atEnd());
    44. QApplication::restoreOverrideCursor();
    45. file.close();
    46. proxyModel->invalidate();
    47. }
    To copy to clipboard, switch view to plain text mode 

    Is there any posibility to add 45000 elements to QTreeView with QSortFilterProxyModel (with ability to sorting and filtering) and scroll it without glitches?

    Thank you in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView + QSortFilterProxyModel take a lot of system resources when scrolling

    Since efficiency appears to be a problem here, I think I would start by replacing the QStandardItemModel with a custom subclass of QAbstractItemModel or QAbstractTableModel. Since each column appears to have a fixed data type (and many columns are simple, fixed-length types), you can probably build a much more efficient data structure for a backing store than one based on QStandardItems.

    If the strings involved have known maximum lengths, you can even create a single “plain old data” struct to hold each row.

    It will probably prove difficult to maintain both fast insertion of new records and fast sorting. If insertion speed is more important, keep one linked list and add to the end, then build an indexed list when you (re)sort. If fast sorting is more important, maintain a sorted list of pointers for each order in which you’ll care to sort and add each new record to all the lists in the appropriate place; but keeping that even reasonably efficient for insertions into a 45000-record set will require some creativity.

    Alternatively, it might be that using SQLite to maintain the table would work well for this amount of data.

  3. #3
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView + QSortFilterProxyModel take a lot of system resources when scrolling

    Thanks for fast reply! I'll try to replace this QStandardItemModel.

  4. #4
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView + QSortFilterProxyModel take a lot of system resources when scrolling

    I found simplier solution. I've set UniformRowHeights to true. Now everything works fine.

Similar Threads

  1. QTreeView problem scrolling to end.
    By seneca in forum Qt Programming
    Replies: 7
    Last Post: 22nd December 2015, 12:08
  2. QSortFilterProxyModel and QTreeView
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2009, 10:50
  3. Replies: 4
    Last Post: 6th February 2009, 19:18
  4. QTreeView, QSortFilterProxyModel and item expansions
    By benacler in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2008, 20:30
  5. QSortFilterProxyModel & QTreeView
    By Bear in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 15:04

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.