我正在尝试找出如何在使用TableView的模型中使用qtruby。在http://doc.qt.io/qt-5/modelview.html的教程中,我尝试改编C++中的示例,并编写了如下所示的代码。
问题出在AbstractTableModel的data方法的实现中:只有角色Qt::DisplayRole才能按预期工作。角色Qt::FontRole和Qt::BackgroundRole不会导致错误,但似乎也不会做任何事情。更糟糕的是,如果启用,角色Qt::TextAlignmentRole和Qt::CheckStateRole会导致分段错误。谁能告诉我我是不是做错了什么?
#!/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发布于 2017-01-13 23:21:38
这是因为对于DisplayRole,您将按照预期创建一个新的Qt::Variant。
对于其他返回值,您应该使用:
return Qt::Variant.fromValue(boldFont)
return Qt::Variant.fromValue(redBackground)
return Qt::Variant.fromValue(Qt::AlignRight + Qt::AlignVCenter)
return Qt::Variant.fromValue(Qt::Checked)https://stackoverflow.com/questions/39025904
复制相似问题