Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
QtRuby TableWidget fill all
Andrew Lepyokhin
6/3/2008 7:57:00 PM
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?
--
Posted via
http://www.ruby-...
.
2 Answers
richard.j.dale@gmail.com
6/6/2008 5:43:00 PM
0
On Jun 3, 8:57 pm, Andrew Lepyokhin <la.bi...@gmail.com> wrote:
> 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?
> --
> Posted via
http://www.ruby-...
.
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
Andrew Lepyokhin
6/6/2008 7:32:00 PM
0
richard.j.dale@gmail.com 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!
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
QtRuby TableWidget fill all
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password