Use C ++ to implement the QML TreeViewModel (2) _ PHP Tutorial

Source: Internet
Author: User
Tags createindex
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:

 
 
  1. Class TreeNode;

  2. Typedef TreeNode * TreeNodePtr;

  3. Class TreeNode
  4. {
  5. Public:
  6. Explicit TreeNode ();
  7. TreeNodePtr parent () const;
  8. Void setParent (const TreeNodePtr p); // sets the parent node. the parent node of the root node is NULL.
  9. Void appendNode (TreeNodePtr node );
  10. Void removeNode (int row );
  11. Void setData (int role, QVariant value );
  12. QList Childs; // It is not good to save the child node pointer and put childs in public for convenience.

  13. Private:
  14. TreeNodePtr mParent = NULL; // parent node
  15. 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.

  16. };
The implementation of TreeNode is also quite simple:

 
 
  1. TreeNodePtr TreeNode::parent() const
  2. {
  3. return mParent;
  4. }

  5. void TreeNode::setParent(const TreeNodePtr p)
  6. {
  7. this->mParent = p;
  8. }

  9. void TreeNode::appendNode(TreeNodePtr node)
  10. {
  11. childs.append(node);
  12. }

  13. void TreeNode::removeNode(int row)
  14. {
  15. childs.removeAt(row);
  16. }


  17. QVariant TreeNode::data(int role)
  18. {
  19. return mRecord[role];
  20. }

  21. void TreeNode::setData(int role, QVariant value)
  22. {
  23. mRecord[role] = value;
  24. }
Next, start the SqlMenuEntryModel operation:

 
 
  1. Class SqlMenuEntry: public q1_actitemmodel, public QQmlParserStatus
  2. {
  3. Q_OBJECT
  4. Public:
  5. Explicit SqlMenuEntry (QObject * parent = 0 );
  6. ~ SqlMenuEntry ();
  7. Enum MenuEntryRoles {idRole = Qt: UserRole + 1, nameRole, defaultEntryRole, customEntryRole, iconRole, iconHoverRole };
  8. Int rowCount (const QModelIndex & parent = QModelIndex () const;
  9. Int columnCount (const QModelIndex & parent = QModelIndex () const;
  10. QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex () const;
  11. QVariant data (const QModelIndex & index, int role = Qt: DisplayRole) const;
  12. QModelIndex parent (const QModelIndex & child) const;
  13. QHash RoleNames () const;
  14. Private:
  15. QHash MRoleNames;
  16. QList<QHash<Int,QVariant>>MRecords;// QList cannot store tree data. kill
  17. QList mRootEntrys; // used to save the root node
  18. Void _ addEntryNode (TreeNodePtr node, TreeNodePtr parent = 0); // used to add nodes to the tree
  19. };
_ AddEntryNode is implemented as follows:

 
 
  1. void SqlMenuEntry::_addEntryNode(TreeNodePtr node, TreeNodePtr parent)
  2. {
  3. if(parent == NULL)
  4. {
  5. mRootEntrys.append(node);
  6. }else{
  7. node->setParent(parent);
  8. parent->appendNode(node);
  9. }
  10. }

You can add data to the model using the following code:


 
 
  1. BeginResetModel ();

  2. Auto node = new TreeNode ();
  3. Node-> setData (nameRole, "parent 1 ");
  4. Node-> setData (iconRole, "parenticon1 ");
  5. _ AddEntryNode (node); // add the first root node
  6. Auto subNode = new TreeNode ();
  7. SubNode-> setData (nameRole, "child1 ");
  8. _ AddEntryNode (subNode, node); // add a subNode

  9. 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:

 
 
  1. Int SqlMenuEntry: rowCount (const QModelIndex & parent) const
  2. {
  3. If (parent. isValid () // if parent is not empty, the number of rows to be obtained is the number of child nodes of a node.
  4. {
  5. TreeNodePtr parentNode = (TreeNodePtr) parent. internalPointer (); // The node information is stored in internalPointer of QModelIndex during index.
  6. Return parentNode-> childs. size ();
  7. }
  8. Return mRootEntrys. size (); // otherwise, the number of lines returned from the root node is

  9. }


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:

 
 
  1. QModelIndex SqlMenuEntry: index (int row, int column, const QModelIndex & parent) const
  2. {
  3. 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
  4. {
  5. If (row> = 0) & (row <mRootEntrys. size ()))
  6. {
  7. Return createIndex (row, column, mRootEntrys. at (row ));
  8. }
  9. } Else {
  10. TreeNodePtr parentNode = (TreeNodePtr) parent. internalPointer (); // return the modelIndex of the child node
  11. Return createIndex (row, column, parentNode-> childs [row]);
  12. }
  13. Return QModelIndex ();
  14. }
Parent (const QModelIndex & child) is used to return the index of the parent node of a node:

 
 
  1. QModelIndex SqlMenuEntry: parent (const QModelIndex & child) const
  2. {
  3. TreeNodePtr node = (TreeNodePtr) child. internalPointer ();
  4. If (node-> parent () = NULL)
  5. {
  6. Return QModelIndex (); // The root node does not have a parent
  7. } Else {
  8. Return createIndex (0, 1, node-> parent ());
  9. }
Implement data (constQModelIndex & index, introle) to return data in the node:
 
 
  1. QVariant SqlMenuEntry::data(const QModelIndex &index, int role) const
  2. {
  3. TreeNodePtr node = (TreeNodePtr)index.internalPointer();
  4. return node->data(role);
  5. }

After the above creation, the model can normally provide data for the TreeView:

 
 
  1. TreeView{
  2. anchors.fill: parent
  3. TableViewColumn {
  4. title: "Name"
  5. role: "name"
  6. }

  7. TableViewColumn {
  8. title: "Entry"
  9. role: "icon"
  10. }
  11. model:MenuEntryModel{}
  12. }
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.