Qt: Use the check box (checkbox) in treemodel + qtreeview)

Source: Internet
Author: User
Tags emit

You need to implement a function: In the TreeView of QT, you can use the check box, and select the check box of the parent node to select or cancel the check box of the child node. Here we will take the simpletreemodel project included with QT as an example to describe its usage. The simpletreemodel project path is usually in the itemviews directory of the example directory in the qt directory. For example, my path is in C:/Qt/2010.05/qt/examples/itemviews.

 

1. In the header file treemodel. h, add the header file

# Include <QList>

# Include <QPersistentModelIndex>

Then, add the setdata function and m_checkedList variable definitions in the treemodel class definition.

Public: <br/> bool setData (const QModelIndex & index, const QVariant & value, int role = Qt: EditRole); <br/> QList <QPersistentModelIndex> m_checkedList;

Specifically, setData is the parent class q1_actitemmodel of the treemodel class.
A function defined in. Its function is to respond to the mouse click node action.

M_checkedList is used to save the information of the selected node (checked in the check box ).

 

2. modify it in the treemodel. cpp file. It mainly modifies the flags () and data () Functions and implements the setData () functions.

Change the flags () function:

Qt: itemflags treemodel: Flags (const qmodelindex & Index) const <br/>{< br/> If (! Index. isvalid () <br/> return 0; <br/> If (index. column () = 0) // if it is the node of the first column, it is capable of displaying checkbox <br/> return QT: itemisenabled | QT: itemisselectable | QT:: itemisusercheckable; <br/> return QT: itemisenabled | QT: itemisselectable; <br/>}

The main modification is that when the location of the node is determined to be in the first column, the Qt
::
ItemIsUserCheckable to display checkbox

 

Then modify the data () function:

QVariant TreeModel: data (const QModelIndex & index, int role) const <br/>{< br/> if (! Index. isValid () <br/> return QVariant (); <br/> if (role = Qt: CheckStateRole & index. column () = 0) // determines whether the displayed object is checkbox and is in the first column <br/> {<br/> if (m_checkedList.contains (index )) // search in m_checkedList. If yes, the checkbox is displayed. <br/> return Qt: Checked; <br/> else <br/> return Qt: Unchecked; // if no checkbox is displayed, It is not selected <br/>}< br/> if (role! = Qt: DisplayRole) <br/> return QVariant (); <br/> TreeItem * item = static_cast <TreeItem *> (index. internalPointer (); <br/> return item-> data (index. column (); <br/>}

 

The last step is to implement the setData () function, which is relatively complicated.

Bool TreeModel: setData (const QModelIndex & index, const QVariant & value, int role) <br/>{< br/> if (role = Qt :: checkStateRole & index. column () = 0) <br/>{< br/> if (value = Qt: Unchecked) <br/>{< br/> m_checkedList.removeOne (index ); <br/> emit (dataChanged (index, index); <br/>}< br/> else if (value = Qt: Checked) <br/>{< br/> m_checkedList.append (index); <br/> emit (dataChanged (index, index )); <br/>}< br/> int childCount = rowCount (); <br/> if (childCount> 0) // determine whether a subnode exists <br/> {<br/> for (int I = 0; I <childCount; I ++) <br/> {<br/> QModelIndex child = this-> index (I, 0, index); // obtain the index of the child node <br/> setData (child, value, Qt: CheckStateRole); // recursion, set the checkbox of the subnode to the selected status <br/>}< br/>}

It mainly determines whether the checkbox operation is selected or reversed. If it is selected, add the index of the node to m_checkedList and send the dataChanged signal. Otherwise, the index of the node is deleted from m_checkedList and the dataChanged signal is sent. The dataChanged signal will trigger the corresponding slot function and call the data () function. This will reload the node status.

 

 

Reference: http://blog.csdn.net/Rex237/archive/2010/09/09/5873492.aspx for rex237 Columns

 

Also, in http://www.qtcn.org/bbs/read.php? Tid = 28120

Some people mentioned that you can use QStandardItem to implement checkbox in TreeView. For details, refer


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.