[qt] - connect signal with my method

Hi all,

  I'm trying to connect signal with the method of my class. If the
method doesn't have any arguments I have not the problems. But I
couldn't find any solution to pass argument to my method.

  This is my code:

class RDPSelect < Qt::ListWidget

  slots 'action()'

  def initialize(parent = nil)
    super(parent)
  end

  def action(item)
    puts "hello"
  end
end

# gui section

app = Qt::Application.new(ARGV)

window = Qt::Widget.new()
window.resize(200, 120)

list = RDPSelect.new(window)
configs.each {|config| list.addItem(config['name'])}
Qt::Object.connect(list, SIGNAL('itemActivated(QListWidgetItem*)'),
list, SLOT('action(item)'))

window.show()
app.exec()

   When I launch it I get the message:

Object::connect: No such slot RDPSelect::action(item)

   Thanks in advance,
   Andrey

···

--
Posted via http://www.ruby-forum.com/.

The reason is that, both in the call to slots and in the call to connect, you
specify the slot in the wrong name. The correct way to specify a slot (or a
signal) is to use its signature, which is made of:
* the slot's name
* a list of the class of the slot's arguments enclosed in brackets.
In your call to slots you specify no argument, so when Qt tries to find a slot
called action which takes one argument, it fails.

The way you specify the slot in the call to connect is wrong for a similar
reason: this time, you do specify an argument for the slot, but you use the
name of the variable, instead of its class.

To make your code work, you need to make two changes:
* replace the call to slots with
  slots 'action(QListWidgetItem*)'
* replace the call to connect with
  Qt::Object.connect(list, SIGNAL('itemActivated(QListWidgetItem*)'),
list, SLOT('action(QListWidgetItem*)'))

What I stated above is completely true when speaking of Qt in C++. In ruby,
things can be a little different. In particular, in ruby, as far as signals
and slots are concerned, Qt knows nothing about methods, but only cares of
what we declare using the signals ands slots methods. This means, for example,
that we can do something like this:

class C < Qt::Objec

  slots 'slot1(QString)'

  def slot1(a, b = '', c = 3)
    ...
  end

end

Here, I declared a slot with a single argument, but defined the associated
method as taking three arguments (with the last two optional). I can make a
connection which calls slot1 with one argument, (but not with two or three,
since the slot's signature only have one argument), but I can call it by hand
with one, two or three arguments.

I hope this helps

Stefano

···

On Wednesday 17 June 2009, Andrey Demidov wrote:

>Hi all,
>
> I'm trying to connect signal with the method of my class. If the
>method doesn't have any arguments I have not the problems. But I
>couldn't find any solution to pass argument to my method.
>
> This is my code:
>
>class RDPSelect < Qt::ListWidget
>
> slots 'action()'
>
> def initialize(parent = nil)
> super(parent)
> end
>
> def action(item)
> puts "hello"
> end
>end
>
># gui section
>
>app = Qt::Application.new(ARGV)
>
>window = Qt::Widget.new()
>window.resize(200, 120)
>
>list = RDPSelect.new(window)
>configs.each {|config| list.addItem(config['name'])}
>Qt::Object.connect(list, SIGNAL('itemActivated(QListWidgetItem*)'),
>list, SLOT('action(item)'))
>
>window.show()
>app.exec()
>
> When I launch it I get the message:
>
>Object::connect: No such slot RDPSelect::action(item)
>
> Thanks in advance,
> Andrey

Thank you very much for your detailed answer.

Andrey

···

--
Posted via http://www.ruby-forum.com/.