【问题标题】:How to emit signals from source to replica using QT Remote Objects如何使用 QT 远程对象从源向副本发出信号
【发布时间】:2021-12-20 01:26:15
【问题描述】:

如何从源 c++ 文件发出信号并使用副本文件中的连接捕获它。 我已经在rep文件中声明了信号和发出信号的槽。尝试使用Connections捕获信号:并将目标作为复制对象。我已经在我的客户端qt引擎中设置了replicaobject。 //.rep 文件 班级考试 { 插槽(测试()); 信号(测试信号()); }

//Interface.cpp file in source side
void Interface::test()
{
    Q_EMIT testsignal();
}

//main.qml 在客户端 连接{ 目标:Interfacereplica 测试信号:{ console.log("警告!!!"); }

【问题讨论】:

    标签: qt qtremoteobjects


    【解决方案1】:

    您需要将副本存储在可以从 QML 访问的类实例中。您可以将副本的信号连接到您在类中声明的信号,然后从 QML 接收它。

    请查看下面的代码。 (replicaexample.h、replicaexample.cpp 和 main.qml)

    (主机应用程序是控制台应用程序,客户端是 QtQuick 应用程序。)

    主机应用程序

    "Hello! I am sending message, counter: 0"
    "Hello! I am sending message, counter: 1"
    "Hello! I am sending message, counter: 2"
    "Hello! I am sending message, counter: 3"
    "Hello! I am sending message, counter: 4"
    "Hello! I am sending message, counter: 5"
    

    ExampleReplica.rep

    class ExampleRep
    {
        SIGNAL(my_signal(QString message));
    };
    

    sourceexample.h

    #ifndef SOURCEEXAMPLE_H
    #define SOURCEEXAMPLE_H
    
    #include <QTimer>
    #include <QDebug>
    
    #include "rep_ExampleReplica_source.h"
    
    class SourceExample : public ExampleRepSimpleSource
    {
        Q_OBJECT
    public:
        SourceExample(QObject* parent = nullptr);
        ~SourceExample();
    
    private Q_SLOTS:
        void _timerSlot();
    
    private:
        int _counter;
        QTimer* _timer;
        QString _message;
    };
    
    #endif // SOURCEEXAMPLE_H
    

    sourceexample.cpp

    #include "sourceexample.h"
    
    SourceExample::SourceExample(QObject* parent)
        : ExampleRepSimpleSource(parent)
        , _counter(0)
    {
        _timer = new QTimer(this);
        _timer->setInterval(2000);
        connect(_timer, &QTimer::timeout, this, &SourceExample::_timerSlot);
        _timer->start();
    }
    
    SourceExample::~SourceExample()
    {
    
    }
    
    void SourceExample::_timerSlot()
    {
        _message = "Hello! I am sending message, counter: " + QString::number(_counter++);
        qInfo() << _message;
        Q_EMIT my_signal(_message);
    }
    

    ma​​in.cpp

    #include <QCoreApplication>
    #include <iostream>
    
    #include "sourceexample.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        SourceExample sourceObject;
    
        QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:sourceNode")));
        srcNode.enableRemoting(&sourceObject);
    
        return a.exec();
    }
    

    客户端应用程序

    qml: console log: Hello! I am sending message, counter: 0
    qml: console log: Hello! I am sending message, counter: 1
    qml: console log: Hello! I am sending message, counter: 2
    qml: console log: Hello! I am sending message, counter: 3
    qml: console log: Hello! I am sending message, counter: 4
    qml: console log: Hello! I am sending message, counter: 5
    

    replicaexample.h

    #ifndef REPLICAEXAMPLE_H
    #define REPLICAEXAMPLE_H
    
    #include <QObject>
    #include <QSharedPointer>
    #include <QDebug>
    
    #include "rep_ExampleReplica_replica.h"
    
    class ReplicaExample : public QObject
    {
        Q_OBJECT
    public:
        ReplicaExample(QSharedPointer<ExampleRepReplica> repNode, QObject* parent = nullptr);
        ~ReplicaExample();
    
    Q_SIGNALS:
        void my_signal(QString message);
    
    private:
        QSharedPointer<ExampleRepReplica> _repNode;
    };
    
    #endif // REPLICAEXAMPLE_H
    

    replicaexample.cpp

    #include "replicaexample.h"
    
    ReplicaExample::ReplicaExample(QSharedPointer<ExampleRepReplica> repNode, QObject* parent)
        : QObject(parent)
        , _repNode(repNode)
    {
        QObject::connect(_repNode.data(), &ExampleRepReplica::my_signal, this, &ReplicaExample::my_signal);
    }
    
    ReplicaExample::~ReplicaExample()
    {
    
    }
    

    ma​​in.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlEngine>
    #include <QQmlContext>
    
    #include "replicaexample.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QSharedPointer<ExampleRepReplica> ptr;
    
        QRemoteObjectNode repNode;
        repNode.connectToNode(QUrl(QStringLiteral("local:sourceNode")));
    
        ptr.reset(repNode.acquire<ExampleRepReplica>());
    
        ReplicaExample client(ptr);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        engine.rootContext()->setContextProperty("repObject", &client);
    
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    ma​​in.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Connections {
            target: repObject
    
            function onMy_signal(message) {
                console.log("console log: " + message)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多