Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to speed up QListView?

How to speed up QListView?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zither
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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+?

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zither
        wrote on last edited by
        #3

        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?
        Thanks

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zither
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved