Results 1 to 5 of 5

Thread: how to add icons to QTreeWidget?

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default how to add icons to QTreeWidget?

    i manage to draw a treeview with QTreeWidget using QAbstractItemMode.
    i would like to ask for coding to add icons n display in QTreeWidget..

    Qt Code:
    1. nav->QTreeView::setModel(model);
    2. nav->QTreeView::setAnimated(true);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to add icons to QTreeWidget?

    Do not use an external model with QTreeWidget. QTreeWidget has a built-in model, which explains why QTreeWidget::setModel() has been made private. You are supposed to use QTreeWidgetItems to fill a QTreeWidget. QTreeWidgetItem has a method setIcon().

    If you want to use a model, use QTreeView instead (and forget about QTreeWidgetItems). Then make your model's data() return icons for Qt::DecorationRole.
    J-P Nurmi

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

    wei243 (21st September 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: how to add icons to QTreeWidget?

    If you want to use a model, use QTreeView instead (and forget about QTreeWidgetItems). Then make your model's data() return icons for Qt:ecorationRole.
    thx jpn. i change from QTreeWidget to QTreeView...
    actually i program base on the 'Simple Tree Model' which is using QTreeView provided in qt example.
    but i m new in qt4, got any example coding of it?

  5. #4
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to add icons to QTreeWidget?

    hi

    please try like this.....

    QTreeWidget Tree;
    QTreeWidgetItem *item ;
    item = new QTreeWidgetItem(Tree);
    item->setText( 0, country );
    item->setIcon(0,QIcon("your icon path or file name "));

    item->setExpanded( true );

  6. The following user says thank you to sabeesh for this useful post:

    wei243 (21st September 2007)

  7. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how to add icons to QTreeWidget?

    Qt Code:
    1. QVariant YourModel::data(const QModelIndex &index, int role) const
    2. {
    3. ...
    4. switch (role)
    5. {
    6. ...
    7. case Qt::DecorationRole: // <---
    8. return QIcon(...); // <---
    9. ...
    10. }
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QIcon Icons are not visible
    By harakiri in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2007, 19:11
  2. Suppressing menu icons on the Mac
    By Brandybuck in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2006, 19:00
  3. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32
  4. Facing problem with tool bar icons
    By jnana in forum Qt Programming
    Replies: 4
    Last Post: 20th April 2006, 08:37
  5. few questions related to QTreeWidget
    By prakash in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2006, 07:32

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.