首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用qtruby让表的模型/视图工作?

如何使用qtruby让表的模型/视图工作?
EN

Stack Overflow用户
提问于 2016-08-19 03:14:59
回答 1查看 90关注 0票数 2

我正在尝试找出如何在使用TableView的模型中使用qtruby。在http://doc.qt.io/qt-5/modelview.html的教程中,我尝试改编C++中的示例,并编写了如下所示的代码。

问题出在AbstractTableModel的data方法的实现中:只有角色Qt::DisplayRole才能按预期工作。角色Qt::FontRole和Qt::BackgroundRole不会导致错误,但似乎也不会做任何事情。更糟糕的是,如果启用,角色Qt::TextAlignmentRole和Qt::CheckStateRole会导致分段错误。谁能告诉我我是不是做错了什么?

代码语言:javascript
复制
#!/usr/bin/env ruby
require 'Qt'
include Qt

class MyModel < AbstractTableModel
  def initialize(p)
    super
  end

  def rowCount(p)
    2
  end

  def columnCount(p)
    3
  end

  def data(index, role)
    row = index.row
    col = index.column

    case role
    when Qt::DisplayRole
      return Variant.new "Row#{row + 1}, Column#{col + 1}"
    when Qt::FontRole
      # this doesn't result in an error, but doesn't seem to do anything either
      if (row == 0 && col == 0)
        boldFont = Font.new
        boldFont.setBold(true)
        return boldFont
      end
    when Qt::BackgroundRole
      # this doesn't result in an error, but doesn't seem to do anything either
      if (row == 1 && col == 2)
        redBackground = Brush.new(Qt::red)
        return redBackground
      end
    when Qt::TextAlignmentRole
      # # the following causes a segmentation fault if uncommented
      # if (row == 1 && col == 1)
      #   return Qt::AlignRight + Qt::AlignVCenter
      # end
    when Qt::CheckStateRole
      # # the following causes a segmentation fault if uncommented
      # if (row == 1 && col == 0)
      #   return Qt::Checked
      # end
    end
    Variant.new
  end
end

app = Application.new ARGV
tableView = TableView.new
myModel = MyModel.new(nil)
tableView.setModel(myModel)
tableView.show
app.exec
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-13 23:21:38

这是因为对于DisplayRole,您将按照预期创建一个新的Qt::Variant。

对于其他返回值,您应该使用:

代码语言:javascript
复制
return Qt::Variant.fromValue(boldFont)

return Qt::Variant.fromValue(redBackground)

return Qt::Variant.fromValue(Qt::AlignRight + Qt::AlignVCenter)

return Qt::Variant.fromValue(Qt::Checked)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39025904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档