PDA

View Full Version : QTreeWidgetItem ?



allensr
22nd December 2006, 22:41
This is probably an easy question (except for me :confused:), but how do you get a QTreeWidgetItem's parents column number? I have QTreeWidget that when a user selects an item, I am building a concatenated string of all of that selected item's parent's names. I can get the original item's name that is selected from currentColumn().


...
foreach(item, selected)
{
attrName = item->text(mAttributeTreeWidget->currentColumn());
if(item->parent())
{
QTreeWidgetItem * parent = item->parent();
parentName = parent->text(??));
if(parent->parent())
{
QTreeWidgetItem * plat = parent->parent();
platName = plat->text(??);
if(plat->parent())
{
QTreeWidgetItem * run = plat->parent();
runName = run->text(??);
}
}
}
}
QString fullName = runName + "::" + platName + "::" + attrName;
...

jacek
23rd December 2006, 00:15
how do you get a QTreeWidgetItem's parents column number?
But which one? ;) Each QTreeWidgetItem consists of several columns --- it's something like a table row with several cells.

Most likely currentColumn() will give you what you want.

allensr
2nd January 2007, 20:54
But which one?

I'm trying to get the parent of the QTreeWidgetItem selected by the user. So the user may select an item in column 2 but the parent is in column 0. I can get the selected item's column number, but still am not getting the parents'. You mentioned columnCount(), which I am using, but evidently doing it wrong. For the selected item I am making the call:


mAttributeTreeWidget->currentColumn()

which is giving me the right column. But for the parent's column I am doing:



QTreeWidgetItem * parent = item->parent();
int column = parent()->treeWidget()->currentColumn());
qDebug() << "Parent's Column " << column << endl;


But am getting the same column as the selected item. I know for a fact that the parent is in column 0, but this call always returns column 1.

What am I doing wrong??

TIA.

jacek
2nd January 2007, 22:18
So the user may select an item in column 2 but the parent is in column 0.
In QTreeWidget an item represents the whole row, so there is no "item in column x", but "column x of item".


But am getting the same column as the selected item. I know for a fact that the parent is in column 0, but this call always returns column 1.
Each item has the same number of columns, so the parent has both column 0 and column 1. What data do you hold in those columns?

allensr
3rd January 2007, 18:26
OK. So perhaps I am going about this wrong. The reason for the original question is I am trying to get the text from the parent and in the API you have to specify the column. I am not sure how to do that. Do you have a suggestion on the correct way to get this information?

BTW, if I just hardcode 0 in the call I get the correct text string.



foreach(item, selected)
{
attrName = item->text(mAttributeTreeWidget->currentColumn());
if(item->parent())
{
QTreeWidgetItem * parent = item->parent();
parentName = parent->text(??));
}
}

jacek
3rd January 2007, 18:51
if I just hardcode 0 in the call I get the correct text string.
If you want the text from column 0, there's no other way than hardcode that 0 somewhere.

You can subclass QTreeWidgetItem and add a method that returns the text you want:

QString MyTreeWidgetItem::name() const
{
return text( 0 );
}
...
MyTreeWidgetItem *item = dynamic_cast< MyTreeWidgetItem * >( widgetItem );
if( item ) {
parentName = item->name();
}
Alternatively you can define an enum and name those columns:

enum SomeItemColumn {
Name = 0,
SomethingElse = 1,
AndSoOn = 2
};
...
parentName = item->text( Name );