How can I improving performance when I use QListView with large numbers of items
-
Could somebody tell me the concrete method about that? I have used QListView with QAbstractTableModel to insert date.But it will makes the main UI stop a few seconds.
It is possible to give the view hints about the data it is handling in order to improve its performance when displaying large numbers of items.
-
A List view but Table model? That sounds weird.
Anyway, here are some hints:
- use lazy loading (
canFetchMore()
andfetchMore()
) - set uniformRowHeights or uniformItemSizes to true
- use batched layout mode https://doc.qt.io/qt-5/qlistview.html#layoutMode-prop
- analyze your model's
data()
method, make sure it runs as fast as possible - start loading your data even before the view is shown (on application startup, for example) - if possible
- make sure your delegates are as simple as possible
- use lazy loading (
-
hi @Mihan
I had the case where I had a very fancy delegate for my ListView, that slowed things down dramatically when one would scroll very quickly.
If that's the bottleneck for you as well, then you could do the same as me.
Replace the delegate with a Loader, set it to asynchronous = true, and only active, if its on screen or the next/previous one in line.
An other fancy Idea I had for that, would be to replace the ListView with a PathView (it circles around) and simply change the data accordingly. Needs a separated index management but should speed things up quickly.
Reading through @sierdzio 's list. Seems like I reinvented the wheel as the batched layout mode is basically that ...
And I'm talking about QML here.
Oh man, I'm not on game today. Need more coffee! -
There is QAbstractListModel. You don't have to change, though. If table model works for you, it can stay.
-
@sierdzio Yep,it works. I think it's just one more overloaded function (columnCount) than QAbstractListModel, rigth?
By the way,How can i optimize the date() method. ```
QVariant CLogSqlModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
{
return m_LogList.at(index.row()).LogTime.toString("yyyy.MM.dd hh:mm") + m_LogList.at(index.row()).LogDesp;
}
else if (role == Qt::BackgroundRole)
{
if(index.row()%2 == 0)
return QColor(Qt::darkGray);
else
return QVariant();
}
else
return QVariant();
} -
Hm ok that looks simple enough. Only small optimization I'd suggest is for DisplayRole:
const auto &item = m_LogList.at(index.row()); return item.LogTime.toString("yyyy.MM.dd hh:mm") + item.LogDesp;
to avoid traversing the list twice. But I don't know any implementation details here, so it is mostly a guess.
Does m_LogList perform any heavy calculations, or complex data reads (SQL or something), or is it just a static list?
-
@Mihan
you can combine qml and Widgets yes.The classes QQuickWidget and QQuickView are specifically made for that purpose.
Atm, to my knowledge, there's no official way to create embeded QWidgets into QML.
So only QML components in QWidgets
-
@J.Hilk said in How can I improving performance when I use QListView with large numbers of items:
Atm, to my knowledge, there's no official way to create embeded QWidgets into QML.
There's KDAB's DeclarativeWidgets which might be of some help.
-
@SGaist Hi SGaist
I found the Listview showed nothing few second if the layoutmode is batched.So I want to add a loading gif to make it .......Uh, you know that? Like the reflash of web and so on. But I can't find some signals or event about update, just it has a updatelater event when i used eventfiler to show all events. Could you give me a hand?
Thank you so much! -
How many items are you loading ?
-
Depending on what you are going to do with these items, you will likely need to implement things like windowing to avoid having uselessly too many items in memory.