QtRuby connects signals and slots with parameters / arguments

I would like to know how I can connect to a signal that takes parameters (using Ruby blocks).

I know how to connect to one that does not accept parameters:

myCheckbox.connect(SIGNAL :clicked) { doStuff } 

However, this does not work:

 myCheckbox.connect(SIGNAL :toggle) { doStuff } 

This does not work because the switch slot has the void QAbstractButton::toggled ( bool checked ) parameter void QAbstractButton::toggled ( bool checked ) . How can I make it work with parameters?

Thanks.

+6
source share
1 answer

The short answer to your question is that you must declare your method signature to connect the slot using the slots method:

 class MainGUI < Qt::MainWindow # Declare all the custom slots that we will connect to # Can also use Symbol for slots with no params, eg :open and :save slots 'open()', 'save()', 'tree_selected(const QModelIndex &,const QModelIndex &)' def initialize(parent=nil) super @ui = Ui_MainWin.new # Created by rbuic4 compiling a Qt Designer .ui file @ui.setupUi(self) # Create the interface elements from Qt Designer connect_menus! populate_tree! end def connect_menus! # Fully explicit connection connect @ui.actionOpen, SIGNAL('triggered()'), self, SLOT('open()') # You can omit the third parameter if it is self connect @ui.actionSave, SIGNAL('triggered()'), SLOT('save()') # close() is provided by Qt::MainWindow, so we did not need to declare it connect @ui.actionQuit, SIGNAL('triggered()'), SLOT('close()') end # Add items to my QTreeView, notify me when the selection changes def populate_tree! tree = @ui.mytree tree.model = MyModel.new(self) # Inherits from Qt::AbstractItemModel connect( tree.selectionModel, SIGNAL('currentChanged(const QModelIndex &, const QModelIndex &)'), SLOT('tree_selected(const QModelIndex &,const QModelIndex &)') ) end def tree_selected( current_index, previous_index ) # …handle the selection change… end def open # …handle file open… end def save # …handle file save… end end 

Note that signatures passed to SIGNAL and SLOT do not contain variable names.

In addition, as you concluded in your comment, it’s easier (and more Ruby-esque) to completely abandon the concept of a “slot” and simply use the Ruby block to connect the signal to call any method you like (or put inline logic). Using the following syntax, you should not use the slots method to pre-declare your method or process code.

 changed = SIGNAL('currentChanged(const QModelIndex &, const QModelIndex &)') # Call my method directly @ui.mytree.selectionMode.connect( changed, &method(:tree_selected) ) # Alternatively, just put the logic in the same spot as the connection @ui.mytree.selectionMode.connect( changed ) do |current_index, previous_index| # …handle the change here… end 
+4
source

All Articles