Solved Replace QstandItemModel with QAbstractItemModel
-
@leinad
Hi
I think more like something like
treeWidget->header()->setStretchLastSection(true);
or
treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); -
@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.
-
@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.
-
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.
-
Your example above uses a QTreeView but the data model looks like a table - so what do you need?
-
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.
-
@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...
-
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.
-
So you don't need to implement a tree model and can use QAbstractTableModel as base instead which is much easier.
-
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.
-
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?
-
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_OBJECTpublic:
TopTreeViewModel(QObject *parent=0);
~TopTreeViewModel();public:
void insertRowsIntoModel(int rowNumber, QList<QList<QStandardItem *>> QStandardItemList, TopTreeViewModel *model, QModelIndex parent);
};
#endif // TOPTREEVIEWMODEL_H
-
@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 viaQStandardItemList[0].size()
but indexing/looping viarows < QStandardItemList.size()
. The first has[0]
, the second does not. I would guess the latter is correct, andQStandardItemList[0].size()
is to do with column count? Or maybe I'm going mad....