Nominate our 2022 Qt Champions!

How to speed up QListView?

  • Dear all,

    method 1
    @QSqlQueryModel->setQuery("SELECT * FROM Table WHERE text LIKE 'text%'");

    method 2
    @QSqlQuery sql;
    sql.prepare("SELECT * FROM Table WHERE text LIKE 'text%'");
    if (sql.exec()){
    QStandardItem *item;
    item = new QStandardItem(sql.value(0).toString());

    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)


  • What do you mean "in search"? If you want to filter your results use a QSortFilterProxyModel.

    How many results does your query return? 10+, 100+, 1000+?

  • 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 XML

    So, 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?

  • You can't. As I mentioned in your "initial thread": 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

    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...


  • 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":

  • [quote author="zither" date="1310915639"]You means that, cause of slow performance is
    This is the cause of your problem.
    item = new QStandardItem(sql.value(0).toString());
    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.

Log in to reply