//readJSON.h
{
Q_OBJECT
public:
readJSON();
public slots:
};
//main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url
(QStringLiteral
("qrc:/main.qml"));
QObject::connect(&engine,
&QQmlApplicationEngine
::objectCreated,
if (!obj && url == objUrl)
}, Qt::QueuedConnection);
watcher.addPath("data.json");
readJSON* myGlobal = new readJSON();
engine.rootContext()->setContextProperty("myGlobalObject", myGlobal);
QObject::connect(&watcher,
SIGNAL(fileChanged
()),
myGlobal, SLOT(updateDisplay()));
engine.load(url);
return app.exec();
}
//main.qml
Window {
visible: true
width: 800
height: 800
title: qsTr("Screen Display")
DisplaysForm {
function callCPP(text) {
var dataOne = myGlobalObject.readingJson(text)
dataOne = parseFloat(dataOne)
return dataOne
}
function updateDisplay() {
var value = callCPP("Engine_Spd")
engSpd_txt.text = value
var value1 = callCPP("Engine_Power")
engPwr_txt.text = value1
}
//readJSON.h
class readJSON :public QObject
{
Q_OBJECT
public:
readJSON();
public slots:
QString readingJson(const QString &name);
};
//main.cpp
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
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);
QFileSystemWatcher watcher;
watcher.addPath("data.json");
readJSON* myGlobal = new readJSON();
engine.rootContext()->setContextProperty("myGlobalObject", myGlobal);
QObject::connect(&watcher, SIGNAL(fileChanged()),
myGlobal, SLOT(updateDisplay()));
engine.load(url);
return app.exec();
}
//main.qml
Window {
visible: true
width: 800
height: 800
title: qsTr("Screen Display")
DisplaysForm {
function callCPP(text) {
var dataOne = myGlobalObject.readingJson(text)
dataOne = parseFloat(dataOne)
return dataOne
}
function updateDisplay() {
var value = callCPP("Engine_Spd")
engSpd_txt.text = value
var value1 = callCPP("Engine_Power")
engPwr_txt.text = value1
}
To copy to clipboard, switch view to plain text mode
Bookmarks