PDA

View Full Version : trying to access treewidget indexFromItem



gamenovice
6th May 2012, 05:37
Hello all,

I have been trying to make use of the QTreeWidget's indexFromItem to make use to access an actual index on a QStringList (for file writing purposes).

I can't show code b/c this is an offsite computer, but I need help.

when I try to do something like this:



void on_treeWidget_itemChanged(QTreeItem *item, int column)
{

int Index = ui->treeWidget->indexFromItem(item->parent(),0).row();
}


i get this error message saying indexFromItem is a protected member.

Ive tried direct input, such as:


QStringList list;

list.removeAt(ui->treeWidget->indexFromItem(item->parent(),0).row());

or even a failed attempt at getting and setting

I'm pressed for time, but I was hoping that somebody had the answers to this dilemma.

Thanks in advance,
gamenovice

ps. If I am not being clear, let me know

ChrisW67
6th May 2012, 08:23
This is a C++ understanding issue. Protected member functions are only available to the class that contains them or classes that inherit from that class. They behave as private to all others attempting access.

You are trying to determine which child of the changed item's grandparent is the changed item's parent. Something like:


int index = -1; // means not found
QTreeWidgetItem *parent = item->parent();
if (parent) {
QTreeWidgetItem *grandparent = parent->parent();
if (grandparent)
index = grandparent->indexOfChild(parent);
}

gamenovice
6th May 2012, 17:47
this does work, thanks,

but i seem to have noticed another problem:

if i were to have 2 item entries in my tree widget:




bob
id
full name
last name
email
phone

sally
id
full name
last name
email
phone


now if i were to edit sally phone number in the treewidget, then the next time i open up the widget, i see that bobs phone number and sally's phone number have swapped around.






bob
id
full name
last name
email
sally's phone

sally
id
full name
last name
email
bob's phone


I know that I was trying to get the index of the parent for the widget, but is there any way to make the ordering and index of the parent itself consistent, so that this doesn't happen?

thanks,
gamenovice

Added after 6 minutes:

another wierd problem that show's up is that i cannot seem to set the text of the parent. if i try qt crashes. so i have to implement my widget like this:


*blank*
id
name
etc.

any ideas what's going on there?

ChrisW67
7th May 2012, 01:51
The QTreeWidget does not reorder items in the tree magically. This must be something you are doing in your code's logic. Since we cannot see your code you will have to find this yourself.

You cannot set the text of the parent item if there is no parent. Check for parent() returning null (which you will notice I was doing in the example I gave you).

gamenovice
7th May 2012, 02:55
thanks for the advice,

I actually found a pretty obvious solution.

Make a grandfather node like specified when looking for the index(earlier in thread) and restructure the tree accordingly. It seems much more manageable that way as well.