Use C ++ to implement the QML TreeViewModel (2 ). Use C ++ to implement the QML TreeViewModel (2) The implementation method of the two dimensional table model has been introduced above, and then share the implementation of the hierarchical model, first, implement a node class to save the number of trees and use C ++ to implement the QML TreeView Model (2)
The implementation method of the two dimensional table model has been introduced above. Next, we will share the implementation of the hierarchical model. First, we will implement a node class to store node data and node relationships of the tree data model:
- Class TreeNode;
- Typedef TreeNode * TreeNodePtr;
- Class TreeNode
- {
- Public:
- Explicit TreeNode ();
- TreeNodePtr parent () const;
- Void setParent (const TreeNodePtr p); // sets the parent node. the parent node of the root node is NULL.
- Void appendNode (TreeNodePtr node );
- Void removeNode (int row );
- Void setData (int role, QVariant value );
- QList Childs; // It is not good to save the child node pointer and put childs in public for convenience.
- Private:
- TreeNodePtr mParent = NULL; // parent node
- QHash MRecord; // A node can store a row of data. the key of the hash table is an integer, used to store role and QVariant to store data.
- };
The implementation of TreeNode is also quite simple:
- TreeNodePtr TreeNode::parent() const
- {
- return mParent;
- }
- void TreeNode::setParent(const TreeNodePtr p)
- {
- this->mParent = p;
- }
- void TreeNode::appendNode(TreeNodePtr node)
- {
- childs.append(node);
- }
- void TreeNode::removeNode(int row)
- {
- childs.removeAt(row);
- }
- QVariant TreeNode::data(int role)
- {
- return mRecord[role];
- }
- void TreeNode::setData(int role, QVariant value)
- {
- mRecord[role] = value;
- }
Next, start the SqlMenuEntryModel operation:
- Class SqlMenuEntry: public q1_actitemmodel, public QQmlParserStatus
- {
- Q_OBJECT
- Public:
- Explicit SqlMenuEntry (QObject * parent = 0 );
- ~ SqlMenuEntry ();
- Enum MenuEntryRoles {idRole = Qt: UserRole + 1, nameRole, defaultEntryRole, customEntryRole, iconRole, iconHoverRole };
- Int rowCount (const QModelIndex & parent = QModelIndex () const;
- Int columnCount (const QModelIndex & parent = QModelIndex () const;
- QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex () const;
- QVariant data (const QModelIndex & index, int role = Qt: DisplayRole) const;
- QModelIndex parent (const QModelIndex & child) const;
- QHash RoleNames () const;
- Private:
- QHash MRoleNames;
- QList<QHash<Int,QVariant>>MRecords;// QList cannot store tree data. kill
- QList mRootEntrys; // used to save the root node
- Void _ addEntryNode (TreeNodePtr node, TreeNodePtr parent = 0); // used to add nodes to the tree
- };
_ AddEntryNode is implemented as follows:
- void SqlMenuEntry::_addEntryNode(TreeNodePtr node, TreeNodePtr parent)
- {
- if(parent == NULL)
- {
- mRootEntrys.append(node);
- }else{
- node->setParent(parent);
- parent->appendNode(node);
- }
- }
You can add data to the model using the following code:
- BeginResetModel ();
- Auto node = new TreeNode ();
- Node-> setData (nameRole, "parent 1 ");
- Node-> setData (iconRole, "parenticon1 ");
- _ AddEntryNode (node); // add the first root node
-
- Auto subNode = new TreeNode ();
- SubNode-> setData (nameRole, "child1 ");
- _ AddEntryNode (subNode, node); // add a subNode
- EndResetModel ();
The above code adds a child node child1 of the root node parent1 and parent1 to the model. Next, we can continuously improve the SqlMenuEntryModel so that the TreeView can smoothly display the correct data and data relationship. First, change int rowCount (const QModelIndex & parent) so that the model can correctly return the number of root nodes and the number of child nodes:
- Int SqlMenuEntry: rowCount (const QModelIndex & parent) const
- {
- If (parent. isValid () // if parent is not empty, the number of rows to be obtained is the number of child nodes of a node.
- {
- TreeNodePtr parentNode = (TreeNodePtr) parent. internalPointer (); // The node information is stored in internalPointer of QModelIndex during index.
- Return parentNode-> childs. size ();
- }
- Return mRootEntrys. size (); // otherwise, the number of lines returned from the root node is
- }
Implement the index interface function so that the model can return the modelIndex of the subnode and save the corresponding node pointer in internalPointer of modelIndex:
- QModelIndex SqlMenuEntry: index (int row, int column, const QModelIndex & parent) const
- {
- If (! Parent. isValid () // if parent is null, the modelIndex of the root node is returned. at the same time, the node data pointer (TreeNodePtr) is saved in internalPointer of QModelIndex, to obtain node data in other functions
- {
- If (row> = 0) & (row <mRootEntrys. size ()))
- {
- Return createIndex (row, column, mRootEntrys. at (row ));
- }
- } Else {
- TreeNodePtr parentNode = (TreeNodePtr) parent. internalPointer (); // return the modelIndex of the child node
- Return createIndex (row, column, parentNode-> childs [row]);
- }
- Return QModelIndex ();
- }
Parent (const QModelIndex & child) is used to return the index of the parent node of a node:
- QModelIndex SqlMenuEntry: parent (const QModelIndex & child) const
- {
- TreeNodePtr node = (TreeNodePtr) child. internalPointer ();
- If (node-> parent () = NULL)
- {
- Return QModelIndex (); // The root node does not have a parent
- } Else {
- Return createIndex (0, 1, node-> parent ());
- }
Implement data (constQModelIndex & index, introle) to return data in the node:
- QVariant SqlMenuEntry::data(const QModelIndex &index, int role) const
- {
- TreeNodePtr node = (TreeNodePtr)index.internalPointer();
- return node->data(role);
- }
After the above creation, the model can normally provide data for the TreeView:
- TreeView{
- anchors.fill: parent
- TableViewColumn {
- title: "Name"
- role: "name"
- }
- TableViewColumn {
- title: "Entry"
- role: "icon"
- }
- model:MenuEntryModel{}
- }
Continue to work. we will continue to add, delete, and update data tomorrow.
The implementation method of the two dimensional Model (II) has been introduced above. then, we will share the implementation of the hierarchical Model. First, we will implement a node class to save the number of trees...