Results 1 to 6 of 6

Thread: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

  1. #1
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    Hi,

    I have QFileSystemModel and Custom delegate which inherits from QAbstractItemDelegate and inside paint function I try to paint item icon like that

    Qt Code:
    1. // ICON
    2. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    3. r = option.rect.adjusted(2, 2, -2, -2);
    4. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 

    but it does nothing, ic.name() return empty string.
    Without setting delegate I see "standard" icons provides by QFileSystemModel so problem is in my delegate implementation.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    What do you need the QIcon for?

    Have you checked that data() returns a non-empty variant?
    And if yes, does it contain a QPixmap?

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    I added these lines inside delegate paint function

    Qt Code:
    1. QVariant var = index.data(Qt::DecorationRole);
    2. qDebug()<< "var is null?: " << var.isNull(); // false
    3. qDebug()<< "var is valid?: " << var.isValid(); // true
    4. qDebug()<< "icon is null: " << QIcon(qvariant_cast<QPixmap>(var)).isNull(); // true
    To copy to clipboard, switch view to plain text mode 

    but in listView each item has icon...
    When in paint function I add line

    Qt Code:
    1. QIcon ic = QIcon(":/Icons/light/files.png");
    To copy to clipboard, switch view to plain text mode 

    then all works great so I think that problem is in file system model which not contain properly icon under Qt:ecorationRole, it is possible?

  4. #4
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    Ok I found error

    Qt Code:
    1. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    To copy to clipboard, switch view to plain text mode 

    should be

    Qt Code:
    1. QIcon ic = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    To copy to clipboard, switch view to plain text mode 

    mistake was bad cast.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    Quote Originally Posted by Scope View Post
    mistake was bad cast.
    Make one wonder why I didn't suggest to check if the variant actually contained a QPixmap.
    Oh wait, I did.

    Cheers,
    _

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

    Scope (17th May 2016)

  7. #6
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?

    Yes, you were right.
    I solve that problem thanks to your suggestions.

Similar Threads

  1. Access delegate Property
    By beemaneni in forum Qt Quick
    Replies: 3
    Last Post: 10th February 2016, 19:08
  2. [Solved] How to access tableView from delegate?
    By selli69 in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2016, 22:58
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 20:23
  4. Replies: 0
    Last Post: 1st February 2010, 11:00
  5. Access from Delegate to Model
    By starcontrol in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2008, 12:26

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.