首页 文章

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

提问于
浏览
3

我正在使用QtRuby和Qt 4.8.6并尝试创建一个树视图,其中每个项目在树控件和名称之间都有一个自定义图标 . 最终结果应该是这样的:

tree view with icons before each item

我正在为图标的位置分配空间,但我没有看到任何图标 . 我需要做些什么才能让它们出现?

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 文件(如果树视图需要具有我没有的属性集),则可以是seen here .

3 回答

  • 1

    我认为使用指定的路径找不到图像 . 使用QFileInfo::exists()验证它 .

  • 1

    使用QTreeWidget的QTreeWidget . 然后为你的新项目,你喜欢的每件事创建一个新的表单类(YourTreeWidgetItem),然后执行以下操作:

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

    但它是c Qt代码

  • 1

    显然 QPixmap 需要包装在_967459中才能正常工作 . 这是通过使用 Qt::Variant.fromValue() 在QtRuby中完成的:

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

    感谢#qt irc.freenode.net上的andre和peppe帮助解决这个问题 .

相关问题