Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How does the QML Treeview data model implement add, delete, or modify methods in C++?



  • Are these beginInsertRows , beginRemoveRos , emit dataChanged() methods used?

    The official example of QML Treeview does not implement these methods...
    https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

    #ifndef TREEMODEL_H
    #define TREEMODEL_H
    
    #include <QAbstractItemModel>
    #include <QModelIndex>
    #include <QVariant>
    
    class TreeItem;
    
    //! [0]
    class TreeModel : public QAbstractItemModel
    {
        Q_OBJECT
    
    public:
        explicit TreeModel(const QString &data, QObject *parent = nullptr);
        ~TreeModel();
    
        QVariant data(const QModelIndex &index, int role) const override;
        Qt::ItemFlags flags(const QModelIndex &index) const override;
        QVariant headerData(int section, Qt::Orientation orientation,
                            int role = Qt::DisplayRole) const override;
        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;
    
    private:
        void setupModelData(const QStringList &lines, TreeItem *parent);
    
        TreeItem *rootItem;
    };
    

  • Lifetime Qt Champion

    Hi,

    Yes they are. The methods overloaded in the example do not need to call them. But they will be when you remove rows from your model for example.



  • @SGaist Is there anything wrong with my add and delete operation?

    
    void TreeModel::appendChild(const QModelIndex& index)
    {
    	TreeItem* clickItem = static_cast<TreeItem*>(index.internalPointer());
    	/*beginRemoveRows(index, 0, 0);
    	clickItem->deleteAllChild();
    	endRemoveRows();*/
    	removeRows(0, clickItem->childCount(), index);
     
     
    	QList<QVariant> TestItem;
    	TestItem.append("TestItem");
    	TestItem.append("TI");
    	TreeItem* TestItem_Item = new TreeItem(TestItem, clickItem);
     
     
    	QList<QVariant> TestItem2;
    	TestItem2.append("TestItem2");
    	TestItem2.append("TI2");
    	TreeItem* TestItem_Item2 = new TreeItem(TestItem2, TestItem_Item);
     
     
    	beginInsertRows(index, 0, 0);
    	TestItem_Item->appendChild(TestItem_Item2);
    	clickItem->appendChild(TestItem_Item);
    	endInsertRows();
    }
    
    
    bool TreeModel::removeRows(int row, int count, QModelIndex parent)
    {
    	if (parent.isValid())
    	{
    		for (int r = row; r < (row + count); ++r)
    		{
    			QModelIndex idxRemove = parent.child(r, 0);
    			TreeItem* fiRemove = static_cast<TreeItem*>(idxRemove.internalPointer());
    			if (idxRemove.child(0, 0).isValid())
    			{
    				bool childRemoved = removeRows(0, fiRemove->childCount(), idxRemove);
    			}
    		}
     
     
    		TreeItem* fiParent = static_cast<TreeItem*>(parent.internalPointer());
     
     
    		int last = 0;
    		if (row + count - 1 > 0)
    		{
    			last = row + count - 1;
    		}
    		beginRemoveRows(parent, row, last);
    		fiParent->deleteAllChild();
    		endRemoveRows();
    		return true;
    	}
    	else
    	{
    		for (int r = row; r < (row + count); ++r)
    		{
    			TreeItem* slnItem = m_rootItem->child(r);
    			bool projectRemoved = removeRows(0, slnItem->childCount(), index(r, 0, QModelIndex()));
    		}
    		if ((row + count) > 0)
    		{
    			beginRemoveRows(QModelIndex(), row, row + count - 1);
    			m_rootItem->deleteAllChild();
    			endRemoveRows();
    		}
    		return true;
    	}
    	return false;
    }
    

  • Lifetime Qt Champion

    What am I supposed to look for ? What is the issue ?



  • @SGaist
    Excuse me, how to write the modification method?Can you provide some sample code?


  • Lifetime Qt Champion

    Again: what issue do you have with your current code ?



  • @SGaist

    There is currently no modified method. can you?


  • Lifetime Qt Champion

    Sorry but you are really unclear...



  • @SGaist You must know how to write the modify method , right?
    Help me write an example.


  • Lifetime Qt Champion

    As I already asked you: what is wrong with the methods you wrote ?



  • @mirro What do you mean by a 'modified method'?

    All I can think is that you want an example of using the dataChanged signal. You would use this if you are modifying the data associated with a tree node (in contrast with your add and remove methods that are concerned with structural changes to the tree) but the details of that are mainly specific to your application - e.g. what data you want to change, how the GUI presentation implemented in your use of the TreeView depends on that data, etc.

    The basic point is that if you are changing data associated with a node that the TreeView needs to know about then emit dataChanged for the node.



  • @Bob64 Is this the right way to implement the substitution?

    bool TreeModel::replaceRows(int row, int count, QModelIndex parent)
    {
    	if (parent.isValid())
    	{
    		QModelIndex idxRemove = parent.child(row, 0);
    		TreeItem* fiModify    = static_cast<TreeItem*>(idxRemove.internalPointer());
    		if (fiModify.child(0, 0).isValid())
    		{
    			beginRemoveRows(QModelIndex(),row,0);
    			removeRows(0, 0, index(row, 0, QModelIndex()));
    			endRemoveRows();
    			QList<QVariant> TestItem;
    			TestItem.append("TestItem");
    			TreeItem* TestItem_Item = new TreeItem(TestItem);
    			rootItem->replace(row,TestItem_Item);
    			dataChanged(index(row,0),index(row,0),{NameEnum});
    			return true;
    		}
    	}
    	else
    	{
    		beginRemoveRows(QModelIndex(), row, 0);
    		removeRows(row, 0, index(row, 0, QModelIndex()));
    		endRemoveRows();
    		QList<QVariant> TestItem;
    		TestItem.append("TestItem");
    		TreeItem* TestItem_Item = new TreeItem(TestItem);
    		rootItem->replace(row,TestItem_Item);
    		dataChanged(index(row,0),index(row,0),{NameEnum});
    		return true;
    	}
    	return false;
    }
    


  • @mirro Honestly, I don't know. I don't understand exactly what your method is intended to do. I can see that you are removing rows there but you aren't using the parent index. No rows are being added, so I don't know where the 'replacement' aspect comes in. And then I don't understand the intent of the following code that calls replace on your TreeItem and emits dataChanged.

    I am not an expert user, but when I implemented a tree model, I kept structural changes to the tree such as adding and removing rows completely separate from updating data at a node (which is where dataChanged is needed).



  • @Bob64 Hi, How do you replace node data? Can you sample the code.


  • Lifetime Qt Champion

    That's your model. You are supposed to know how it works therefore how you should manage the data it handles.


Log in to reply