Replace QstandItemModel with QAbstractItemModel
-
@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.
-
@leinad
Hi
I think more like something like
treeWidget->header()->setStretchLastSection(true);
or
treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);@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?
-
-
@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...
-
So you don't need to implement a tree model and can use QAbstractTableModel as base instead which is much easier.
-
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
-
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....