QTreeWidget

Posted shishouths

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QTreeWidget相关的知识,希望对你有一定的参考价值。

#ifndef QTREEWIDGETHELPER_H
#define QTREEWIDGETHELPER_H

#include <QTreeWidget>
#pragma execution_character_set("utf-8")
void QTreeWidgetSetHearderLabel(QTreeWidget *source,QString value)

source->setHeaderLabel(value); //设置头的标题
source->headerItem()->setTextAlignment(0, Qt::AlignHCenter | Qt::AlignVCenter);

int treeCount(QTreeWidget *tree, QTreeWidgetItem *parent=0)

Q_ASSERT(tree != NULL);

int count = 0;
if (parent == 0)
int topCount = tree->topLevelItemCount();
for (int i = 0; i < topCount; i++)
QTreeWidgetItem *item = tree->topLevelItem(i);
if (item->isExpanded())
count += treeCount(tree, item);


count += topCount;

else
int childCount = parent->childCount();
for (int i = 0; i < childCount; i++)
QTreeWidgetItem *item = parent->child(i);
if (item->isExpanded())
count += treeCount(tree, item);


count += childCount;

return count;

void QTreeWidgetAddOneLine(QTreeWidget *source, QString value)

int counter = treeCount(source);
// QString var = QString("%1 %2").arg(counter).arg(value);
QString var = QString("%1").arg(value);
QTreeWidgetItem *imageItem2 = new QTreeWidgetItem(source, QStringList(var));

bool findedCurRow = false;
int curRow(QTreeWidget *tree, QTreeWidgetItem *curItem, QTreeWidgetItem *parent=0)

Q_ASSERT(tree != NULL);
Q_ASSERT(curItem != NULL);

int count = 0;
if (parent == 0)

int topCount = tree->topLevelItemCount();
for (int i = 0; i < topCount; i++)

if (findedCurRow)

break;

count++;
QTreeWidgetItem *item = tree->topLevelItem(i);
if (item == curItem)

break;

if (item->isExpanded())
count += curRow(tree, item, curItem);



else
int childCount = parent->childCount();
for (int i = 0; i < childCount; i++)
if (findedCurRow)

break;

count++;
QTreeWidgetItem *item = parent->child(i);
if (item == curItem)

findedCurRow = true;
break;

if (item->isExpanded())
count += curRow(tree, item, curItem);



return count;

以上是关于QTreeWidget的主要内容,如果未能解决你的问题,请参考以下文章