Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QStandardItemModel: efficient way to add a batch of items?
Is there any efficient way to add a batch of QStandardItems to a model? The model is being cleared and then I'm adding a lot of items with setItem. QtreeWidget has addTopLevelItems ( const QList<QTreeWidgetItem *> & items ) for just this, how to optimize such addition with model / view architecture?
The problem is that I may need to add thousands of items, and every time I do setItem a sort model is sorting the contents and view is invoked to be updated and it's taking a lot of time. If I could clear the model, disable its dataChanged() signal, add all the items and then notify about changes once - it would be much more efficient.
Jeroentjehome last edited by
I think the best way is to inherit the QAbstractItemModel and do it yourself. The QStandardItemModel is not optimized for the insertion of large chunks of data. When you subclass the model you are able to control when the view is updated, so after the big load of data.
That's not an option. I've had to resort to QStandardItemModel after spending a week trying to make my QAbstractItemModel subclass work.
I've been advised to append all the columns to a single QStandardItem, and then to add a bunch of items with QStandardItemModel::appendColumn(const QList<QStandardItem *> & items). What do you think?
Jeroentjehome last edited by
Hmm, That doesn't sound very nice.
It might work when you need to append a row, but what about toplevel items? When you include a new column it will be a sublevel?
IYAM it's better to figure out what went wrong with your qabstracttableModel instead.
The tutorial is here: "Model/View tutorial":http://qt-project.org/doc/qt-4.8/modelview.html#1-3-overview-of-the-model-view-widgets
Maybe start by using the QAbstractTableModel instead. That's a bit easier to control at first. It seems a challenge at first, but when you get the principle of operation you will decrease code and increase flexibility.
Otherwise start with the tutorial example of the TableView/QAbstractTableModel and go from there. Must say that the QModelIndex of a QTreeView use is a bit tricky.
When your stuck, you may send me a mail, or post it here and we'll take a look.
I've actually studied that tutorial, and even thought I understand the basics and can make the model work. But it kept crashing and behaving in a way I couldn't understand.
Can I use QAbstractTableModel with a QTreeView? I only have top-level items, no siblings.
worthy7 last edited by
Work out the number of rows you will need in advanced and set the row count before adding items.
[quote author="worthy7" date="1388191631"]Work out the number of rows you will need in advanced and set the row count before adding items.
Thanks! If it really makes things faster - that's a great tip.