简而言之,您需要使用slots
方法声明要连接的槽的方法签名:
class MainGUI < Qt::MainWindow
slots 'open()', 'save()',
'tree_selected(const QModelIndex &,const QModelIndex &)'
def initialize(parent=nil)
super
@ui = Ui_MainWin.new
@ui.setupUi(self)
connect_menus!
populate_tree!
end
def connect_menus!
connect @ui.actionOpen, SIGNAL('triggered()'), self, SLOT('open()')
connect @ui.actionSave, SIGNAL('triggered()'), SLOT('save()')
connect @ui.actionQuit, SIGNAL('triggered()'), SLOT('close()')
end
def populate_tree!
tree = @ui.mytree
tree.model = MyModel.new(self)
connect(
tree.selectionModel,
SIGNAL('currentChanged(const QModelIndex &, const QModelIndex &)'),
SLOT('tree_selected(const QModelIndex &,const QModelIndex &)')
)
end
def tree_selected( current_index, previous_index )
end
def open
end
def save
end
end
请注意,传递给
SIGNAL
和
SLOT
的签名不包括任何变量名。
此外,正如您在评论中得出的结论,更简单(并且更符合 Ruby 风格)的做法是完全放弃“slot”概念,只需使用 Ruby 块将信号连接到调用所需的任何方法(或将逻辑内联)。使用以下语法,您无需使用
slots
方法预先声明方法或处理代码。
changed = SIGNAL('currentChanged(const QModelIndex &, const QModelIndex &)')
@ui.mytree.selectionMode.connect( changed, &method(:tree_selected) )
@ui.mytree.selectionMode.connect( changed ) do |current_index, previous_index|
end
checkbox.connect( :SIGNAL "toggle(bool)" ) { |x| puts x }
。 - Jean-Luc