# QtRuby TableWidget fill all

**URL:** <https://www.ruby-forum.com/t/qtruby-tablewidget-fill-all/139435>\
**Category:** Ruby\
**Created:** [June 3, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/qtruby-tablewidget-fill-all/139435 "2008-06-03T21:57:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [June 3, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/qtruby-tablewidget-fill-all/139435/1 "2008-06-03T21:57:58Z")

</div>

Hello,  
i’m still a ruby-newbie 🙂 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?

---

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [June 6, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/qtruby-tablewidget-fill-all/139435/2 "2008-06-06T19:50:15Z")

</div>

On Jun 3, 8:57 pm, Andrew L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![bizzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bizzz](https://www.ruby-forum.com/u/bizzz)\
**Post date:** [June 6, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/qtruby-tablewidget-fill-all/139435/3 "2008-06-06T21:33:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I would use a  
> Qt::TableView with a custom model to load the data lazyly instead  
> Thank you, Richard, second time 🙂  
> 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!
