QtRuby TableWidget fill all

Hello,
i’m still a ruby-newbie :slight_smile: and will be very thankful for any advise.
I’ve got a table via QTableWidget. The size is set during program
execution by setRowCount/setColumnCount. Then i need to randomly fill
all cells.
I can’t go .setText through every cell beacuse they’re still Nill,
so i’ve done it like that:

for i in (0…self.rowCount-1) do
self.setCurrentCell(i, 0)
tmp=Qt::TableWidgetItem.new
tmp.setText(rand.to_s)
self.setItem(i, 0, tmp)
end

and here’s the problem:
when table size is more than 1000x1000 my procedure is insanely slow
(because of so many .new items, i think).
So is there any good way of doing the same task without waiting for 2
minutes?

On Jun 3, 8:57 pm, Andrew L. [email protected] wrote:

tmp=Qt::TableWidgetItem.new
Posted viahttp://www.ruby-forum.com/.
I don’t think Qt::TableWidget is the best class to use for this, as
you have to populate all the cells up front. I would use a
Qt::TableView with a custom model to load the data lazyly instead like
this:

require ‘Qt4’

class RandomStringTableModel < Qt::AbstractTableModel
def initialize(parent = nil)
super(parent)
end

def rowCount(parent)
    return 1000
end

def columnCount(parent)
    return 1000
end

def data(index, role)
    if !index.valid?
        return Qt::Variant.new
    elsif role == Qt::ToolTipRole
        return Qt::Variant.new
    end

    return Qt::Variant.new(rand.to_s)
end

end

app = Qt::Application.new(ARGV)
model = RandomStringTableModel.new
table = Qt::TableView.new
table.model = model
table.windowTitle = Qt::Object.tr(“Random String View”)
table.resize(640, 480)
table.show
app.exec

The above code starts straight away and you can navigate round it
without any delays. I hope you don’t mind if I make other comments on
your code. Here are some small changes:

for i in (0…rowCount) do
setCurrentCell(i, 0)
tmp = Qt::TableWidgetItem.new
tmp.text = rand.to_s
setItem(i, 0, tmp)
end

I’ve changed the Range to use three dots and so it finishes at
rowCount - 1. You don’t need to use ‘self’ so much like you do in
python. In QtRuby you can write methods with names like ‘setFoo’ that
have one argument as ‘foo =’ in ruby…

– Richard

[email protected] wrote:

I would use a
Qt::TableView with a custom model to load the data lazyly instead
Thank you, Richard, second time :slight_smile:
I think i have to dig in Qt MVC implementation, because i didn’t even
understood ‘data(index, role)’. So long)

I hope you don’t mind if I make other comments on
your code.
Surely not!
Three-dot thing is very helpful (i even started to think of ruby’s
drawback, before you told me)
Thanks!