0

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

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

//main.qml 在客户端 Connections { target: Interfacereplica onTestsignal: { console.log("Alert!!!"); }

4

1 回答 1

2

您需要将副本存储在可以从 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));
};

源示例.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

源示例.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);
}

主文件

#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

副本示例.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

副本示例.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()
{

}

主文件

#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();
}

main.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)
        }
    }
}
于 2022-01-17T20:50:56.243 回答