Вы можете отправить, создав класс C ++. Когда вы отправляете эти определенные значения на C ++, вы можете читать на стороне QML. Вот пример, вы можете попробовать:
main. cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "sender.h"
#include <QQmlContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Sender>("Sender", 1, 0, "Sender");
Sender sender;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("Sender", &sender);
const QUrl url(QStringLiteral("qrc:/main.qml"));
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();
}
sender.h
#ifndef SENDER_H
#define SENDER_H
#include <QObject>
class Sender: public QObject
{
Q_OBJECT
public:
explicit Sender(QObject *parent = nullptr);
public slots:
void send_defined();
signals:
void sendDefined(QString str);
};
#endif // SENDER_H
sender. cpp
#include "sender.h"
#define STR_STEP0 "step0"
#define STR_STEP1 "step1"
#define STR_STEP2 "step2"
#define STR_STEP3 "step3"
#define STR_STEP4 "step4"
#define STR_STEP5 "step5"
#define STR_STEP6 "step6"
#define STR_STEP7 "step7"
#define STR_STEP8 "step8"
Sender::Sender(QObject *parent)
{
}
void Sender::send_defined()
{
emit sendDefined(STR_STEP0);
emit sendDefined(STR_STEP1);
emit sendDefined(STR_STEP2);
emit sendDefined(STR_STEP3);
emit sendDefined(STR_STEP4);
emit sendDefined(STR_STEP5);
emit sendDefined(STR_STEP6);
emit sendDefined(STR_STEP7);
emit sendDefined(STR_STEP8);
}
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Connections{
target: Sender
onSendDefined:{
console.log(str)
}
}
Button{
text: "Test"
onClicked: {
Sender.send_defined()
}
}
}