Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How does the QML Treeview data model implement add, delete, or modify methods in C++?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
15 Posts 3 Posters 1.5k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    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.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    M 1 Reply Last reply
    0
    • SGaistS SGaist

      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.

      M Offline
      M Offline
      mirro
      wrote on last edited by
      #3

      @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;
      }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • SGaistS SGaist

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

          M Offline
          M Offline
          mirro
          wrote on last edited by mirro
          #5

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • SGaistS SGaist

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

              M Offline
              M Offline
              mirro
              wrote on last edited by mirro
              #7

              @SGaist

              There is currently no modified method. can you?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Sorry but you are really unclear...

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                1
                • SGaistS SGaist

                  Sorry but you are really unclear...

                  M Offline
                  M Offline
                  mirro
                  wrote on last edited by mirro
                  #9

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

                  B 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M mirro

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

                      B Offline
                      B Offline
                      Bob64
                      wrote on last edited by
                      #11

                      @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.

                      M 1 Reply Last reply
                      1
                      • B Bob64

                        @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.

                        M Offline
                        M Offline
                        mirro
                        wrote on last edited by
                        #12

                        @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;
                        }
                        
                        B 1 Reply Last reply
                        0
                        • M mirro

                          @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;
                          }
                          
                          B Offline
                          B Offline
                          Bob64
                          wrote on last edited by
                          #13

                          @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).

                          M 1 Reply Last reply
                          2
                          • B Bob64

                            @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).

                            M Offline
                            M Offline
                            mirro
                            wrote on last edited by
                            #14

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

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              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