Results 1 to 5 of 5

Thread: Retrieveing data from QTreeWidgetItem

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Retrieveing data from QTreeWidgetItem

    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);

    }
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieveing data from QTreeWidgetItem

    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.

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieveing data from QTreeWidgetItem

    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??

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieveing data from QTreeWidgetItem

    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.

  5. The following user says thank you to marcel for this useful post:

    db (19th October 2007)

  6. #5
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieveing data from QTreeWidgetItem

    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

    Qt Code:
    1. void CTREEcu::processTree()
    2. {
    3.  
    4. // loop through all branches and nodes of the tree and diaplay its text
    5. for ( int i=0; i< twMain->topLevelItemCount(); i++)
    6. {
    7. QTreeWidgetItem *item = twMain->topLevelItem(i);
    8.  
    9. qDebug("PARENT - idx: %d text: %s",i,qPrintable(item->text(0)));
    10.  
    11. processItem(item);
    12. }
    13.  
    14. }
    15.  
    16. void CTREEcu::processItem(QTreeWidgetItem * parent)
    17. {
    18.  
    19. for (int i=0; i< parent->childCount(); i++)
    20. {
    21. QTreeWidgetItem *child = parent->child(i);
    22. qDebug("CHILD - idx: %d text: %s",i,qPrintable(child->text(0)));
    23. if ( child->checkState(0) == Qt::Checked )
    24. {
    25.  
    26. qDebug("CHILD - THIS ONE IS CHECKED");
    27. child->setCheckState(0,Qt::Unchecked);
    28.  
    29. // this piece of code can be used to set a marker for the current checked
    30. // item so it is not erased
    31. QModelIndex idx = twMain->currentIndex();
    32. qDebug("internalId: %ld",idx.internalId());
    33.  
    34. // print out sample data
    35. QMap<QString,QVariant> nodeData = child->data(0,Qt::UserRole).toMap();
    36. qDebug("method: %s",qPrintable(nodeData.value("method").toString()));
    37. }
    38. processItem(child);
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 19th October 2007 at 20:54. Reason: missing [code] tags

Similar Threads

  1. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 12:14
  2. How to store/get pointer on QTreeWidgetItem data?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2007, 22:26
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.