久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Qt 信號可以返回一個值嗎?

Can Qt signals return a value?(Qt 信號可以返回一個值嗎?)
本文介紹了Qt 信號可以返回一個值嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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 文檔 中表達(dá))是 Qt 信號不可能做到這一點.

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

但是,當(dāng)我在以下類定義上運行 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 返回類型的信號,而且似乎以一種允許返回值傳遞的方式積極實現(xiàn)它:

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;
}

所以:根據(jù)文檔,這件事是不可能的.那么moc在這里做什么?

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

插槽可以有返回值,所以我們可以將插槽與現(xiàn)在有返回值的信號的返回值?畢竟這可能嗎?如果有,有用嗎?

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,當(dāng)它解析為 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).

推薦答案

好的.所以,我做了更多的調(diào)查.似乎這是可能的.我能夠發(fā)出信號,并從信號所連接的插槽接收值.但是,問題是它只從多個連接的插槽返回最后一個返回值:

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"

當(dāng) main 運行時,它會構(gòu)建一個測試類.構(gòu)造函數(shù)將兩個插槽連接到 testSignal 信號,然后發(fā)出信號.它從調(diào)用的插槽中捕獲返回值.

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 信號是信號模式的語法糖接口.插槽是信號的接收者.在直接連接的信號槽關(guān)系中,你可以認(rèn)為它類似于(偽代碼):

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.

這篇關(guān)于Qt 信號可以返回一個值嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 99亚洲| 午夜影院在线观看免费 | 韩日精品在线观看 | 美女网站视频免费黄 | 亚洲成av | 亚洲成人激情在线观看 | 亚洲欧美中文日韩在线v日本 | 无码国模国产在线观看 | 91一区| 欧美精品成人影院 | 欧美性生活免费 | 一区二区三区高清不卡 | 在线观看涩涩视频 | 欧美午夜精品理论片a级按摩 | 99热最新网址 | 亚洲欧美日韩精品久久亚洲区 | 99精品热视频 | 欧美日韩成人影院 | 97超碰成人| 欧美一级免费观看 | 在线免费91 | 亚洲国产一区二区三区四区 | 午夜小视频在线观看 | 亚洲欧美日韩中文字幕一区二区三区 | 午夜成人在线视频 | 日韩欧美亚洲 | 国产男人的天堂 | 日韩欧美二区 | 精品亚洲一区二区三区 | 欧美精品91| 一级片在线视频 | 五月激情综合 | 九九亚洲 | 中文字幕在线观看国产 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 国产成人精品午夜 | 一级黄色绿像片 | 欧美日韩高清在线一区 | 国产精品久久国产精品 | 国产欧美一区二区三区久久人妖 | 精品动漫一区 |