6

(Rubyブロックを使用して)パラメーターを受け取るシグナルに接続する方法を知りたいです。

パラメータをとらないものに接続する方法を知っています:

myCheckbox.connect(SIGNAL :clicked) { doStuff }

ただし、これは機能しません。

myCheckbox.connect(SIGNAL :toggle) { doStuff }

トグルスロットがパラメータを取るため、機能しませんvoid QAbstractButton::toggled ( bool checked )。どうすればパラメータで動作させることができますか?

ありがとう。

4

1 に答える 1

4

あなたの質問に対する簡単な答えは、次のメソッドを使用して、接続するスロットのメソッドシグネチャを宣言する必要があるということですslots

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

渡された署名には変数名が含まれSIGNALSLOTいないことに注意してください。

また、コメントで結論付けたように、「スロット」の概念を完全に廃止し、Rubyブロックを使用してシグナルを接続し、好きなメソッドを呼び出す(またはロジックを配置する)方が簡単です(そしてRuby風になります)。列をなして)。次の構文を使用すると、メソッドを使用してメソッドや処理コードを事前に宣言する必要はありません。slots

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
于 2014-10-19T18:43:26.750 に答える