关闭

qt QTreeWidget使用

1184人阅读 评论(0) 收藏 举报
分类:

itemwidget.h 

#ifndef ITEMWIDGET_H
#define ITEMWIDGET_H

#include <QtWidgets/QWidget>
#include <QTreeWidget>
#include "ui_itemwidget.h"

class itemWidget : public QWidget
{
	Q_OBJECT

public:
	itemWidget(QWidget *parent = 0);
	void init();
	void updateParentItem(QTreeWidgetItem* item);
	~itemWidget();

private:
	Ui::itemWidgetClass ui;
private slots:
	void treeItemChanged(QTreeWidgetItem* item, int column);
};

#endif // ITEMWIDGET_H

itemwidget.cpp 

#include "itemwidget.h"

itemWidget::itemWidget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	init();
	connect(ui.treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(treeItemChanged(QTreeWidgetItem*, int)));
}

itemWidget::~itemWidget()
{
	;
}

void itemWidget::init()
{
	ui.treeWidget->clear();

	QTreeWidgetItem *group1 = new QTreeWidgetItem(ui.treeWidget);
	group1->setText(0, "group1");
	group1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	group1->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem11 = new QTreeWidgetItem(group1);
	subitem11->setText(0, "subitem11");
	subitem11->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem11->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem12 = new QTreeWidgetItem(group1);
	subitem12->setText(0, "subitem12");
	subitem12->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem12->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem13 = new QTreeWidgetItem(group1);
	subitem13->setText(0, "subitem13");
	subitem13->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem13->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem14 = new QTreeWidgetItem(group1);
	subitem14->setText(0, "subitem14");
	subitem14->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem14->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *group2 = new QTreeWidgetItem(ui.treeWidget);
	group2->setText(0, "group2");
	group2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	group2->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem21 = new QTreeWidgetItem(group2);
	subitem21->setText(0, "subitem21");
	subitem21->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem21->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem22 = new QTreeWidgetItem(group2);
	subitem22->setText(0, "subitem22");
	subitem22->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem22->setCheckState(0, Qt::Unchecked);

	QTreeWidgetItem *subitem23 = new QTreeWidgetItem(group2);
	subitem23->setText(0, "subitem23");
	subitem23->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	subitem23->setCheckState(0, Qt::Unchecked);

}


void itemWidget::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++)
	{
		QTreeWidgetItem *childItem = parent->child(i);
		if (childItem->checkState(0) == Qt::Checked)
		{
			selectedCount++;
		}
	}
	if (selectedCount <= 0)
	{
		parent->setCheckState(0, Qt::Unchecked);
	}
	else if(selectedCount > 0 && selectedCount < childCount)
	{
		parent->setCheckState(0, Qt::PartiallyChecked);
	}
	else if(selectedCount == childCount)
	{
		parent->setCheckState(0, Qt::Checked);
	}
}

void itemWidget::treeItemChanged(QTreeWidgetItem* item, int column)
{
	if (Qt::Checked == item->checkState(0))
	{
		int count = item->childCount();
		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);
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:888495次
    • 积分:16772
    • 等级:
    • 排名:第658名
    • 原创:671篇
    • 转载:422篇
    • 译文:3篇
    • 评论:135条
    总有一天你将破蛹而出,成长得比人们期待的还要美丽。
    但这个过程会很痛,会很辛苦,有时候还会觉得灰心。
    面对着汹涌而来的现实,觉得自己渺小无力。
    但这,也是生命的一部分。做好现在你能做的,然后,一切都会好的。
    我们都将孤独地长大,不要害怕。

    —— 寂地《踮脚张望的时光》
    博客专栏
    最新评论