2

qtrubyを使用したモデルでTableViewを使用する方法を理解しようとしています。http://doc.qt.io/qt-5/modelview.htmlにあるチュートリアルの C++ の例を適応させようとしたところ、 以下に示すコードが思いつきました。

問題は、AbstractTableModel のデータ メソッドの実装にあります。ロール 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
4

1 に答える 1