qt 插槽柯里化

发布于 2024-10-13 08:23:34 字数 37 浏览 3 评论 0原文

有没有办法咖喱qt插槽?也许有类似 curryng 的东西?

Is there a way curry qt slot? Maybe there is something similar to curryng?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烟雨凡馨 2024-10-20 08:23:34

尽管不可能直接使用 Qt,但可以通过 LibQxt 获得一些绑定/柯里化。例如,来自 QxtBoundFunction 的文档:

到目前为止,最常见的预期用途是在以下情况下向插槽提供参数:
信号没有优惠一。许多刚接触 Qt 的开发人员尝试编写如下代码:
\代码
连接(按钮,信号(单击()),lineEdit,SLOT(setText(“你好,世界”)));
\结束码
经验丰富的 Qt 开发人员会立即发现这里的缺陷。典型解决方案
是创建一个简短的单行包装槽来调用所需的函数。一些
聪明的开发人员甚至可以使用 QSignalMapper 来处理只需要一个的插槽
int 或 QString 参数。

QxtBoundFunction 使得前面的 connect 语句可以这样写:
\代码
连接(按钮,信号(单击()),QxtMetaObject ::绑定(lineEdit,
SLOT(setText(QString)), Q_ARG(QString, "你好,世界!")));
\代码
这可以实现相同的结果,而无需创建新的插槽,或更糟的是,
整个对象,只是为了传递一个常量值。

此外,通过使用 QXT_BIND 宏,来自信号的参数
可以重新排列、跳过或与提供的常量参数一起传递
使用 Q_ARG 宏。这可用于提供有状态回调
例如,通用函数。

可以绑定多种功能。最常见的绑定适用于
Qt 信号和槽,但也可以绑定标准 C/C++ 函数。
未来的开发可能会添加绑定到 C++ 成员函数的功能,
开发人员可以创建自定义 QxtBoundFunction 子类以实现更多功能
如有必要,请灵活处理。

虽然我已经向 LibQxt 提交了一些补丁,但我没有直接使用它,所以你的情况可能会有所不同。

Although it's not possible directly using Qt, some binding/currying is available through LibQxt. For example and from the docs of QxtBoundFunction:

By far, the most common expected use is to provide a parameter to a slot when the
signal doesn't have offer one. Many developers new to Qt try to write code like this:
\code
connect(button, SIGNAL(clicked()), lineEdit, SLOT(setText("Hello, world")));
\endcode
Experienced Qt developers will immediately spot the flaw here. The typical solution
is to create a short, one-line wrapper slot that invokes the desired function. Some
clever developers may even use QSignalMapper to handle slots that only need one
int or QString parameter.

QxtBoundFunction enables the previous connect statement to be written like this:
\code
connect(button, SIGNAL(clicked()), QxtMetaObject::bind(lineEdit,
SLOT(setText(QString)), Q_ARG(QString, "Hello, world!")));
\code
This accomplishes the same result without having to create a new slot, or worse,
an entire object, just to pass a constant value.

Additionally, through the use of the QXT_BIND macro, parameters from the signal
can be rearranged, skipped, or passed alongside constant arguments provided
with the Q_ARG macro. This can be used to provide stateful callbacks to a
generic function, for example.

Many kinds of functions can be bound. The most common binding applies to
Qt signals and slots, but standard C/C++ functions can be bound as well.
Future development may add the ability to bind to C++ member functions,
and developers can make custom QxtBoundFunction subclasses for even more
flexibility if necessary.

Although I have submitted some patches to LibQxt, I haven't used this directly so your mileage may vary.

昨迟人 2024-10-20 08:23:34

使用 Qt 信号/槽无法绑定参数。您必须使用 boost::signals 和 boost::bind 来实现此类功能。

Binding arguments is not possible using Qt signal/slots. You'll have to use boost::signals and boost::bind instead to achieve such functionality.

烙印 2024-10-20 08:23:34

您可以使用 QSignalMapper 将一些信号绑定到它,然后将它自己的信号连接到附加一些参数的目标插槽。

// connect signal to mapper
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button1, QString("param1"));
signalMapper->setMapping(button2, QString("param2"));
connect(button1, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(button2, SIGNAL(clicked()), signalMapper, SLOT(map()));

// connect mapper signal to slot
connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(originalSlot(const QString &)));

You could use QSignalMapper to bind connect some signals to it and then connect it's own signals to target slots with some parameters attached.

// connect signal to mapper
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(button1, QString("param1"));
signalMapper->setMapping(button2, QString("param2"));
connect(button1, SIGNAL(clicked()), signalMapper, SLOT(map()));
connect(button2, SIGNAL(clicked()), signalMapper, SLOT(map()));

// connect mapper signal to slot
connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(originalSlot(const QString &)));
北恋 2024-10-20 08:23:34

当然,现在我们有了 Qt 5 并能够将信号连接到任意可调用对象:

connect(sender, &MyClass::mySignal, receiver, std::bind(&OtherClass::mySlot, boundArg));

connect(sender, &MyClass::mySignal, receiver, [=] { receiver->mySlot(boundArg); });

Of course, now we have Qt 5 and the ability to connect signals to arbitrary callable objects:

connect(sender, &MyClass::mySignal, receiver, std::bind(&OtherClass::mySlot, boundArg));

connect(sender, &MyClass::mySignal, receiver, [=] { receiver->mySlot(boundArg); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文