在 QTreeView 中的文本之前显示图标

2024-03-22

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

这是我的代码(稍微简化以消除无数据边缘情况):

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

如果您想检查 Qt Designer.ui文件(如果树视图需要设置我没有的属性)它可以是在这里看到 https://github.com/Phrogz/RUIC/blob/7bd95fcc7936e19a9b755e2da70769a02a75e123/gui/window.ui.


显然是QPixmap需要包裹在一个QVariant才能正常工作。这是在 QtRuby 中通过使用完成的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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 QTreeView 中的文本之前显示图标 的相关文章

随机推荐