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:
            tree view with icons before each item

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?
            tree view with 2 columns, but no icons

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 .

+3


source to share


3 answers


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.

+1


source


I think the image cannot be found with the specified path. Confirm this with QFileInfo :: exists () .



+1


source


use the QTreeWidget built in QTreeView. then create a new form class (YourTreeWidgetItem) for your new items, whatever you like, then do this:

YourTreeWidgetItem* wItem = new YourTreeWidgetItem;
treeWidget->setItemWidget(wItem);

      

but this is C ++ Qt code

+1


source







All Articles