Could we see the code you use for adding data to the model?
Could we see the code you use for adding data to the model?
This is all i use right now to add/modify my model data.
Qt Code:
if (m_rowNumsForItems.contains(item.ObjectId) == false) { /// if the item isn't already in the list, it is added, and the resulting rownum recorded. if (rowCount() - m_lastUpdatedRow == 10) { } m_items.push_back(item); m_rowNumsForItems.insert(item.ObjectId, rowCount() - 1); if (rowCount() - m_lastUpdatedRow == 11) { endInsertRows(); m_lastUpdatedRow = rowCount(); } } else { /// if the item is in the list, it is replaced and dataChanged is emitted. rowNum = m_rowNumsForItems.value(item.ObjectId); m_items.replace(rowNum, item); emit dataChanged(index(rowNum, 0), index(rowNum, columnCount() - 1)); }To copy to clipboard, switch view to plain text mode
(The 10 is purely arbitrary, and of course, i need to add some kind of timer, which i dont currently have, so that it doesnt happen that the last <10 items are never shown )
And how do you call it?
The worker thread emits a signal, with an item as parameter. In the slot connected to it in the main thread i call this addItem function, with the received item as parameter.
For each single row separately? Why not insert all pending data at once?
I don't really understand what you mean by pending. Slot is called with 1 item as parameter, addItem is called in the model with that one item. That is what happens. I tried to lessen the burden on the ui by doing that beginInsertRows/endInsertRows only for every 10 of these items, in bulk, but maybe that was a mistake, because it has the effect that the view has always less items then the model behind it.
Maybe i should queue the items *always*, in the slot, and add them to the model in bulk?
Actually taking this buffering outside the model?
Now I think you are doing something weird... Isn't it true that you do:
insert row 1
insert row 2
...
insert row 9
beginInsertRows
insert row 10
endInsertRows
Instead you should be doing:
add row 1 to other list
add row 2 to other list
...
add row 10 to other list
beginInsertRows
add all 10 rows from the other list to the model list
endInsertRows
Exactly what im doing now, and exactly the solution which i asked i should do.
Yes, now that i think of it, it is weird.
I'll try to do just that + a timer (because it can happen that after a while, the initial surge of data dies down, and maybe i receive 10 new items in 5 minutes, instead of 10ms, so i periodically have to transfer the list to the model list).
I will try this, hope it works.
Thanks a lot, Wysota, and you too Marcel, i'm very glad you both are willing to devote time to sort out through my rookie mistakes.
You can probably use the canFetchMore() and fetchMore() feature of the model to do it without timers.
I use the treeview currently as a tableview, i.e. only top level items, no children.
I've never used canFetchMore() and fetchMore(), could you please explain a bit how it can help?
The view is irrelevant, it all happens inside the model. canFetchMore() is called to ask whether the model needs to update itself with new rows and fetchMore() does the update.
In your case canFetchMore() should return true if there are any pending rows and fetchMore() should append them to the model. I don't know if it will work (I never used those two methods), but it's worth to try. At worst you'll have to call those two methods yourself from within the timer.
Bookmarks