2017-08-07 59 views
0

在學習如何使用QtRO技術預覽模塊時,我嘗試了一個簡單的3節點網絡(一個註冊表節點,一個源對象遠程處理節點和一個客戶端節點)。QRemoteObjectNode :: remoteObjectAdded信號不會觸發

註冊表節點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啓動,bu t它沒有。

您的幫助將得到高度讚賞。

回答

1

我回答我的問題。

我在連接方法調用中使用了錯誤的信號源。

客戶端節點會像:

#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信號合理的邏輯。