qTreeView with custom model not working.
-
hello every one.
i try to implement my first qtreeview with a custom model but i meet some difficulties. I took inspiration from the qt exemple but i didn't arrive to make it work. When i click on the button to add child the treeview is not updating. If someone could help me to understand this kind of stuff it would be nice. Here my code:
TreeItem.h
class TreeItem { public: explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = nullptr); explicit TreeItem(const QVector<QVariant> &data, SequenceManager* manager, TreeItem *parent = nullptr); explicit TreeItem(); ~TreeItem(); TreeItem *child(int number); int childCount() const; int columnCount() const; QVariant data(int column) const; //SequenceManager * getManager(); bool insertChildren(int position, int count, int columns); bool insertColumns(int position, int columns); TreeItem *parent(); bool removeChildren(int position, int count); bool removeColumns(int position, int columns); int childNumber() const; bool setData(int column, const QVariant &value); //bool setManager(SequenceManager * manager); private: QVector<TreeItem*> childItems; QVector<QVariant> itemData; TreeItem *parentItem; //SequenceManager * manager = nullptr; };
TreeItem.cpp
TreeItem::TreeItem(){} TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent) : itemData(data), parentItem(parent) { } TreeItem::TreeItem(const QVector<QVariant> &data, SequenceManager * manager ,TreeItem *parent) : itemData(data), parentItem(parent), manager(manager) { } TreeItem::~TreeItem() { qDeleteAll(childItems); } TreeItem *TreeItem::child(int number) { if (number < 0 || number >= childItems.size()) return nullptr; return childItems.at(number); } int TreeItem::childCount() const { return childItems.count(); } int TreeItem::childNumber() const { if (parentItem) return parentItem->childItems.indexOf(const_cast<TreeItem*>(this)); return 0; } int TreeItem::columnCount() const { return itemData.count(); } QVariant TreeItem::data(int column) const { if (column < 0 || column >= itemData.size()) return QVariant(); return itemData.at(column); } /*SequenceManager *TreeItem::getManager() { return this->manager; }*/ bool TreeItem::insertChildren(int position, int count, int columns) { if (position < 0 || position > childItems.size()) return false; for (int row = 0; row < count; ++row) { QVector<QVariant> data(columns); TreeItem *item = new TreeItem(data, this); childItems.insert(position, item); } return true; } bool TreeItem::insertColumns(int position, int columns) { if (position < 0 || position > itemData.size()) return false; for (int column = 0; column < columns; ++column) itemData.insert(position, QVariant()); for (TreeItem *child : qAsConst(childItems)) child->insertColumns(position, columns); return true; } TreeItem *TreeItem::parent() { return parentItem; } bool TreeItem::removeChildren(int position, int count) { if (position < 0 || position + count > childItems.size()) return false; for (int row = 0; row < count; ++row) delete childItems.takeAt(position); return true; } bool TreeItem::removeColumns(int position, int columns) { if (position < 0 || position + columns > itemData.size()) return false; for (int column = 0; column < columns; ++column) itemData.remove(position); for (TreeItem *child : qAsConst(childItems)) child->removeColumns(position, columns); return true; } bool TreeItem::setData(int column, const QVariant &value) { if (column < 0 || column >= itemData.size()) return false; itemData[column] = value; return true; } /*bool TreeItem::setManager(SequenceManager *manager) { this->manager = manager; return true; }*/
TreeModel.h
class TreeModel : public QAbstractItemModel { Q_OBJECT public: explicit TreeModel(QObject *parent = nullptr); // Header: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; // Basic functionality: QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; // Add data: bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; // Remove data: bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; TreeItem * getRoot(); private: TreeItem *getItem(const QModelIndex &index) const; TreeItem * rootItem; };
TreeModel.cpp
TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent) { this->rootItem = new TreeItem(); } QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const { // FIXME: Implement me! if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return QString("test"); return QVariant(); } QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const { // FIXME: Implement me! if (parent.isValid() && parent.column() != 0) return QModelIndex(); TreeItem *parentItem = getItem(parent); if (!parentItem) return QModelIndex(); TreeItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); return QModelIndex(); } QModelIndex TreeModel::parent(const QModelIndex &index) const { // FIXME: Implement me! if (!index.isValid()) return QModelIndex(); TreeItem *childItem = getItem(index); TreeItem *parentItem = childItem ? childItem->parent() : nullptr; if (parentItem == rootItem || !parentItem) return QModelIndex(); return createIndex(parentItem->childNumber(), 0, parentItem); } int TreeModel::rowCount(const QModelIndex &parent) const { if (!parent.isValid()) return 0; // FIXME: Implement me! const TreeItem *parentItem = getItem(parent); return parentItem ? parentItem->childCount() : 0; } int TreeModel::columnCount(const QModelIndex &parent) const { if (!parent.isValid()) return 0; // FIXME: Implement me! Q_UNUSED(parent); return rootItem->columnCount(); } QVariant TreeModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); /*if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();*/ TreeItem *item = getItem(index); return item->data(index.column()); // FIXME: Implement me! } bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent) { TreeItem *parentItem = getItem(parent); if (!parentItem) return false; beginInsertRows(parent, position, position + rows - 1); const bool success = parentItem->insertChildren(position, rows, rootItem->columnCount()); endInsertRows(); return success; } bool TreeModel::insertColumns(int column, int count, const QModelIndex &parent) { TreeItem *parentItem = getItem(parent); if (!parentItem) return false; beginInsertColumns(parent, column, column + count - 1); // FIXME: Implement me! const bool success = parentItem->insertColumns(column, count); endInsertColumns(); return success; } bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent) { TreeItem *parentItem = getItem(parent); if (!parentItem) return false; beginRemoveRows(parent, position, position + rows - 1); const bool success = parentItem->removeChildren(position, rows); endRemoveRows(); return success; } bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent) { beginRemoveColumns(parent, position, position + columns - 1); const bool success = rootItem->removeColumns(position, columns); endRemoveColumns(); if (rootItem->columnCount() == 0) removeRows(0, rowCount()); return success; } TreeItem *TreeModel::getRoot() { return this->rootItem; } TreeItem *TreeModel::getItem(const QModelIndex &index) const { if (index.isValid()) { TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); if (item) return item; } return this->rootItem; }
noisefiletable.h (the widget)
namespace Ui { class NoiseFileTable; } class NoiseFileTable : public QWidget { Q_OBJECT public: explicit NoiseFileTable(QWidget *parent = nullptr); ~NoiseFileTable(); private: void add(); TreeModel * model; TreeItem * root; Ui::NoiseFileTable *ui; };
noisefiletable.cpp
NoiseFileTable::NoiseFileTable(QWidget *parent) : QWidget(parent), ui(new Ui::NoiseFileTable) { ui->setupUi(this); this->model = new TreeModel(this); ui->treeView->setModel(this->model); this->root = this->model->getRoot(); ui->treeView->setHeaderHidden(true); connect(ui->pushButton, &QPushButton::clicked, this, &NoiseFileTable::add); } NoiseFileTable::~NoiseFileTable() { delete ui; } void NoiseFileTable::add(){ if(ui->lineEdit->text() != "") QString description = ui->lineEdit->text(); QString cat = ui->comboBox->currentText(); this->root->insertChildren(0,1,1); this->root->child(0)->setData(0, cat); ui->treeView->update(); }
noisefiletable.xml
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>NoiseFileTable</class> <widget class="QWidget" name="NoiseFileTable"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="3"> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>add</string> </property> </widget> </item> <item row="1" column="0" colspan="4"> <widget class="QTreeView" name="treeView"/> </item> <item row="0" column="0"> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item row="0" column="2"> <widget class="QComboBox" name="comboBox"> <item> <property name="text"> <string>Bias</string> </property> </item> <item> <property name="text"> <string>Dark</string> </property> </item> <item> <property name="text"> <string>Flat</string> </property> </item> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="lineEdit"> <property name="text"> <string>Description (opionnel)</string> </property> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
-
@superdu You say: When i click on the button to add child the treeview is not updating, In which part of the slot code do you add an element to the model?
void NoiseFileTable::add(){ if(ui->lineEdit->text() != "") QString description = ui->lineEdit->text(); QString cat = ui->comboBox->currentText(); ui->treeView->update(); }
The update() method only calls to repaint the view, nothing else.
-
sorry i missed some lines. I aslo tried:
void NoiseFileTable::add(){ if(ui->lineEdit->text() != "") QString description = ui->lineEdit->text(); QString cat = ui->comboBox->currentText(); this->root->insertChildren(0,1,1); this->root->child(0)->setData(0, cat); ui->treeView->setModel(this->model); }
but nothing.
-
@superdu If you are going to insert items then you have to use the
beginInsertRows()
andendInsertRows()
functions before and after the insert ininsertChildren()
method, respectively. Please check the official docs: https://doc.qt.io/qt-5/model-view-programming.html#inserting-and-removing-rows.No need to set the model back to the view, remove
ui->treeView->setModel(this->model);
inadd
method. -
i read the documentation and i understand the meaning of beginInsertRows() and endInsertRows(). but i dont think its the problem. indeed if you check TreeModel.cpp the headerData method return just a test element but the treeview dont even show header informations.
-
@superdu Your code has several problems like the one I already pointed out to you. The other problem that you indicate is that the columnCount (which is the same number of headers) depends on the rootItem,
rootItem->columnCount()
is 0 so the number of columns(and headers) is 0. Make thecolumnCount()
ofrootItem
at least 1 and you will see that the header will appear -
ok so i tried what you said to just return 1 at the columnCount but nothing happen. For your first comment, its already implemented in TreeModel::insertRows so i could use this method to add rows than directly add child to the root but i dont know how to use QModelIndex. What should i store in?
-
In both
rowCount()
andcolumnCount()
you have:if (!parent.isValid()) return 0;
The top level item has an invalid parent so your model will always have 0 rows and 0 columns.
P.S.
When implementing a new model, always run it through the model test it's an invaluable resource for spotting mistakes -
Thank you for your help and your tips. i deleted this if statements from this methodes but that still not working. I already tried to use QAbstractItemModelTester but that doesn't return any problems. maybe im not using it in the good way. I also tried with the three FailureReportingMode but notihng. its like if the model is not connected to the view at all but i dont understand why. i put some qdebug in the model's methods but they are no any outputs of them from the header method.
-
thank you for your advice but it's more or less what i did already ^^. the problem is the fact that for the official example they read text file which is different from what i'm looking for. But if you check the example you will see that my classes have same names and definitions of methodes are praticly the same than the official example. maybe you have other examples which can help me?
-
If you want another example you can have a look at this implementation but I fear it’s a bit complicated for your stage.
Can I ask why you decided to go down the hell of making a model instead of using a ready-made one (like QStandardItemModel)? -
yep, i tried standardmodel and that works but my items can store more things than strings so i did a class which inherits standard item class. after that i dont remember well why but i didn't arrived to do what i want with standard model because of type error. I foud a potential solution on internet but it was not elegant. So i thought about developping my own model for learn and for have the possibility to complexify the widget in the future . thank you for your example by the way