我想这样做,以便我可以将第三级(子级)扩展到顶级项目(root)下的子级。我所能做的就是让多个孩子成为一个根。
这是我的.cpp
QStringList string1, string2;
string1 << "xxxxxxxx" << "xxxxxxxxxxx";
string2 << "yyyyyy" << "yy";
m_treeWidget->insertTopLevelItem(0, new QTreeWidgetItem(string1));
m_treeWidget->insertTopLevelItem(1, new QTreeWidgetItem(string2));
//here I add a child
AddChild(m_treeWidget->topLevelItem(0),"hello","world", m_treeWidget);
//here I make two attempts to make a sub child
AddChild(m_treeWidget->itemBelow(m_treeWidget->topLevelItem(0)),"hello_sub1","world_sub1", m_treeWidget);
AddChild(m_treeWidget->itemAt(0,0),"hello_sub2","world_sub2", m_treeWidget);
以下是我的Add Child Method也在同一个.cpp文件中:
void Dialog::AddChild (QTreeWidgetItem *parent, QString name, QString Description, QTreeWidget* treeWidget)
{
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
item->setText(0,name);
item->setText(1, Description);
parent->addChild(item);
}
答案 0 :(得分:1)
为了创建树层次结构,您可以使用QTreeWidgetItem
的API,尤其是其构造函数。构造函数可以接受QTreeWidget
或QTreeWidgetItem
作为父对象。在第一种情况下,顶级项目将添加到树窗口小部件,而在第二种情况下 - 顶级项目将添加到另一项目的子项目。此API更易于使用,因为您不需要将项显式附加到树窗口小部件。以下是实现该想法的示例代码:
QStringList string1, string2;
string1 << "xxxxxxxx" << "xxxxxxxxxxx";
string2 << "yyyyyy" << "yy";
QTreeWidget tv;
// The top level items
QTreeWidgetItem *top1 = new QTreeWidgetItem(&tv, string1);
QTreeWidgetItem *top2 = new QTreeWidgetItem(&tv, string2);
// A child item.
QTreeWidgetItem *child1 =
new QTreeWidgetItem(top1, QStringList() << "Hello" << "World");
// The grandchildren.
new QTreeWidgetItem(child1, QStringList() << "Hello_sub1" << "World_sub1");
new QTreeWidgetItem(child1, QStringList() << "Hello_sub2" << "World_sub2");
答案 1 :(得分:0)
实际上我能够以另一种方式解决它......
中的.cpp:
//Initialize the QTreeWidget with 2 columns
QTreeWidget m_treeWidget = new QTreeWidget();
m_treeWidget->setColumnCount(2);
//these are the method calls:
AddRoot("Root1_Column1", "Root2_Column2", m_treeWidget);
AddRoot("Root2_Column1", "Root2_Column2", m_treeWidget);
//topLevelItem(0) makes it a child of the first root... topLevelItem(1) makes it a child of the second root
AddChild(m_treeWidget->topLevelItem(0),"Child1_Column1","Child1_Column2");
AddChild(m_treeWidget->topLevelItem(1),"Child2_Column1","Child2_Column2");
AddSubChild(m_treeWidget->itemBelow(m_treeWidget->topLevelItem(0)),"SubChild_Column1", "SubChild_Column2");
这些是我在同一个.cpp文件中使用的方法:
void Dialog::AddRoot (QString name, QString Description, QTreeWidget* treeWidget)
{
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
item->setText(0,name);
item->setText(1,Description);
item->setExpanded(true); //expand automatically
treeWidget->addTopLevelItem(item);
}
void Dialog::AddChild (QTreeWidgetItem *parent, QString name, QString Description)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0,name);
item->setText(1, Description);
parent->addChild(item);
}
void Dialog::AddSubChild (QTreeWidgetItem *parent, QString name, QString Description)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0,name);
item->setText(1, Description);
parent->addChild(item);
}