3

I'm using QtRuby with Qt 4.8.6 and trying to create a tree view where each item has a custom icon between the tree controls and the name. The end result should be like this:
                      tree view with icons before each item

I am getting space allocated for where the icon should go, but I'm not seeing any icons. What do I have to do to get them to show up?
                      tree view with 2 columns, but no icons

Here's my code (simplified slightly to remove the no-data edge cases):

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 inspect the Qt Designer .ui file (in case the tree view needs to have a property set that I have not) it can be seen here.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • I get the same results if I replace the path to the Pixmap with a path directly to an image. If I remove the `Qt::DecorationRole` altogether the empty space where the icons are supposed to go is removed; it's ALMOST doing the right thing. – Phrogz Oct 08 '14 at 22:40
  • If you use full path to the image file, try to escape backslashes if you are on Windows. – vahancho Oct 09 '14 at 06:50
  • @vahancho Thank you for the advice, but please not from the code above that I am using forward slashes. Per comments below, the Pixmap is being found and is valid. I also experience similar results on OS X. (There as I resize the TreeView the empty gaps for the icons appear and disappear randomly.) – Phrogz Oct 09 '14 at 12:54

3 Answers3

3

Apparently the QPixmap needs to be wrapped in a QVariant to work properly. This is done in QtRuby by using Qt::Variant.fromValue():

when Qt::DecorationRole
  if index.column==0 then
    Qt::Variant.fromValue( Qt::Pixmap.new(':/path/to/resource') )

Credit to andre and peppe on #qt irc.freenode.net for helping with this.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Note that it is better to cache the pixmap or variant rather than creating a new instance each time the `data` method is called. – Phrogz Oct 09 '14 at 17:21
2

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

Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
  • Thank you for the advice. They can be found: `Qt::FileInfo.new(':/resources/images/Objects-Scene-Normal.png').exists #=> true`; the same is true if I change `:` to `.` or remove `:/`. If I edit the path to something nonsensical, the value becomes `false` as expected. – Phrogz Oct 08 '14 at 23:22
  • 1
    Then check if `QPixmap` actually loads the image file. Check `QPixmap::isNull()` or `QPixmap::size()`. Is Qt built with PNG support? – Oleg Shparber Oct 08 '14 at 23:37
  • `pic.size #=> #`; `pic.isNull #=> false`. I have used PNG images in my menus, and I am able to add this same image resource (using Qt Designer) to a button and have it show up. – Phrogz Oct 09 '14 at 02:38
0

use QTreeWidget insted of QTreeView. then create a new form class (YourTreeWidgetItem) for your new items, every thing you like, then do this:

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

but it is c++ Qt code

QCoder
  • 79
  • 1
  • 6
  • I'd prefer not do to this as (a) [I have trouble properly inserting items in a TreeWidget](http://stackoverflow.com/questions/26198356/inserting-items-in-a-qttreewidget) and (b) I chose a TreeView so that I wouldn't have to write all the logic for adding/removing items (recreating the whole model myself). – Phrogz Oct 09 '14 at 12:55