Displaying icon in front of text in QTreeView
I am using QtRuby with Qt 4.8.6 and am trying to create a tree view where each item has a custom icon between the tree and name controls. The end result should be as follows:
I am getting the allocated space for the icon, but I cannot see any icons. What do I need to do to make them appear?
Here's my code (slightly simplified to remove cases of no data edges):
class MyModel < Qt::AbstractItemModel
# ...
def data(index, role)
case role
when Qt::DisplayRole
case index.column
when 0; Qt::Variant.new(index.internalPointer.displayName)
when 1; Qt::Variant.new(index.internalPointer.displayType)
end
when Qt::DecorationRole
if index.column==0 then
# Just testing to show a static icon for all items
Qt::Pixmap.new(':/resources/images/Objects-Scene-Normal.png')
end
end
end
end
@mytreeview.model = MyModel.new
If you want to check the Qt Designer file .ui
(in case the treeview should have a set of properties that I don't have), it can be seen here .
source to share
Apparently QPixmap
needs to wrap in QVariant
for it to work properly. This is done in QtRuby with Qt::Variant.fromValue()
:
when Qt::DecorationRole
if index.column==0 then
Qt::Variant.fromValue( Qt::Pixmap.new(':/path/to/resource') )
Please confirm andre and peppe at #qt irc.freenode.net to help with this.
source to share
I think the image cannot be found with the specified path. Confirm this with QFileInfo :: exists () .
source to share