gpt4 book ai didi

c++ - qt 插槽柯里化(Currying)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:08 28 4
gpt4 key购买 nike

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

最佳答案

虽然不能直接使用 Qt,但可以通过 LibQxt 进行一些绑定(bind)/套用。 .例如,来自 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.

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

关于c++ - qt 插槽柯里化(Currying),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4784251/

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