Results 1 to 7 of 7

Thread: How to add Icons to items using QAbstractListModel in QListView

  1. #1
    Join Date
    Aug 2008
    Posts
    16
    Thanks
    3

    Question How to add Icons to items using QAbstractListModel in QListView

    Hi All:
    I don't know how to add Icons to items using QAbstractListModel in QListView ,
    Is anybody know how to ?
    Thanks for your suggestions.

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

    Default Re: How to add Icons to items using QAbstractListModel in QListView

    return the icon in QAbstractItemModel::data() for role == Qt::DecorationRole
    HTH

  3. #3
    Join Date
    Dec 2010
    Location
    Ukraine, Kharkiv
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: How to add Icons to items using QAbstractListModel in QListView

    Quote Originally Posted by caduel View Post
    return the icon in QAbstractItemModel::data() for role == Qt::DecorationRole
    HTH
    I don't understand anything you wrote.
    Can you post some example code here?

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add Icons to items using QAbstractListModel in QListView

    Hey
    You can set an icon with one of the QAbstractItem::setData overloaded methods.

    Example:

    Qt Code:
    1. item->setData(QIcon("icon.png"),Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: How to add Icons to items using QAbstractListModel in QListView

    you can see many easy examples in qt assistant....
    search for "model/view" topics

  6. #6
    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: How to add Icons to items using QAbstractListModel in QListView

    Are you able to return text from the model ?
    If yes, you must do it with Qt:isplayRole.
    Similary theres Qt:ecorationRole.. thats what caduel said

  7. #7
    Join Date
    Dec 2010
    Location
    Ukraine, Kharkiv
    Posts
    17
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: How to add Icons to items using QAbstractListModel in QListView

    Qt Code:
    1. ui.contentListView->setViewMode(QListView::IconMode);
    2. iStandardModel = new QStandardItemModel(this);
    3.  
    4. QList<QStandardItem*> *items = new QList<QStandardItem*> ();
    5.  
    6. QString qs = ui.searchLineEdit->text();
    7.  
    8. *items = getItemsToDisplay(qs);
    9.  
    10. iStandardModel->appendColumn(*items);
    11.  
    12. //Setting the icon size
    13. ui.contentListView->setIconSize(QSize(128, 128));
    14. //Setting the model
    15. ui.contentListView->setModel(iStandardModel);
    To copy to clipboard, switch view to plain text mode 

    In ui_GUIClass.h:
    Qt Code:
    1. public:
    2. QListView *contentListView;
    To copy to clipboard, switch view to plain text mode 

    In GUIClass.h:
    Qt Code:
    1. public:
    2. QStandardItemModel* iStandardModel;
    To copy to clipboard, switch view to plain text mode 

    The result is in attached sample.jpg.
    Hope, this helps.
    Attached Images Attached Images

Similar Threads

  1. QListView with icons on top of the text, not at the left
    By vfernandez in forum Qt Programming
    Replies: 7
    Last Post: 19th February 2009, 05:19
  2. how to get item's text or path from QListVIew
    By Mystical Groovy in forum Qt Programming
    Replies: 4
    Last Post: 5th September 2008, 02:25
  3. Navigate through qlistview items with shortcuts
    By jiveaxe in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:28
  4. Items in QListView should sort on Header Click
    By vinnu in forum Qt Programming
    Replies: 14
    Last Post: 10th November 2006, 13:49
  5. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 13:25

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.