PDA

View Full Version : Retrieveing data from QTreeWidgetItem



db
19th October 2007, 12:16
I have created a QTreeWidget with 2 levels with each branch and node a QTreeWidgetItem. The tree works fine, however I am having a problem understanding how to retrieve the QMap data from a node after I have set it with setData(). How is it done? I have tried several different approaches that have not been successful.

Given the code snippet below, how would I get the QMap data I set in the node??

….

QMap<QString,QString> nodeDataMap;

…..

for(Idx = 1; Idx < numTop+1; Idx++)
{
… retrieve QString data (settingValue) for node name

// create tree node under the WST and add retrieved data
QTreeWidgetItem *topItem;
topItem = new QTreeWidgetItem(mainItem);
topItem->setText(0,settingValue);

// and finally create the list of functions to process for eaach EW
for(nodeIdx = 1; nodeIdx < numNode+1; nodeIdx++)
{
… retrieve QString data (settingValue) for node name

// create tree node under the WST and add retrieved data
QTreeWidgetItem *nodeItem;
nodeItem = new QTreeWidgetItem(topItem);

nodeItem->setText(0,settingValue);
nodeItem->setFlags(nodeItem->flags() | Qt::ItemIsUserCheckable );
nodeItem->setCheckState(0,Qt::Unchecked);

… retrieve QString data for node name

// assign library name to data structure
nodeDataMap.clear();
nodeDataMap.insert("library",settingValue1);

// assign method name to data structure
nodeDataMap.insert("method",settingValue2);

// assign argument type to data structure
nodeDataMap.insert("arg1",settingValue3);
nodeDataMap.insert("arg2",settingValue4);

nodeItem->setData(0, Qt::UserRole, &nodeDataMap);

}
}

marcel
19th October 2007, 12:25
When exactly do you want to retrieve the data? At a mouse click on the item? Or as a result of some other event?

Either way, you must call QTreeWidgetItem::data(column, role).
The role must be Qt::UserRole.
This function will give you a QVariant, and you can use QVariant::toMap to retrieve the QMap.

db
19th October 2007, 13:09
Thanks for the information, I'll give it a try.

In response to your question: Actually my plan is to retrieve the data when button is clicked. That function would traverse the QTreeWidget looking for the node that is checked and then read its data.

However for a sanity check I tried to access the data after I had set it and got nowhere.

Also, I'm having a little trouble traversing my tree. Any suggestions??

marcel
19th October 2007, 13:14
Also, I'm having a little trouble traversing my tree. Any suggestions??

There are a few threads about this. You should search the forum. I answered one of them myself.
The idea is to create a recursive function that traverses the tree.

db
19th October 2007, 19:47
OK. Now I have the tree created using the above referenced code. And I figured out how to traverse it. Problem is when I try to print the data from the node I get nothing. Just the “method: “ portion.

Where did I go wrong??

Also when the lower node is “checked” what kinf of even is generated? I want to be able to do some processing when that occurs.

Thanks, again



void CTREEcu::processTree()
{

// loop through all branches and nodes of the tree and diaplay its text
for ( int i=0; i< twMain->topLevelItemCount(); i++)
{
QTreeWidgetItem *item = twMain->topLevelItem(i);

qDebug("PARENT - idx: %d text: %s",i,qPrintable(item->text(0)));

processItem(item);
}

}

void CTREEcu::processItem(QTreeWidgetItem * parent)
{

for (int i=0; i< parent->childCount(); i++)
{
QTreeWidgetItem *child = parent->child(i);
qDebug("CHILD - idx: %d text: %s",i,qPrintable(child->text(0)));
if ( child->checkState(0) == Qt::Checked )
{

qDebug("CHILD - THIS ONE IS CHECKED");
child->setCheckState(0,Qt::Unchecked);

// this piece of code can be used to set a marker for the current checked
// item so it is not erased
QModelIndex idx = twMain->currentIndex();
qDebug("internalId: %ld",idx.internalId());

// print out sample data
QMap<QString,QVariant> nodeData = child->data(0,Qt::UserRole).toMap();
qDebug("method: %s",qPrintable(nodeData.value("method").toString()));
}
processItem(child);
}
}