2

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?

4

1 回答 1

1

您是否已经熟悉 Qt、Ruby 和/或 C++?如果是这样,那将对您的旅程有很大帮​​助,因为不幸的是,没有那么多关于 Qt 和 Ruby 的文档可用。

无论如何,问题是您返回的是 Array 而不是所需索引的元素,请参阅QAbstractItemModel::data。里面的 idx 参数是一个 QModelIndex,所以只需查找想要的行并像这样返回它:

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

此外,请查看http://techbase.kde.org/Development/Languages/Ruby以获取有关 Ruby 和 Qt 的一般信息。

于 2012-05-02T06:54:53.083 回答