qtruby and the ListViewItemIterator

N

Nigel Wilkinson

Hi folks

I'm trying qtruby out and have struck an issue I can't conquer.

I have a listview widget which is populated with listviewitems and want
to iterate through the whole lot.

The code snippet is

it = Qt::ListViewItemIterator.new(@listView)
puts it.operator++()

The "puts" is only there so I can see what's happening.

When I run the app I get the following all on one line (not 2 lines
as here):-

/mainform.ui.rb:33:in `method_missing': undefined method `operator' for
#<Qt::ListViewItemIterator:0xb6c08c24> (NoMethodError)

I'm useing the Trolltech API documentation and can't figure where I'm
messing up.

Thanks in advance
Frustrated of Heathfield (aka Nigel)
 
V

Vincent Fourmond

Nigel said:
Hi folks

I'm trying qtruby out and have struck an issue I can't conquer.

I have a listview widget which is populated with listviewitems and want
to iterate through the whole lot.

The code snippet is

it = Qt::ListViewItemIterator.new(@listView)
puts it.operator++()
/mainform.ui.rb:33:in `method_missing': undefined method `operator' for
#<Qt::ListViewItemIterator:0xb6c08c24> (NoMethodError)

Well, the operator ++ is meant to be called directly ++ when written
in C++. In ruby, however, that won't work as ++ is not an operator.
You might want to try (untested):

p it.send:)'++')

Cheers !

Vince
 
R

richard.j.dale

Vincent said:
Well, the operator ++ is meant to be called directly ++ when written
in C++. In ruby, however, that won't work as ++ is not an operator.
You might want to try (untested):

p it.send:)'++')
No, that won't work. You need to use 'it += 1' instead in QtRuby:

irb(main):002:0> lv = Qt::ListView.new
=> #<Qt::ListView:0xb79e1dac name="unnamed", x=320, y=240, width=640,
height=320>
irb(main):003:0> item1 = Qt::ListViewItem.new(lv)
=> #<Qt::ListViewItem:0xb79dbe70>
irb(main):004:0> item2 = Qt::ListViewItem.new(lv)
=> #<Qt::ListViewItem:0xb79d8180>
irb(main):005:0> item3 = Qt::ListViewItem.new(lv)
=> #<Qt::ListViewItem:0xb79d3054>
irb(main):006:0> it = Qt::ListViewItemIterator.new(lv)
=> #<Qt::ListViewItemIterator:0xb79cf620>
irb(main):007:0> it.current
=> #<Qt::ListViewItem:0xb79d3054>
irb(main):008:0> it += 1
=> #<Qt::ListViewItemIterator:0xb79cf620>
irb(main):009:0> it.current
=> #<Qt::ListViewItem:0xb79dbe70>
irb(main):010:0> it += 1
=> #<Qt::ListViewItemIterator:0xb79cf620>
irb(main):011:0> it.current
=> #<Qt::ListViewItem:0xb79d8180>
irb(main):012:0> it += 1
=> #<Qt::ListViewItemIterator:0xb79cf620>
irb(main):013:0> it.current
=> nil


-- Richard
 
R

richard.j.dale

Vincent said:
Well, the operator ++ is meant to be called directly ++ when written
in C++. In ruby, however, that won't work as ++ is not an operator.
You might want to try (untested):

p it.send:)'++')
That will nearly work, but the method is a C++ method in the Smoke
library, and so it is called 'operator++':

mardigras rdale 504% rbqt3api Qt::ListViewItemIterator |grep operator
QListViewItem* QListViewItemIterator::eek:perator*()
QListViewItemIterator& QListViewItemIterator::eek:perator++()
const QListViewItemIterator
QListViewItemIterator::eek:perator++(int)
QListViewItemIterator& QListViewItemIterator::eek:perator+=(int)
QListViewItemIterator& QListViewItemIterator::eek:perator--()
const QListViewItemIterator
QListViewItemIterator::eek:perator--(int)
QListViewItemIterator& QListViewItemIterator::eek:perator-=(int)
QListViewItemIterator& QListViewItemIterator::eek:perator=(const
QListViewItemI

Instead you need to do something like this:

p it.send("operator++".to_sym)

I've just added some each methods to various Qt3 QtRuby classes, and
made them enumerable, so you no longer need to use an external C++
style iterator with them. This is the comment from the ChangeLog:

* Made Qt::ListView, Qt::ListViewItem, Qt::BoxLayout, Qt::HBoxLayout,
Qt::VBoxLayout and Qt::GridLayout Enumerable with implementations
of each() so they don't need to use the Qt External iterators like
Qt::LayoutIterator anymore. For instance:

lv = Qt::ListView.new do
["one", "two", "three", "four"].each do |label|
Qt::ListViewItem.new(self, label, "zzz")
end
end

lv.each do |item|
p item.inspect
pp item.inspect
end

* Add inspect() and pretty_print() methods to Qt::ListViewItem so that
they show the text of their columns

The improvements will be in the next release of Qt3 QtRuby.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top