QTreeWidget 右键菜单,复选框 及其他

一、

添加节点

 1 void AddTreeItem(QTreeWidget *pTree, QTreeWidgetItem* parentItem, QString strItemTxt)
 2 {
 3     QString txt;
 4     QTreeWidgetItem *item = new QTreeWidgetItem(parentItem);
 5     item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
 6     item->setCheckState(0, Qt::Unchecked);
 7     item->setText(0, strItemTxt);
 8 //    item->setData(0, Qt::UserRole, QString::number(nType));
 9 //    item->setIcon(0, QIcon(QStringLiteral(":image/group.png")));
10     item->setWhatsThis(0, strItemTxt);
11 
12     pTree->expandItem(parentItem);
13 }
14 
15 void InitTree()
16 {
17     ui->treeWidget->header()->setHidden(1);//隐藏最顶上的表头
18     QTreeWidgetItem* topItem = new QTreeWidgetItem;
19     topItem->setFlags(topItem->flags()|Qt::ItemIsUserCheckable);
20     topItem->setCheckState(0, Qt::Unchecked);
21     topItem->setText(0, "目标列表");
22     ui->treeWidget->addTopLevelItem(topItem);
23 
24     AddTreeItem(ui->treeWidget, topItem, "飞机");
25     AddTreeItem(ui->treeWidget, topItem, "大炮");
26     AddTreeItem(ui->treeWidget, topItem, "坦克");
27 
28     connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(TreeItemChanged(QTreeWidgetItem*,int)));
29 
30 }

 

一、

详细说一下QTreeWidget右键菜单的使用。

在mainwindow中,添加变量

QTreeWidget* m_tree;

为了使用右键菜单,需要设置

m_tree->setContextMenuPolicy(Qt::CustomContextMenu);

定义槽函数:

ShowContextMenu(const QPoint&);
connect(m_tree, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowContextMenu(const QPoint&)));

在槽函数中:

1 QTreeWidgetItem *item = m_tree->itemAt(point); //可得到右键条目
2     if(item)
3     {
4         m_contextMenu->exec(QCursor::pos());//右键菜单
5     }

这里简单叙述了过程。

 二、

树结构使用复选框

参考:https://www.cnblogs.com/doubleeleven/articles/3894250.html

在树形结构中有时候需要在每项前添加复选框,能够进行勾选,复选框的添加可以使用tree本身的属性就行

 void QTreeWidgetItem::setCheckState ( int column, Qt::CheckState state )
Sets the item in the given column check state to be state.

 

添加完控件后,需要对树形结构的勾选操作做一些限定:

1、父项勾选后,子项自动全部勾选

2、子项部分勾选后,父项为部分选中状态

3、当子项全部勾选后,父项也会随之勾选

 

connect(tree,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
 1 void Count::TreeItemChanged(QTreeWidgetItem *item, int column)
 2 {
 3     QString itemText = item->text(0);
 4     if (item->checkState(0) == Qt::Checked)
 5         {
 6         QTreeWidgetItem *parent = item->parent();
 7         int cnt = item->childCount();
 8         if (cnt >0)
 9             {
10             for (int i = 0;i < cnt;i++)
11                 {
12                  item->child(i)->setCheckState(0,Qt::Checked);
13             }
14         }
15         else
16             {
17             updateParentItem(item);
18         }
19     }
20     else if (item->checkState(0) == Qt::Unchecked)
21         {
22         int cnt = item->childCount();
23         if (cnt > 0)
24             {
25             for (int i = 0;i < cnt;i++)
26                 {
27                 item->child(i)->setCheckState(0,Qt::Unchecked);
28             }
29         }
30         else
31             {
32             updateParentItem(item);
33         }
34     }
35 }
36 void Count::updateParentItem(QTreeWidgetItem *item)
37 {
38     QTreeWidgetItem *parent = item->parent();
39     if (parent == NULL)
40     {
41        return;
42     }
43     //选中的子节点个数
44     int selectedCount = 0;
45     int childCount = parent->childCount();
46     for (int i = 0; i < childCount; i++)
47     {
48        QTreeWidgetItem *childItem = parent->child(i);
49        if (childItem->checkState(0) == Qt::Checked)
50        {
51         selectedCount++;
52        }
53     }
54     if (selectedCount <= 0)
55     {
56         //未选中状态
57        parent->setCheckState(0, Qt::Unchecked);
58     }
59     else if (selectedCount > 0 && selectedCount < childCount)
60     {
61        //部分选中状态
62        parent->setCheckState(0, Qt::PartiallyChecked);
63     }
64     else if (selectedCount == childCount)
65     {
66       //选中状态
67        parent->setCheckState(0, Qt::Checked);
68     }
69 }

 

posted @ 2020-04-09 10:30  阳光下的小土豆  阅读(2365)  评论(0编辑  收藏  举报