QtRuby使用参数/参数连接信号和插槽

Jea*_*Luc 6 ruby qt qt4 qtruby

我想知道如何连接到带参数的信号(使用Ruby块).

我知道如何连接到不带参数的那个:

myCheckbox.connect(SIGNAL :clicked) { doStuff }
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用:

myCheckbox.connect(SIGNAL :toggle) { doStuff }
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为切换槽采用参数void QAbstractButton::toggled ( bool checked ).如何使其与参数一起使用?

谢谢.

Phr*_*ogz 4

对您问题的简短回答是,您必须使用以下方法声明要连接的插槽的方法签名slots

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

请注意,传递给 的签名SIGNALSLOT包含任何变量名称。

\n\n

另外,正如您在评论中总结的那样,完全消除“槽”概念并仅使用 Ruby 块连接信号来调用您喜欢的任何方法(或者将逻辑排队)。使用以下语法,您不需要使用slots方法来预先声明您的方法或处理代码。

\n\n
changed = SIGNAL(\'currentChanged(const QModelIndex &, const QModelIndex &)\')\n\n# Call my method directly\n@ui.mytree.selectionMode.connect( changed, &method(:tree_selected) )\n\n# Alternatively, just put the logic in the same spot as the connection\n@ui.mytree.selectionMode.connect( changed ) do |current_index, previous_index|\n  # \xe2\x80\xa6handle the change here\xe2\x80\xa6\nend\n
Run Code Online (Sandbox Code Playgroud)\n