QtRuby连接信号和带参数/参数的插槽
我想知道如何连接到接收参数的信号(使用Ruby块) 我知道如何连接到不带参数的设备:QtRuby连接信号和带参数/参数的插槽,ruby,qt,qt4,qtruby,Ruby,Qt,Qt4,Qtruby,我想知道如何连接到接收参数的信号(使用Ruby块) 我知道如何连接到不带参数的设备: myCheckbox.connect(SIGNAL :clicked) { doStuff } 但是,这不起作用: myCheckbox.connect(SIGNAL :toggle) { doStuff } 它不起作用,因为切换槽采用参数void QAbstractButton::toggled(bool选中)。如何使其与参数一起工作 谢谢。您的问题的简短答案是,您必须使用插槽方法声明要连接到的插槽的方法
myCheckbox.connect(SIGNAL :clicked) { doStuff }
但是,这不起作用:
myCheckbox.connect(SIGNAL :toggle) { doStuff }
它不起作用,因为切换槽采用参数void QAbstractButton::toggled(bool选中)
。如何使其与参数一起工作
谢谢。您的问题的简短答案是,您必须使用
插槽方法声明要连接到的插槽的方法签名:
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
以前从未尝试过QtRuby,但不妨尝试一下,看看它是否有效:myCheckbox.connect(SIGNAL:toggle){| checked | doStuff}是的,想到了,不起作用:(尝试实现这个:这有点荒谬,我想……我只是决定做checkbox.connect(:SIGNAL“toggle(bool)”{| x | puts x}
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