pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

Can Qt signals return a value?(Qt 信號可以返回一個值嗎?)
本文介紹了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 返回類型的信號,而且似乎以一種允許返回值傳遞的方式積極實現(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,當它解析為 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).

推薦答案

好的.所以,我做了更多的調查.似乎這是可能的.我能夠發(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"

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

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模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(liá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)形?)
主站蜘蛛池模板: 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | 一礼通 (www.yilitong.com)-企业礼品解决方案一站式服务平台 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 禹城彩钢厂_钢结构板房_彩钢复合板-禹城泰瑞彩钢复合板加工厂 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 贵州成人高考网_贵州成考网 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 多功能三相相位伏安表-变压器短路阻抗测试仪-上海妙定电气 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 高扬程排污泵_隔膜泵_磁力泵_节能自吸离心水泵厂家-【上海博洋】 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 进口便携式天平,外校_十万分之一分析天平,奥豪斯工业台秤,V2000防水秤-重庆珂偌德科技有限公司(www.crdkj.com) | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 云南成人高考网| 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 | 压滤机-洗沙泥浆处理-压泥机-山东创新华一环境工程有限公司 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 阴离子聚丙烯酰胺价格_PAM_高分子聚丙烯酰胺厂家-河南泰航净水材料有限公司 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 深圳富泰鑫五金_五金冲压件加工_五金配件加工_精密零件加工厂 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 高扬程排污泵_隔膜泵_磁力泵_节能自吸离心水泵厂家-【上海博洋】 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 | 传爱自考网_传爱自学考试网| 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 加盟店-品牌招商加盟-创业项目商机平台 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 酒店厨房设计_中央厨房设计_北京商用厨房设计公司-奇能商厨 |