2

I’m old Delphi/FreePascal/Lazarus programmer. I’m programming in Qt and C++ from a year. The most painfull for me in Qt is model programming. Especially efficient. For example filling QStandardItemModel with 1 million records take ~15 seconds, 700MB memory usage and QTreeView scrolling is almost impossible (100% CPU, seems that each scroll validate even not visible items). For comparison, in Lazarus I have TVirtualTreeView where I override one virtual method OnGetData. In arguments I get row and column index and returning data from TList (something like QList) or from dynamic array. Result: filling 1 million records = 150 ms, +20MB in RAM, fully responsible scrolling (OnGetData is called only for visible items). I tried also with QAbstractItemModel, a lot of coding and I only reduced memory usage (not so much).

My question: Is there any alternative for model programming? Few months ago I saw tutorial for custom dataset in QTreeView but can’t find it now. They used simple QList and override few methods in QTreeView.

Regards

Dibo
  • 1,159
  • 17
  • 34
  • 3
    QStandardItemModel is a complex jack-of-all-trades, with a lot of overhead. For a million items, a custom model is a must. Then QAIM is just an API for your own data. One OOTB option might be sqlite and a stock Qt SQL model. – hyde Aug 28 '14 at 20:33
  • Could you post some sample code? QStandardItemModel won't perform well with that many entries. See [this question](http://stackoverflow.com/q/21159095/1583123). – Andrew Dolby Aug 28 '14 at 20:37
  • Also, the model-view system is perhaps the most complex part of Qt, so take learning it as a big task, needing real studying. Start with simpler things like subclassing QAbstractListModel, do not jump straight into subclassing QAIM. – hyde Aug 28 '14 at 20:47
  • 1
    A `QStandardItemModel` is a convenience class. It's meant to be used for small amounts of items. The `QAbstractItemModel` will save memory because the view is not caching the elements in any way. You also need to set a fixed item height on the view. The updates are then instantaneous. IOW: You're not using it right. – Kuba hasn't forgotten Monica Aug 28 '14 at 22:34
  • 1
    I think you'll have to provide a short, self contained test case that populates the model with some data to demonstrate the issue. We can then show what you're doing wrong. Without code it's a bit too broad. – Kuba hasn't forgotten Monica Aug 29 '14 at 13:14

2 Answers2

2

Derive your own model directly from QAbstractItemModel. Use it as an interface for your custom (efficient) data storage.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
2

Ok I rewrote my model carefully while reading QAbstractItemModel documentation. I reduced memory usage a lot but still had lags while scrolling 1 million records. I changed QTreeView to QListView / QTableView and everything worked very fast. So I checked QTreeView properties and disabled itemsExpandable and checked uniformRowHeights. Finally my view is fast and furious :) . Loading 1 mln record = 1 second, +140MB in RAM, smooth scrolling without lags. Thanks!

Dibo
  • 1,159
  • 17
  • 34