QtRuby connect signals and slots with parameters/arguments
Asked Answered
P

1

6

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

I know how to connect to one which does not take parameters:

myCheckbox.connect(SIGNAL :clicked) { doStuff }

However, this does not work:

myCheckbox.connect(SIGNAL :toggle) { doStuff }

It doesn't work because the toggle slot take a parameter void QAbstractButton::toggled ( bool checked ). How can I make it work with parameters?

Thanks.

Panache answered 4/12, 2012 at 0:59 Comment(4)
never tried QtRuby before, but try this maybe, see if this works: myCheckbox.connect(SIGNAL :toggle) { |checked| doStuff }Arciform
yeah thought of that, doesn't work :(Panache
try implement this one: pcapriotti.wordpress.com/2010/09/24/effective-qt-in-ruby-part-2Arciform
That's getting a bit ridiculous I think... I just decided to do checkbox.connect( :SIGNAL "toggle(bool)" ) { |x| puts x }Panache
D
4

The short answer to your question is that you must declare your method signature for the slot to connect to, 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, e.g. :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 the signatures passed to SIGNAL and SLOT do not include any variable names.

Also, as you concluded in your comment, it is simpler (and more Ruby-esque) to do away with the "slot" concept altogether and just use a Ruby block to connect the signal to invoke whatever method you like (or put the logic inline). Using the following syntax, you do not need to use the slots method to pre-declare your method or handling 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
Disinfest answered 19/10, 2014 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.