gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:12 25 4
gpt4 key购买 nike

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

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

myCheckbox.connect(SIGNAL :clicked) { doStuff }

但是,这不起作用:

myCheckbox.connect(SIGNAL :toggle) { doStuff }

它不起作用,因为切换槽采用参数 void QAbstractButton::toggled ( bool checked )。我怎样才能让它与参数一起工作?

谢谢。

最佳答案

对您的问题的简短回答是,您必须使用 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 block 连接信号以调用您喜欢的任何方法(或将逻辑内联)。使用以下语法,您不需要需要使用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

关于ruby - QtRuby 使用参数/参数连接信号和槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com