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. Replace QstandItemModel with QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

Replace QstandItemModel with QAbstractItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 1.9k 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.
  • L leinad

    Is this what you mean?

    ui->topTreeView->setUniformRowHeights(true);

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @leinad
    Hi
    I think more like something like
    treeWidget->header()->setStretchLastSection(true);
    or
    treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

    Christian EhrlicherC 1 Reply Last reply
    0
    • L Offline
      L Offline
      leinad
      wrote on last edited by
      #5

      @mrjj said in Replace QstandItemModel with QAbstractItemModel:

      treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

      Both did not work. It can easily be tested in the sample code provided. The thing is remove rows worked perfectly fine and fast prior to 5.11.2. I don't see how setting the header will speed up removing rows from the model. I'm guessing I really need to subclass using QAbstractItemModel. Any help in doing that would be great.

      1 Reply Last reply
      0
      • mrjjM mrjj

        @leinad
        Hi
        I think more like something like
        treeWidget->header()->setStretchLastSection(true);
        or
        treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @mrjj It's due a change in QStandardItemModel - see https://bugreports.qt.io/browse/QTBUG-78324 but I've no idea how to fix it without reverting the change. Therefore going to a custom model is (sadly) currently the way to go.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • L Offline
          L Offline
          leinad
          wrote on last edited by
          #7

          This is why I respectfully request how to do this. I'm trying using the following example: https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html but running into issues.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #8

            Your example above uses a QTreeView but the data model looks like a table - so what do you need?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #9

              I need it work with a treeview. You can see that "tv" is initialized as a treeview. Basically the code above using treeview and abstract model. After that I can figure how to replace my larger base code with this simple example. That is all I need.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @leinad said in Replace QstandItemModel with QAbstractItemModel:

                You can see that "tv" is initialized as a treeview

                It does not matter what view you are using but what model you really use - your example code clearly shows us a table structure, not a tree structure - therefore my question...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leinad
                  wrote on last edited by
                  #11

                  Its a table but I treat it like a tree which just a bunch of columns and rows like a spreadsheet. There is nothing fancy like children, grandchildren, etc.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    So you don't need to implement a tree model and can use QAbstractTableModel as base instead which is much easier.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      leinad
                      wrote on last edited by
                      #13

                      Well, all my code is already around a treeview so I would prefer to stay that course. I would have to change the GUI plus the code which seems much more invasive. This is not a small project.

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        leinad
                        wrote on last edited by
                        #14

                        I'm sorry, I misread what you are saying. I can look at using a QAbstractTableModel. Do you think the performance will be a good as it was before all of this?

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          leinad
                          wrote on last edited by
                          #15

                          I have this code fragment which I'm having issues. I'm using QAbstractTableModel as suggested.

                          topTreeViewModel = new TopTreeViewModel();

                          void TopTreeViewModel::insertRowsIntoModel(int rowNumber, QList<QList<QStandardItem *>> QStandardItemList, TopTreeViewModel *model, QModelIndex parent)
                          {
                          beginInsertRows(QModelIndex(), rowNumber, rowNumber + QStandardItemList[0].size() - 1); //QModelIndex, rowNumber, numberOfRows
                          model->insertRows(rowNumber, QStandardItemList[0].size(), parent);
                          for(int rows = 0; rows < QStandardItemList.size(); rows++)
                          {

                              for(int col = 0; col < QStandardItemList[rows].size(); col++)
                              {
                                  QModelIndex index = model->index(rowNumber + rows, col, parent);
                                  model->setData(index, QStandardItemList[rows][col]->text(), Qt::DisplayRole);
                              }
                          }
                          endInsertRows();
                          emit(dataChanged(parent, parent));
                          

                          }

                          I call it by using the following in another mainWindow:

                          metaDataModel->insertRowsIntoModel(rowNumber, QStandardItemList, metaDataModel, parent);
                          rowNumber += QStandardItemList.size();

                          I get an exception in beginInsertRows(); If I comment it out the data is correct but not displayed in the treeView.

                          The header is declared as follows (there are other methods but this is the one causing issues. Any help?
                          #ifndef TOPTREEVIEWMODEL_H
                          #define TOPTREEVIEWMODEL_H

                          #include <QObject>
                          #include <QAbstractTableModel>
                          #include <QStandardItem>
                          #include <QDebug>
                          

                          class TopTreeViewModel : public QAbstractTableModel
                          {
                          Q_OBJECT

                          public:
                          TopTreeViewModel(QObject *parent=0);
                          ~TopTreeViewModel();

                          public:

                          void insertRowsIntoModel(int rowNumber, QList<QList<QStandardItem *>> QStandardItemList, TopTreeViewModel *model, QModelIndex parent);

                          };

                          #endif // TOPTREEVIEWMODEL_H

                          JonBJ 1 Reply Last reply
                          0
                          • L leinad

                            I have this code fragment which I'm having issues. I'm using QAbstractTableModel as suggested.

                            topTreeViewModel = new TopTreeViewModel();

                            void TopTreeViewModel::insertRowsIntoModel(int rowNumber, QList<QList<QStandardItem *>> QStandardItemList, TopTreeViewModel *model, QModelIndex parent)
                            {
                            beginInsertRows(QModelIndex(), rowNumber, rowNumber + QStandardItemList[0].size() - 1); //QModelIndex, rowNumber, numberOfRows
                            model->insertRows(rowNumber, QStandardItemList[0].size(), parent);
                            for(int rows = 0; rows < QStandardItemList.size(); rows++)
                            {

                                for(int col = 0; col < QStandardItemList[rows].size(); col++)
                                {
                                    QModelIndex index = model->index(rowNumber + rows, col, parent);
                                    model->setData(index, QStandardItemList[rows][col]->text(), Qt::DisplayRole);
                                }
                            }
                            endInsertRows();
                            emit(dataChanged(parent, parent));
                            

                            }

                            I call it by using the following in another mainWindow:

                            metaDataModel->insertRowsIntoModel(rowNumber, QStandardItemList, metaDataModel, parent);
                            rowNumber += QStandardItemList.size();

                            I get an exception in beginInsertRows(); If I comment it out the data is correct but not displayed in the treeView.

                            The header is declared as follows (there are other methods but this is the one causing issues. Any help?
                            #ifndef TOPTREEVIEWMODEL_H
                            #define TOPTREEVIEWMODEL_H

                            #include <QObject>
                            #include <QAbstractTableModel>
                            #include <QStandardItem>
                            #include <QDebug>
                            

                            class TopTreeViewModel : public QAbstractTableModel
                            {
                            Q_OBJECT

                            public:
                            TopTreeViewModel(QObject *parent=0);
                            ~TopTreeViewModel();

                            public:

                            void insertRowsIntoModel(int rowNumber, QList<QList<QStandardItem *>> QStandardItemList, TopTreeViewModel *model, QModelIndex parent);

                            };

                            #endif // TOPTREEVIEWMODEL_H

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #16

                            @leinad
                            I haven't attempted to follow all your code, but:

                            beginInsertRows(QModelIndex(), rowNumber, rowNumber + QStandardItemList[0].size() - 1); //QModelIndex, rowNumber, numberOfRows
                            model->insertRows(rowNumber, QStandardItemList[0].size(), parent);
                            for(int rows = 0; rows < QStandardItemList.size(); rows++)
                            

                            This does not look right, does it? For the "I get an exception in beginInsertRows();", I think you mean in the arguments to calling that function. There must be something fundamentally wrong in counting the required rows via QStandardItemList[0].size() but indexing/looping via rows < QStandardItemList.size(). The first has [0], the second does not. I would guess the latter is correct, and QStandardItemList[0].size() is to do with column count? Or maybe I'm going mad....

                            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