Results 1 to 7 of 7

Thread: adding one icon to all items in QTreeView

  1. #1
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation adding one icon to all items in QTreeView

    hey all,
    i would like to add one icon to all items in qtreeview

    i tried the following:
    (listModel is my QDirModel)

    Qt Code:
    1. int role;
    2. QIcon modelicon("/images/package.png");
    3. modelicon.operator QVariant();
    4. listModel->setData(listModel->index("/home/test/"),modelicon, role = Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int role;
    2. listModel->setData(listModel->index("/home/test/"),QVariant(QIcon("/images/package.png")), role = Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. listModel->setData(listModel->index("/home/test/"),QVariant(QIcon("/images/package.png")), Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    the program compiles and runs successfully but it doesnt add my icon in the items in QTreeView, it continues to show the default icon.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding one icon to all items in QTreeView

    i) why do you assign the role to your variable role?
    (Looks like you are confused about default values...)

    ii) check the return value of setData(); probably it is false.
    Not every model supports setting data (or setting any role).


    You can subclass QDirModel and have your subclass's data function return that icon for Qt:ecorationRole.

    HTH

  3. #3
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding one icon to all items in QTreeView

    thanks for your reply

    youre right im deeply confused im not used to the "model" system yet haha.
    can you help me with some example code on how to do this?

    thank you

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding one icon to all items in QTreeView

    Qt Code:
    1. MyModel::data ( const QModelIndex & index, int role = Qt::DisplayRole )
    2. {
    3. if(role == Qt::DecorationRole)
    4. return QIcon(...);
    5.  
    6. // Rest of the code..
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: adding one icon to all items in QTreeView

    thank you, but the problem remains,
    this is the code:

    Qt Code:
    1. int role;
    2. listModel->data ( listModel->index("/home/test"), role = Qt::DisplayRole );
    3. if(role == Qt::DecorationRole)
    4. {
    5. return QIcon("");
    6. }
    To copy to clipboard, switch view to plain text mode 

    it gives me
    " error: return-statement with a value, in function returning 'void'"

  6. #6
    Join Date
    Sep 2008
    Posts
    43
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: adding one icon to all items in QTreeView

    i got it working after all,
    problem solved

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding one icon to all items in QTreeView

    Can you share what was the mistake ? It might help others

Similar Threads

  1. Problem with application icon
    By sanjayshelke in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2009, 10:21
  2. Differentiate the icon and text in a QTreeView column
    By gmat4321 in forum Qt Programming
    Replies: 0
    Last Post: 25th February 2009, 15:38
  3. adding version to .exe with icon problem..
    By triperzonak in forum Newbie
    Replies: 1
    Last Post: 16th January 2009, 20:45
  4. QTreeView - different icon sizes on sublevels?
    By luf in forum Qt Programming
    Replies: 3
    Last Post: 18th January 2008, 15:34
  5. How to dispay an icon in the first column of QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2006, 15:51

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.