How to speed up QListView?
-
Dear all,
method 1
@QSqlQueryModel->setQuery("SELECT * FROM Table WHERE text LIKE 'text%'");
QListView->setModel(QSqlQueryModel);@method 2
@QSqlQuery sql;
sql.prepare("SELECT * FROM Table WHERE text LIKE 'text%'");
if (sql.exec()){
QStandardItem *item;
while(sql.next()){
item = new QStandardItem(sql.value(0).toString());
QStandardItemModel->appendRow(item);
}
QListView->setModel(QStandardItemModel);
}@method 1 has significant higher speed than method 2 in search...
How can I tune method 2 to speed up in search
(Remark: I use method 2 cos I need some data to add after search)Thanks
-
Thanks for your advice..
But, I can't use QSortFilterProxyModel cos it need to add all unfiltered Data to a Source Model.
Search is two parts:
first - Search words in dictionary on SQLite database file. (about 60,000+ rows per one table)
second - Search words in wikipedia via API & it will return as XMLSo, I made a ItemModel to accept these two data sources.
Adding data to ItemModel just using first search is slower than old method (QListView.setModel(QSqlQueryModel))How can I change my algorithm to speed up?
Thanks -
You can't. As I mentioned in your "initial thread":http://developer.qt.nokia.com/forums/viewthread/7760/ this approach will have a significant overhead and performance loss if your query returns a large number of rows, because you have to
- step through your complete result set once before your can view it and
- allocate memory for every single item and copy the data from the query model to the standard item model.
Your implementation does not have a runtime performance problem (the amount of resources needed to display the data), it has a serious setup performance problem (the amount of resources needed to populate your model with data).
Create your own item model which wraps a QSqlQueryModel and a QStandardItemModel. This will take an hour of work and will be better performance-wise than any optimization to your existing implementation.
-
You means that, cause of slow performance is
QListView.setModel(myModel)?when I traced with timer, upto QListView.setModel is good.
But, display the data is too slow.Can I make my model with inherit from QStandardItemModel & combine with QSqlQueryModel?
I don't understand well how to inherit from QAbstractItemModel & add QSqlQueryModel & QStandardItemModel...Plz show me some...
Thanks
-
Hi Zither,
QSqlQueryModel and QStabndardItemModel both already derive from QAbstractItemModel. QAbstractItemModel is the base class for ALL models. The problem you have is that QStanbdardItemModel is a fully generic model, thus it is not optimized for thousands of elements. It's perfect for small models.
If you need more information on how to implement custom models, I suggest you use the dev net search or look at: "Model/View Programming":http://doc.qt.nokia.com/4.7/model-view-programming.html
-
[quote author="zither" date="1310915639"]You means that, cause of slow performance is
QListView.setModel(myModel)?[/quote]
This is the cause of your problem.
@
while(sql.next()){
item = new QStandardItem(sql.value(0).toString());
QStandardItemModel->appendRow(item);
}
@
Traversing the whole result set is an expensive operation. Allocating, constructing and copying elements are expensive operations. You do all of them.I don't think that displaying the data is your problem. I did a quick performance test and I see no difference in displaying hundreds, thousands or hundreds of thousands of items.
You cannot create a class which is a QAbstractItemModel, QSqlQueryModel and QStandardItemModel. QAbstractItemModel is the base class for all models. It defindes a basic set of operations a class has to provide so it can be used by any of the views.
You should start be reading on how models work in Qt (there is plenty of documentation) and then create your own subclass of QAbstractItemModel which then can be used by the view. You will probably end up in a model or proxy model which has a QSqlQueryModel and a QStandardItemModel and does some index mapping from the "outer" indices to the "inner" indices of QSqlQueryModel and QStandardItemModel.