I am trying to read a value from mysql db through Qt and push the value to QML(specifically to a CircularGauge dial). I am successfully able to read the db value and print it to console in Qt. When I try to expose the value to QML, it is not getting reflected in QML dashboard. The following are my files:-
qml.h
#ifndef QML_H
#define QML_H
#include <QObject>
#include <QVariant>
#include <QDebug>
#include <QTime>
#include <qquickimageprovider.h>
#include <QImage>
#include <QString>
/*------------------------------SPEED------------------------------*/
class Speed : public QObject
{
Q_OBJECT
Q_PROPERTY(double velocity MEMBER m_velocity NOTIFY velocityChanged)
public:
Speed(QObject* parent = nullptr);
virtual ~Speed() {}
const double MPS2KNOTS = 1.94;
void velocityCallback();
private slots:
void updateVelocity(double x);
signals:
void velocityChanged();
void receivedVelocity(double x);
private:
double m_velocity = 0.0;
};
#endif // QML_H
qml.cpp
#include <QSqlDatabase>
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QDebug>
#include <qml.h>
Speed::Speed(QObject*)
{
connect(this, SIGNAL(receivedVelocity(double)), this, SLOT(updateVelocity(double)));
}
void Speed::velocityCallback()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("sim_speed");
db.setUserName("administrator");
db.setPassword("test");
bool ok = db.open();
double x;
QSqlQuery query;
//while (ok) {
query.prepare("SELECT speed FROM speed");
query.exec();
if (query.next()) {
x= query.value(0).toDouble();
}
qDebug() << "Value is";
qDebug() << ok;
qDebug() << x;
receivedVelocity(x);
//}
}
void Speed::updateVelocity(double x)
{
m_velocity = x;
emit velocityChanged();
qDebug() << m_velocity;
}
main.cpp
#include <functional>
#include <QCoreApplication>
#include <QObject>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QUrl>
#include <QString>
#include <QQuickWindow>
#include <QtQml>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>
#include <qml.h>
#include <QQmlProperty>
#include <thread> // std::thread
#include <QDebug>
#include <QCoreApplication>
#include <QTextStream>
#include <QFile>
using namespace std;
int main(int argc, char** argv)
{
//std::thread dbret(velocityCallback());
QGuiApplication app(argc, argv);
Speed speedqml(&app);
//QObject::connect(&app, &QCoreApplication::aboutToQuit, [](){});
QQmlApplicationEngine engine(&app);
engine.rootContext()->setContextProperty("velocity", &speedqml);
Speed sp;
sp.velocityCallback();
engine.load(QUrl("qrc:/main.qml"));
// std::thread dbret(mainn);
//dbret.join();
return app.exec();
}
main.qml
import QtMultimedia 5.8
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Extras 1.4
import QtSensors 5.10
//import Ros 1.0
Window {
visible: true
width: 1920
height: 1080
title: qsTr("Gazebo")
Rectangle {
id: speedometer
x: 10
y: 10
width: 150
height:150
radius: width*0.5
opacity: 0.5
color: "black"
CircularGauge {
id:gauge
value: velocity.velocity
maximumValue : 10
x:10
y:10
width:150
height:150
anchors.centerIn: parent
style: CircularGaugeStyle {
labelStepSize: 0.5
tickmarkStepSize: 0.5
}
Text {
text: "Speed:"+velocity.velocity+"m/s"
anchors.top:parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
Behavior on value {
NumberAnimation {
duration: 1000
}
}
MouseArea {
anchors.fill: parent
onClicked: {
// tachometer.visible = false
// speedometer.visible = false
// compassdial.visible = false
// altimeterback.visible = false
// ammeter.visible = false
// voltmeter.visible = false
// throttlegauge.visible = false
// dashboard.visible = true
}
}
}
}
}