I am using Qt on my RPI3. I found a QML example which is Tesla Car instrument cluster. You can access full code from here or here.
I successfully created a project and debugged it. Now I am trying to change a value in the QML code from C++ side. There is a timer in my C++ code every 30 seconds I am trying to change speed value in the QML code with using QMetaObject::inokeMethod():
function. I read all examples in here.
Here is my C ++ code
#ifndef MYTIMER_H
#define MYTIMER_H
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QTimer>
#include <QtDebug>
class MyTimer : public QObject
{
Q_OBJECT
public:
MyTimer();
QTimer *timer;
int i=0;
public slots:
void MySlot();
};
#endif // MYTIMER_H
#include "mytimer.h"
MyTimer::MyTimer()
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(MySlot()));
timer->start(10000);
}
void MyTimer::MySlot()
{
i++;
if(i==3)
{
i=0;
QQmlEngine engine;
QQmlComponent component(&engine,QUrl(QStringLiteral("qrc:/Speedometer.qml")));
QObject *object = component.create();
QVariant speeds=100;
QVariant returnedValue;
QMetaObject::invokeMethod(object,"speedNeedleValue",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, speeds));
qDebug() << "From QML"<< returnedValue.toString();
delete object;
}
}
Here is QML code:
import QtQuick 2.4
import QtGraphicalEffects 1.0
Rectangle {
color: "transparent"
SpeedNeedle {
id: speedoNeedle
anchors.verticalCenterOffset: 0
anchors.centerIn: parent
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_A) {
speedNeedleValue(100)
drive()
}
}
function speedNeedleValue(speeds) {
speedoNeedle.value = speeds
return ": I am here"
}
}
If I press the "A" button my speedNeedleValue();
function is working. And in debug page I can get the return data return ": I am here"
.
Problem is I can't set the speeds
argument with invoke function.
Here is the debug page:
"https://preview.ibb.co/kqpvWS/rpi.png"
Every time interrupt I can get "I am here". but I also get " JIT is disabled.... " warning too.
Thank you for your answers.