2

I'm currently connecting the signal to a function like this:

 dashboard.ui.start_button_r1.connect(:clicked, dashboard.ui.start_button_r1, :handler)

where start_button_r1 is a QPushButton

Now what I want is a reference to the sending widget within handler, since I will connect this signal to several widgets. Ideally I would like my handler function to receive a emitter argument I can play around with. I could put handlerwithin a class inheriting from Qt::Object (say HandlerContainer) and call sender(), but then how do I connect the signal to HandlerContainer's inner method? I tried instance.method(:handler), but connect doesn't receive slots in that way

I usually use this approach in PyQt, but can't figure out how to do it with Ruby. I feel like I'm doing something terribly wrong since there isn't much discussion on how to get the sender from within a slot using QtRuby. How is it usually done? I have read about QSignalMapper, but that seems awfully overkill for my use case.

Juan Pablo Santos
  • 1,200
  • 2
  • 10
  • 25

1 Answers1

1

You can do it with QObject::sender() function. As you've said inside handler slot, typecast sender() to the type, you expect, QPushButton in your case, and you,ve got a reference to sender object.

In c++ it could be done like this:

// inside handler method
QPushButton *tmpBtn= dynamic_cast<QPushButton*>(sender());

Edit:

 A minimal example on how to do this in Ruby:

class SlotContainer < Qt::Object
   slots "handler()"

   def handler
    puts "called by: " + sender().to_s
   end
end

if $0 == __FILE__
    app = Qt::Application.new(ARGV) 
    ui = Ui_MainWindow.new
    container = SlotContainer.new
    window = Qt::MainWindow.new
    ui.setupUi(window)
    Qt::Object.connect(ui.pushButton, SIGNAL("clicked()"), container, SLOT("handler()"))
    window.show
    app.exec
end
Shf
  • 3,463
  • 2
  • 26
  • 42
  • Sure. That's the usual approach. However, QObject::sender() is an instance method and can't be called from my slot. I can make the handler part of a HandlerContainer object and inherit from QObject. The issue then is connecting the the signal. QtRuby expects a symbol, but what symbol should I use to reference an instance method from an object? – Juan Pablo Santos Nov 27 '13 at 16:27
  • @JuanPablo `connect(dashboard.ui.start_button_r1, SIGNAL(clicked()), reference_to_instance_of_Handler_Container, SLOT(handler());` though i don't know exact syntax of SIGNAL(clicked()) and SLOT(handler()) syntax in QtRuby – Shf Nov 28 '13 at 08:20
  • @JuanPablo `Qt::Object.connect(@dashboard.ui.start_button_r1, SIGNAL("clicked()"), @reference_to_instance_of_Handler_Container, SLOT("handler()") )` also, you may find this link usefull - https://www.ruby-forum.com/topic/192770 – Shf Nov 28 '13 at 08:27
  • I'm going to try this and report back. I just hoped I didn't need to use the string form. – Juan Pablo Santos Nov 29 '13 at 02:15
  • 1
    @JuanPablo i am not exactly expert in QtRuby, it's the first thing i found, about connect in QtRuby, if there is another convenient way - use it, but connect should be in this form - `connect (object_emmiting_signal, signal_itself, object_receiving_signal,receiving_slot_in_object)`. It can also be possible to do it this way: `object_emitting_signal.connect(signal, object_receiving_signal,slot_in_object_receiving_signal)` – Shf Nov 29 '13 at 08:20
  • Ok, this works. Only that the SlotContainer class must call the slots function to make the handler an slot. I'll edit your post with my code and award you the rep :) Thanks! – Juan Pablo Santos Dec 02 '13 at 02:25
  • @JuanPablo glad, it helped you, don't know why (makes sense to add info on how it's done in QtRuby in question associated with QtRuby), your edit was rejected not by me and i could not approve it after that, so i edited it myself as you wanted – Shf Dec 02 '13 at 07:17
  • Maybe an overzealous mod. Thanks once again. – Juan Pablo Santos Dec 02 '13 at 22:55