0

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.

Al.G.
  • 4,327
  • 6
  • 31
  • 56
Serkan
  • 11
  • 3
  • 3
    Possible duplicate of [Calling a QML function from C++](https://stackoverflow.com/questions/20000255/calling-a-qml-function-from-c) – Mohammad Kanan Feb 14 '18 at 20:35
  • Where is your main function? I think the QQmlEngine that you use in MySlot is different from the one you use in the main – eyllanesc Feb 14 '18 at 20:49
  • Your timer is creating a new engine and speedometer everytime it time outs. You can pass the engine that loaded your UI to your timer as reference and then find the root object and then find child object from root object's children using object name. – talamaki Feb 15 '18 at 06:12

0 Answers0