PDA

View Full Version : expand a treeWidget



mattia
6th December 2007, 08:30
Hello, i have a problem with treeWidget.
I'd like to create a treeWidget and i want that all the treeWidget nodes areexpanded.
I have tried with this code, but it doesn.'t work.

bool main::setupTreeView ( QTreeWidget * thetwmTreeWidget )
{
QList<QTreeWidgetItem *> items;

if ( listtwmConfigPool.size() != 0 )
{
for ( int i = 0; i < myObject.size() ; i++ )
{
QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();
tempQTreeWidgetItem->setText ( 0 , myObject.getName() );
tempQTreeWidgetItem->setExpanded( true );
qDebug() << tempQTreeWidgetItem->isExpanded(); // it's false...why?
items.append ( tempQTreeWidgetItem );

for ( int i = 0; i < myObjectParent.size(); i++ )
{
QTreeWidgetItem * tempQTreeWidgetItemParent = new QTreeWidgetItem ( tempQTreeWidgetItem );
tempQTreeWidgetItemParent->setText ( 0 , myObjectParent.getName() );
items.append ( tempQTreeWidgetItemParent );
}
}

thetwmTreeWidget->setColumnCount (1);
thetwmTreeWidget->insertTopLevelItems (0, items);
}
return TRUE;
}

thanks

jpn
6th December 2007, 08:47
To me it looks like you're expanding items by the time they're added nowhere yet. Have you noticed QTreeView::expandAll()?

Edit: Oh, and I just noticed something else that's faulty too. You're adding parents and children to the same list for which you do insertTopLevelItems().

mattia
6th December 2007, 08:52
yes, when i call setExpanded the items aren't allocated into the parent yet...where should i call setExpand?

jpn
6th December 2007, 09:05
The code of yours is confusing because an item you call "parent" is actually created as child. However, this should do the trick:


bool main::setupTreeView ( QTreeWidget * thetwmTreeWidget )
{
QList<QTreeWidgetItem *> items;

if ( listtwmConfigPool.size() != 0 )
{
for ( int i = 0; i < myObject.size() ; i++ )
{
QTreeWidgetItem * tempQTreeWidgetItem = new QTreeWidgetItem ();
tempQTreeWidgetItem->setText ( 0 , myObject.getName() );
//tempQTreeWidgetItem->setExpanded( true );
//qDebug() << tempQTreeWidgetItem->isExpanded(); // it's false...why?
items.append ( tempQTreeWidgetItem );

for ( int i = 0; i < myObjectParent.size(); i++ )
{
QTreeWidgetItem * tempQTreeWidgetItemParent = new QTreeWidgetItem ( tempQTreeWidgetItem );
tempQTreeWidgetItemParent->setText ( 0 , myObjectParent.getName() );
//items.append ( tempQTreeWidgetItemParent ); // <--- get rid of this
}
}

thetwmTreeWidget->setColumnCount (1);
thetwmTreeWidget->insertTopLevelItems (0, items);
thetwmTreeWidget->expandAll(); // <--- add this
}
return TRUE;
}

mattia
6th December 2007, 13:05
ok, in this way i can expand all the items in my treeWidget but if i want to expand just some of them?
can't i set the expand propriety into the "for"? so i can control which node expand..

jpn
6th December 2007, 13:09
Yes, but then you will have to actually add the item to the tree before expanding it. Currently, you have a list of items that do not yet belong to any tree at all. You can for example pass the tree widget to QTreeWidgetItem constructor instead of collecting them to a list.

mattia
6th December 2007, 13:24
;) got it
thx so much!