QRemoteObjectNode::remoteObjectAdded 信号不触发

QRemoteObjectNode::remoteObjectAdded signal does not fire

在学习如何使用 QtRO 技术预览模块时,我尝试了一个简单的 3 节点网络(注册节点、源对象远程处理节点和客户端节点)。

注册表节点 main.cpp:

#include <QCoreApplication>
#include <QRemoteObjectRegistryHost>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QRemoteObjectRegistryHost host(QUrl("tcp://127.0.0.1:5557"));

    return a.exec();
}

源节点main.cpp:

#include <QCoreApplication>
#include <QRemoteObjectHost>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTimer timer;
    timer.start(10000);

    QRemoteObjectHost host;
    if(!host.setHostUrl(QUrl("tcp://127.0.0.1:5556"))) qDebug() << "Host url " << host.lastError();
    if(!host.setRegistryUrl(QUrl("tcp://127.0.0.1:5557"))) qDebug() << "Reg url " << host.lastError();

    if(!host.enableRemoting(&timer, "HostTimer")) qDebug() << "Remoting error " << host.lastError();

    return a.exec();
}

客户端节点main.cpp:

#include <QCoreApplication>
#include <QRemoteObjectNode>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QRemoteObjectNode node(QUrl("tcp://127.0.0.1:5557"));

    QObject::connect(&node, &QRemoteObjectNode::remoteObjectAdded,
                     [](const QRemoteObjectSourceLocation& info){
        qDebug() << "New source added : " << info;
    });

    qDebug() << "Waiting for registry ";
    node.waitForRegistry(10000);

    qDebug() << "Already here sources : " << node.registry()->sourceLocations();

    QTimer timer;
    timer.start(5000);

    QObject::connect(&timer, &QTimer::timeout,
                     [&](){
        qDebug() << "New sources list : " << node.registry()->sourceLocations();
    });


    return a.exec();
}

我首先启动注册表节点,然后是客户端节点,最后是源节点,我希望通过这个启动顺序启动 remoteObjectAdded,但它没有。

非常感谢您的帮助。

我回答我的问题。

我在连接方法调用中使用了错误的信号源。

客户端节点将是:

#include <QCoreApplication>
#include <QRemoteObjectNode>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QRemoteObjectNode node(QUrl("tcp://127.0.0.1:5557"));

    QObject::connect(node.registry(), &QRemoteObjectRegistry::remoteObjectAdded,
                     [](const QRemoteObjectSourceLocation& info){
        qDebug() << "New source added : " << info;
    });

    qDebug() << "Waiting for registry ";
    node.waitForRegistry(10000);

    qDebug() << "Already here sources : " << node.registry()->sourceLocations();

    QTimer timer;
    timer.start(5000);

    QObject::connect(&timer, &QTimer::timeout,
                     [&](){
        qDebug() << "New sources list : " << node.registry()->sourceLocations();
    });


    return a.exec();
}

触发信号的是注册中心而不是节点,在所有源节点都连接到注册中心后听起来很合乎逻辑,因此它会触发 remoteObjectAdded 信号。