Question

I'm learning about Qt Model/View with Ruby and I'm trying run the following code

require 'Qt4'


class MyModel < Qt::AbstractListModel

    def initialize(data = [])
        super()
        @data = data
    end

    def rowCount idx
        @data.size
    end

    def data idx, role = Qt::DisplayRole
        if role == Qt::DisplayRole then
            Qt::Variant.new @data
        else Qt::Variant.new
        end
    end
end

if $0 == __FILE__
    app = Qt::Application.new ARGV 
    v = Qt::ListView.new
    m = MyModel.new(['1', '2', '3'])
    v.model = m
    v.show
    app.exec
end

When I run the script what it shows is a list window with three rows empty. What am I doing wrong? On the other hands, I find it hard to learn to model/view programming with ruby due to the poor documentation (All is C++) anyone know if there are tutorials or something?

Was it helpful?

Solution

Are you familiar with Qt, Ruby and/or C++ already? If so that'll help hugely on your journey as there isn't unfortunately that much documentation for Qt with Ruby available.

Anyway, the problem is that you're returning an Array instead of the element for the wanted index, see QAbstractItemModel::data. The idx argument in there is a QModelIndex, so just look up the wanted row and return it like this:

Qt::Variant.new @data[idx.row]

Also, checkout http://techbase.kde.org/Development/Languages/Ruby for information regarding to Ruby & Qt in general.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top