Results 1 to 3 of 3

Thread: how to use custom type items in QTreeWidget?

  1. #1
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question how to use custom type items in QTreeWidget?

    I'm trying to use custom items in QTreeWidget. e.g:
    tree_item.h Code:
    1. #include <QTreeWidgetItem>
    2. #include "unit.h" // header with any class
    3.  
    4. class tree_item : public QTreeWidgetItem
    5. {
    6. public:
    7. tree_item(unit *object);
    8.  
    9. unit *object() const;
    10. // ...
    11. };
    To copy to clipboard, switch view to plain text mode 
    tree_item.cpp Code:
    1. #include "tree_item.h"
    2.  
    3. tree_item::tree_item(unit *object)
    4. : QTreeWidgetItem(SOME_USER_TYPE)
    5. {
    6. setText(0, *(object->name()));
    7. }
    8.  
    9. // ...
    To copy to clipboard, switch view to plain text mode 

    I can add my custom item to a QTreeWidget. But when I'm trying to get it back, tree is returning item of type QTreeWidgetItem.. In short, I'm loosing data

    main.cpp Code:
    1. // ...
    2.  
    3. QTreeWidget *tree = new QTreeWidget();
    4. tree_item *item = new tree_item(new unit(10, 12));
    5. tree->addTopLevelItem(item);
    6.  
    7. // ...
    To copy to clipboard, switch view to plain text mode 

    This part of code works properly, I can see my object's name in column 0. But if I use,
    for example: tree->itemAt(some_point) of course it gives me item of type QTreeWidgetItem* not tree_item*

    Can someone give me ideas? (If possible, without inheriting QTreeWidget)

    Thanks in advance

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: how to use custom type items in QTreeWidget?

    To store custom data in QTreeWidgetItem you should use the setData function

    Qt Code:
    1. QVariant somedata;
    2. item->setData(0, Qt::UserRole + 1, somedata);
    To copy to clipboard, switch view to plain text mode 

    You can then retrieve it with

    Qt Code:
    1. item->data(0, 0, Qt::UserRole + 1);
    To copy to clipboard, switch view to plain text mode 

    Or create your items with a user type
    and after calling itemAt check the type and cast the object

    Qt Code:
    1. QTreeWidgetItem* item = new CustomItem(mTreeWidget, SOME_USER_TYPE);
    2. ...
    3. QTreeWidgetItem* item = mTreeWidget->itemAt(some_point);
    4. if(item->type() == SOME_USER_TYPE)
    5. {
    6. CustomItem* customitem = static_cast<CustomItem*>(item);
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Berryblue031 for this useful post:

    jackal (4th March 2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to use custom type items in QTreeWidget?

    I don't think the version with QVariant will work in my case, but static_cast is an excellent solution for me! Thanks a lot!

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 22:58
  2. Replies: 10
    Last Post: 30th August 2010, 20:31
  3. QVariant custom type.
    By hickscorp in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 15:23
  4. The order of items in QTreeWidget
    By KK in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 05:54
  5. Use delegate to draw different type of items
    By nifei in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2009, 14:16

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.