Qt 信号可以返回一个值吗?

时间:2023-01-22
本文介绍了Qt 信号可以返回一个值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boost.Signals 允许 各种策略使用槽的返回值来形成信号的返回值.例如.将它们相加,形成一个 vector,或者返回最后一个.

Boost.Signals allows various strategies of using the return values of slots to form the return value of the signal. E.g. adding them, forming a vector out of them, or returning the last one.

常识(在 Qt 文档 中表达)是 Qt 信号不可能做到这一点.

The common wisdom (expressed in the Qt documentation ) is that no such thing is possible with Qt signals.

但是,当我在以下类定义上运行 moc 时:

However, when I run the moc on the following class definition:

class Object : public QObject {
    Q_OBJECT
public:
    explicit Object( QObject * parent=0 )
        : QObject( parent ) {}

public Q_SLOTS:
    void voidSlot();
    int intSlot();

Q_SIGNALS:
    void voidSignal();
    int intSignal();
};

moc 不仅没有抱怨具有非 void 返回类型的信号,而且似乎以一种允许返回值传递的方式积极实现它:

Not only doesn't moc complain about the signal with the non-void return type, it seems to actively implement it in such a way as to allow a return value to pass:

// SIGNAL 1
int Object::intSignal()
{
    int _t0;
    void *_a[] = { const_cast<void*>(reinterpret_cast<const void*>(&_t0)) };
    QMetaObject::activate(this, &staticMetaObject, 1, _a);
    return _t0;
}

所以:根据文档,这件事是不可能的.那么moc在这里做什么?

So: according to the docs, this thing isn't possible. Then what is moc doing here?

插槽可以有返回值,所以我们可以将插槽与现在有返回值的信号的返回值?毕竟这可能吗?如果有,有用吗?

Slots can have return values, so can we connect a slot with a return value to a signal with a return value now? May that be possible, after all? If so, is it useful?

我不是在要求解决方法,所以请不要提供任何方法.

I'm not asking for workarounds, so please don't provide any.

它显然在 Qt::QueuedConnection 模式(也不是 QPrintPreviewWidget API,但它仍然存在并且很有用).但是 Qt::DirectConnectionQt::BlockingQueuedConnection(或 Qt::AutoConnection,当它解析为 Qt::直接连接).

It obviously isn't useful in Qt::QueuedConnection mode (neither is the QPrintPreviewWidget API, though, and still it exists and is useful). But what about Qt::DirectConnection and Qt::BlockingQueuedConnection (or Qt::AutoConnection, when it resolves to Qt::DirectConnection).

推荐答案

好的.所以,我做了更多的调查.似乎这是可能的.我能够发出信号,并从信号所连接的插槽接收值.但是,问题是它只从多个连接的插槽返回最后一个返回值:

OK. So, I did a little more investigating. Seems this is possible. I was able to emit a signal, and receive value from the slot the signal was connected to. But, the problem was that it only returned the last return value from the multiple connected slots:

这是一个简单的类定义(main.cpp):

Here's a simple class definition (main.cpp):

#include <QObject>
#include <QDebug>

class TestClass : public QObject
{
    Q_OBJECT
public:
    TestClass();

Q_SIGNALS:
    QString testSignal();

public Q_SLOTS:
    QString testSlot1() {
        return QLatin1String("testSlot1");
    }
    QString testSlot2() {
        return QLatin1String("testSlot2");
    }
};

TestClass::TestClass() {
    connect(this, SIGNAL(testSignal()), this, SLOT(testSlot1()));
    connect(this, SIGNAL(testSignal()), this, SLOT(testSlot2()));

    QString a = emit testSignal();
    qDebug() << a;
}

int main() {
    TestClass a;
}

#include "main.moc"

当 main 运行时,它会构建一个测试类.构造函数将两个插槽连接到 testSignal 信号,然后发出信号.它从调用的插槽中捕获返回值.

When main runs, it constructs one of the test classes. The constructor wires up two slots to the testSignal signal, and then emits the signal. It captures the return value from the slot(s) invoked.

不幸的是,您只能获得最后一个返回值.如果你评估上面的代码,你会得到:testSlot2",来自信号连接槽的最后一个返回值.

Unfortunately, you only get the last return value. If you evaluate the code above, you'll get: "testSlot2", the last return value from the connected slots of the signal.

这就是原因.Qt 信号是信号模式的语法糖接口.插槽是信号的接收者.在直接连接的信号槽关系中,你可以认为它类似于(伪代码):

Here's why. Qt Signals are a syntax sugared interface to the signaling pattern. Slots are the recipients of a signal. In a direct connected signal-slot relationship, you could think of it similar to (pseudo-code):

foreach slot in connectedSlotsForSignal(signal):
    value = invoke slot with parameters from signal
return value

显然,moc 在这个过程中做了更多的工作(基本的类型检查等),但这有助于描绘画面.

Obviously the moc does a little more to help in this process (rudimentary type checking, etc), but this helps paint the picture.

这篇关于Qt 信号可以返回一个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:将 QString 转换为 char* 下一篇:使用 pkg-config 输出将库链接到 QT 项目

相关文章

最新文章