Qt: Create a tree control with a check box

Source: Internet
Author: User

A tree control in Qt is called a qtreewidget, whereas a tree node in a control is called a qtreewidgetitem.

For more detailed knowledge points about the Qtreewidget controls and Qtreewidgetitem, you can view the official Help documentation for QT.


Qtreewidget class:



Qtreewidgetitem class:



The functions implemented:

    • Creates a tree control, when the top-level tree node is selected, all of the child nodes are checked.

    • When the middle molecular node is selected, the top-level tree node is grayed out. (Partially selected)

    • When the child nodes are all selected, the top-level node becomes selected.

Specific steps:

1. New QT GUI application, project name custom, base class Select Qwidget, select the Create Interface check box.

2. Double-click the "Widget.ui" file to open the Qt interface designer and drag out a copy of the Qtreewidget control.

2. Add the appropriate header file to the header file "Widget.h"

#include <QTreeWidgetItem>

Add the following code to the Widget's class declaration:

Public://Declaration of initialization function    void init ();    void Updateparentitem (qtreewidgetitem* item);p ublic Slots:   //Declare signal with slot to execute void treeitemchanged when sub-options of the tree control are changed    ( qtreewidgetitem* item, int column);


4. Add the following code in the Class widget constructor in the source file "Widget.cpp":

Init (); Connect (ui->treewidget,signal (itemchanged (Qtreewidgetitem*,int)), This,slot (Treeitemchanged (QTreeWidgetItem* , int)));



5. Implement each function in the source file "Widget.cpp":

void Widget::init ()

void Widget::init () {ui->treewidget->clear ();    Initialize a tree control//define the first Tree group qtreewidgetitem* group1 = new Qtreewidgetitem (ui->treewidget);    Group1->settext (0, "group1"); The text information displayed by the tree control Group1->setflags (qt::itemisusercheckable | qt::itemisenabled |   qt::itemisselectable); Sets the properties of the tree control subkey group1->setcheckstate (0,qt::unchecked);    The initial state is not selected//first set of subkeys qtreewidgetitem* SUBITEM11 = new Qtreewidgetitem (group1); Subitem11->setflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);  Subitem11->settext (0, "SubItem11"); Sets the text displayed by the subkey subitem11->setcheckstate (0,qt::checked);    Set the display format and status of sub-options qtreewidgetitem* subItem12 = new Qtreewidgetitem (group1); Subitem12->setflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Subitem12->settext (0, "subItem12");    Subitem12->setcheckstate (0,qt::unchecked);    qtreewidgetitem* subItem13 = new Qtreewidgetitem (group1); Subitem13->setflags (Qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Subitem13->settext (0, "subItem13");    Subitem13->setcheckstate (0,qt::unchecked);    Define a second tree group qtreewidgetitem* group2 = new Qtreewidgetitem (ui->treewidget);    Group2->settext (0, "group2"); Group2->setflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Group2->setcheckstate (0,qt::unchecked);   The second set of subkeys qtreewidgetitem* SubItem21 = new Qtreewidgetitem (group2); Specifies which parent the subkey belongs to Subitem21->setflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Subitem21->settext (0, "subItem21");    Subitem21->setcheckstate (0,qt::unchecked);    qtreewidgetitem* subItem22 = new Qtreewidgetitem (group2); Subitem22->setflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Subitem22->settext (0, "SubItem22");    Subitem22->setcheckstate (0,qt::unchecked);    qtreewidgetitem* subItem23 = new Qtreewidgetitem (group2); Subitem23->seTflags (qt::itemisusercheckable | qt::itemisenabled |    qt::itemisselectable);    Subitem23->settext (0, "subItem23"); Subitem23->setcheckstate (0,qt::unchecked);}


void Widget::treeitemchanged (qtreewidgetitem* item, int column)

void Widget::treeitemchanged (qtreewidgetitem* item, int column) {    //qstring itemtext = item->text (0);    if (qt::checked = = item->checkstate (0))    {       //qtreewidgetitem* parent = Item->parent ();        int count = Item->childcount (); Returns the number of subkeys        if (count >0)        {for            (int i=0; i<count; i++)            {                item->child (i) Setcheckstate (0,qt::checked);            }        }        else        {            Updateparentitem (item);        }     }    else if (qt::unchecked = = item->checkstate (0))    {        int count = Item->childcount ();        if (Count > 0)        {for            (int i=0; i<count; i++)            {                item->child (i)->setcheckstate (0,QT:: unchecked);            }        }        else        {            Updateparentitem (item);        }    }}


void Widget::updateparentitem (qtreewidgetitem* item)

void Widget::updateparentitem (qtreewidgetitem* item) {    Qtreewidgetitem *parent = Item->parent ();    if (parent = = NULL)    {        return;    }    int selectedcount = 0;    int childCount = Parent->childcount ();    for (int i=0; i<childcount; i++)//Determine how many sub-entries are selected    {        qtreewidgetitem* ChildItem = Parent->child (i);        if (childitem->checkstate (0) = = qt::checked)        {            selectedcount++;        }    }    if (selectedcount <= 0)  //If no subkey is selected, the parent is set to unchecked state    {        parent->setcheckstate (0,qt::unchecked);    }    else if (selectedcount>0 && selectedcount<childcount)    //If some of the subkeys are selected, the parent is set to a partially selected state, which is shown in gray    {        parent->setcheckstate (0,qt::P artiallychecked);    }    else if (Selectedcount = = ChildCount)    //If the subkey is all selected, the parent is set to the selected state    {        parent->setcheckstate (0,QT:: Checked);}    }

6. So far, all the code for this program has been written. Click the Run button,

The results of the operation are as follows: (I clicked the mouse)



Small Knowledge Points:

QT provides a rich library of functions that you want to remember all, not the average person can do. (Even if you write it, you don't have to remember it all.)

All this time, the QT Assistant is on the blink.

For example, when we do not know the function setcheckstate () the specific function or usage, only need to position the mouse on the function, and then press the mouse F1 key, the QT creater will pop up about this function help information.


Qt: Create a tree control with a check box

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.