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. QStandardItemModel: efficient way to add a batch of items?
Forum Updated to NodeBB v4.3 + New Features

QStandardItemModel: efficient way to add a batch of items?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.0k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    Is there any efficient way to add a batch of QStandardItems to a model? The model is being cleared and then I'm adding a lot of items with setItem. QtreeWidget has addTopLevelItems ( const QList<QTreeWidgetItem *> & items ) for just this, how to optimize such addition with model / view architecture?

    The problem is that I may need to add thousands of items, and every time I do setItem a sort model is sorting the contents and view is invoked to be updated and it's taking a lot of time. If I could clear the model, disable its dataChanged() signal, add all the items and then notify about changes once - it would be much more efficient.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      I think the best way is to inherit the QAbstractItemModel and do it yourself. The QStandardItemModel is not optimized for the insertion of large chunks of data. When you subclass the model you are able to control when the view is updated, so after the big load of data.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        That's not an option. I've had to resort to QStandardItemModel after spending a week trying to make my QAbstractItemModel subclass work.

        I've been advised to append all the columns to a single QStandardItem, and then to add a bunch of items with QStandardItemModel::appendColumn(const QList<QStandardItem *> & items). What do you think?

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hmm, That doesn't sound very nice.
          It might work when you need to append a row, but what about toplevel items? When you include a new column it will be a sublevel?
          IYAM it's better to figure out what went wrong with your qabstracttableModel instead.
          The tutorial is here: "Model/View tutorial":http://qt-project.org/doc/qt-4.8/modelview.html#1-3-overview-of-the-model-view-widgets
          Maybe start by using the QAbstractTableModel instead. That's a bit easier to control at first. It seems a challenge at first, but when you get the principle of operation you will decrease code and increase flexibility.
          Otherwise start with the tutorial example of the TableView/QAbstractTableModel and go from there. Must say that the QModelIndex of a QTreeView use is a bit tricky.
          When your stuck, you may send me a mail, or post it here and we'll take a look.
          Greetz

          Greetz, Jeroen

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            Thank you!
            I've actually studied that tutorial, and even thought I understand the basics and can make the model work. But it kept crashing and behaving in a way I couldn't understand.

            Can I use QAbstractTableModel with a QTreeView? I only have top-level items, no siblings.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              worthy7
              wrote on last edited by
              #6

              Work out the number of rows you will need in advanced and set the row count before adding items.
              self.model.setRowCount(80000)

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Violet Giraffe
                wrote on last edited by
                #7

                [quote author="worthy7" date="1388191631"]Work out the number of rows you will need in advanced and set the row count before adding items.
                self.model.setRowCount(80000) [/quote]
                Thanks! If it really makes things faster - that's a great tip.

                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