Qt:将信号连接到具有更多参数的插槽

发布于 2024-11-29 04:17:11 字数 330 浏览 4 评论 0原文

我想将按钮的信号 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坦然微笑 2024-12-06 04:17:11

这是您想要做的吗:

单击的信号应该连接到“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")

潜移默化 2024-12-06 04:17:11

这应该适用于 qt 5 中的新信号/槽机制:

connect( button, &QPushButton::clicked, [&](){ someObject.desiredSlot( localFunc1(), localFunc2() ); } );

您需要根据需要调整 lambda 捕获。

This ought to work with the new signal/slot mechanism in qt 5:

connect( button, &QPushButton::clicked, [&](){ someObject.desiredSlot( localFunc1(), localFunc2() ); } );

You will need to adjust the lambda capture to your needs.

空气里的味道 2024-12-06 04:17:11

在某些情况下,默认参数可能会有所帮助,例如将desiredSlot声明为:

desiredSlot(int a=0, int b=0)

但是您无法访问默认参数中的成员。

In some cases, default arguments may help, e.g. declare desiredSlot as:

desiredSlot(int a=0, int b=0)

You cannot access members in default argument though.

难得心□动 2024-12-06 04:17:11

这不是QT中连接信号和槽的方式。您应该使用:

connect(button, SIGNAL(clicked()), receiver, SLOT(slotToBeCalled());

查看QT 文档

That is not the way to connect signals and slots in QT. You should use:

connect(button, SIGNAL(clicked()), receiver, SLOT(slotToBeCalled());

Have a look at the QT documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文