PDA

View Full Version : QTreeWidget



sajis997
18th April 2012, 18:39
Hello forum,

There are functions to add the top level item in the QTreeWidget. How to insert the child level items? Please check the following snippet:





int main(int argc, char *argv[])
{
QApplication app(argc,argv);

QTreeWidget *treeWidget = new QTreeWidget();

QStringList itemList;
itemList << "World";

treeWidget->insertTopLevelItem(0,new QTreeWidgetItem(itemList));

treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
for (int i = 0; i < 10; ++i)
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(1, items);

treeWidget->show();

return app.exec();
}





I want all the items added after the "World" will be under the hierarchy view of the "World" so that the child items can be collasped or expanded. But all i am getting them as list.

Basically i would like to have the "World" as the root item and rest of the items as its children.



Thanks
Sajjad

amleto
18th April 2012, 19:24
how about you read the docs? :)

http://qt-project.org/doc/qt-4.8/qtreewidgetitem.html

sajis997
25th April 2012, 00:53
Hi forum,

I have more issues over qtreewidget. In one of the functions in my project i am pulling out the invisible root item with the following function



QTreeWidgetItem *rootItem = treewidget->invisibleRootItem()


Then i send this item to other function to add widget item recursively in several functions. Apparantly, it may seem a complicated way to populate a tree, but i need to do as i am doing some other processing in those functions.

Now i created a new tree widget item with the parent item(root item in the first pass) as the parent as follows :



//create a new tree item with the tree_item as the parent
QTreeWidgetItem *new_widget_item = new QTreeWidgetItem(tree_item);
//set the text for the new tree widget item
new_widget_item->setText(0,QString(tree_string.c_str()));


I want to add the new item into the qtreewidgt and i am not sure which function to use. qtreewidget has several item like; insertTopLevelItem(...), but in that case you have to know the index value.

The issue showed up while porting an wxwidget application and they are adding the item as follows:



wxTreeItemId new_id = TreeViewTree->AppendItem( tree_id, wxString( tree_string.c_str(), wxConvUTF8 ) );


On the above snippet the treeviewtree is equivalent to the qtreewidget and tree_id is equivalent to the new_widget_item in Qt.


I am looking for some equivalent function in Qt.


Need some hint here.



Regards
Sajjad

sajis997
25th April 2012, 05:31
Hi forum,

No reply so far, i believe i am still ambiguous. Let me try again. I have pulled out the invisible root item from the tree widget as follows:



QTreeWidgetItem *rootItem = treewidget->invisibleRootItem();


Then i initialize another tree widget item root item as the parent as follows:



//create a new tree item with the tree_item as the parent
QTreeWidgetItem *new_widget_item = new QTreeWidgetItem(rootItem);
//set the text for the new tree widget item
new_widget_item->setText(0,QString(tree_string.c_str()));



And i want the new tree widget item to the tree widget. Could you suggest the function inside the qtreewidget class ?


Please let me know if i am still not clear enough

Regards
Sajjad

sajis997
25th April 2012, 20:29
Hello folks,


I am eagerly waiting for some hint to get around with this issue.


Regards
Sajjad

Spitfire
27th April 2012, 09:11
Did you try the code you've posted?
As it should be all you need to do, to see your items in the tree.

When you parent a child to the root node you don't need to add it to the tree any more.
every child is added to the tree in the order they're parented.

Consider following example:


// mainwindow.cpp constructor
QTreeWidget* tree = new QTreeWidget( this );
this->setCentralWidget( tree );

QTreeWidgetItem* root = tree->invisibleRootItem();
QTreeWidgetItem* node = NULL;
QTreeWidgetItem* leaf = NULL;

node = new QTreeWidgetItem( root );
node->setText( 0, "node 1" );
leaf = new QTreeWidgetItem( node );
leaf->setText( 0, "leaf 1" );

node = new QTreeWidgetItem( root );
node->setText( 0, "node 2" );
leaf = new QTreeWidgetItem( node );
leaf->setText( 0, "leaf 2" );


I hope this solves your problem.