Qt:将信号连接到具有更多参数的插槽
我想将按钮的信号 clicked()
连接到不同对象的插槽。
目前,我将信号连接到辅助方法并从那里调用所需的插槽:
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
void buttonClicked() { // Helper method. I'd like to avoid it.
someObject.desiredSlot(localFunc1(), localFunc2());
}
但也许有一种更简单、更明显的方法来做到这一点?
I want to connect a signal clicked()
from the button to a slot of different object.
Currently I connect signal to helper method and call desired slot from there:
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
void buttonClicked() { // Helper method. I'd like to avoid it.
someObject.desiredSlot(localFunc1(), localFunc2());
}
But maybe there is a more simple and obvious way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是您想要做的吗:
单击的信号应该连接到“desiredSlot”,该“desiredSlot”采用 localFunc1 & 返回的两个参数。 2 ??
这是不可能的,您可以在 QT 文档中阅读。槽可以接受比信号提供的参数少的参数 - 但反之则不然! (文档说“此连接将报告运行时错误”)
is this what you want to do:
the signal clicked should be connected to the "desiredSlot" which takes two arguments that are returned by localFunc1 & 2 ??
this is not possible, as you can read in the QT docs. A slot can take less arguments than provided by the signal - but not the opposite way! (The documentation says "This connection will report a runtime error")
这应该适用于 qt 5 中的新信号/槽机制:
您需要根据需要调整 lambda 捕获。
This ought to work with the new signal/slot mechanism in qt 5:
You will need to adjust the lambda capture to your needs.
在某些情况下,默认参数可能会有所帮助,例如将desiredSlot声明为:
但是您无法访问默认参数中的成员。
In some cases, default arguments may help, e.g. declare desiredSlot as:
You cannot access members in default argument though.
这不是QT中连接信号和槽的方式。您应该使用:
查看QT 文档。
That is not the way to connect signals and slots in QT. You should use:
Have a look at the QT documentation.