Search code examples
qtruby

how to get model/view working for table using qtruby?


I'm trying to figure out how to use TableView with a model using qtruby. I tried to adapt the example in C++ in the tutorial given at http://doc.qt.io/qt-5/modelview.html and came up with the code shown below.

The problem is in the implementation of the data method of AbstractTableModel: only the role Qt::DisplayRole works as expected. The roles Qt::FontRole and Qt::BackgroundRole don't cause an error but don't seem to do anything either. Even worse, the roles Qt::TextAlignmentRole and Qt::CheckStateRole cause segmentation faults if enabled. Can someone tell me if I'm doing something wrong here?

#!/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

Solution

  • This is because for the DisplayRole you are creating a new Qt::Variant as expected.

    For the other return values, you should use:

    return Qt::Variant.fromValue(boldFont)
    
    return Qt::Variant.fromValue(redBackground)
    
    return Qt::Variant.fromValue(Qt::AlignRight + Qt::AlignVCenter)
    
    return Qt::Variant.fromValue(Qt::Checked)