Results 1 to 2 of 2

Thread: Return image from custom model depending on selected state

  1. #1
    Join Date
    Sep 2014
    Posts
    14
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Return image from custom model depending on selected state

    Hello folks,

    I am using Qt 4.8 and I am wondering if it possible to return an image from a custom model's QAbstractItemModel::data()-implementation based on a 'selected' row state.

    I have implemented the method data() following, where MyTreeModel is derived from QAbstractItemModel
    Qt Code:
    1. QVariant MyTreeModel::data(const QModelIndex &index, int role) const {
    2.  
    3. if (!index.isValid()) return QVariant();
    4.  
    5. // Handle some other roles...
    6.  
    7. if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
    8. // Is there any way to determine whether index.row() is selected in the view
    9. // in order to return an other image, e.g.
    10. // return IS_ROW_SELECTED(index) ? QImage(":/icons/gears_selected") : QImage(":/icons/gears_unselected");
    11. //
    12. return QImage(":/icons/gears_selected");
    13. }
    14.  
    15. return QVariant();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance and best regards,
    michael


    By playing around I finally figured out the solution - just return a QIcon instead of a QPixmap

    Qt Code:
    1. QVariant MyTreeModel::data(const QModelIndex &index, int role) const {
    2.  
    3. if (!index.isValid()) return QVariant();
    4.  
    5. // Handle some other roles...
    6.  
    7. if (role == Qt::DecorationRole && index.column() == GEARS_COLUMN) {
    8. // Simply return an icon with pixmaps for different modes, then the QTreeView will decide
    9. // which icon to render. In order to have ensured that the icon has a defined size, call QTreeView::setIconSize()
    10.  
    11. QIcon icon;
    12. icon.addPixmap(QPixmap(":/icons/gears_unselected"), QIcon::Normal);
    13. icon.addPixmap(QPixmap(":/icons/gears_selected"), QIcon::Selected);
    14. return icon;
    15. }
    16.  
    17. return QVariant();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Regards, michael
    Last edited by MrGentleman; 30th August 2017 at 06:57. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return image from custom model depending on selected state

    There are two approaches. First you can have two separate roles each returning a single image and then use a custom delegate to pick the one that should be drawn.
    The second approach is to return QIcon instead of QImage so that you can set multiple pixmaps for different conditions.

    Qt Code:
    1. QIcon icon;
    2. icon.addFile(":/icons/gears");
    3. icon.addFile(":/icons/gears_selected", QSize(), QIcon::Selected);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    MrGentleman (30th August 2017)

Similar Threads

  1. Replies: 0
    Last Post: 13th January 2015, 07:03
  2. changing state of previously selected list item
    By tyrnikeisari in forum Qt Quick
    Replies: 2
    Last Post: 26th March 2011, 06:47
  3. Replies: 0
    Last Post: 1st November 2010, 08:22
  4. Replies: 0
    Last Post: 20th September 2010, 21:34
  5. Replies: 1
    Last Post: 16th January 2008, 11:48

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.