I'm trying to use data received from a Bluetooth stream (coded in regular Qt C++) in my QML code (Qt Quick 3D), with the aim of using this data to rotate a 3D model. Is it possible to do this via bindings or similar? Thanks in advance.
Printable View
I'm trying to use data received from a Bluetooth stream (coded in regular Qt C++) in my QML code (Qt Quick 3D), with the aim of using this data to rotate a 3D model. Is it possible to do this via bindings or similar? Thanks in advance.
Supposing that in your C++ you have a class that inherits from QObject:
This could be your class.
[CODE]
#if QT_VERSION > 0x040603
#include <qdeclarative.h>
#endif
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(float x READ getX WRITE setX NOTIFY onXChanged)
Q_PROPERTY(float y READ getY WRITE setY NOTIFY onYChanged)
Q_PROPERTY(float z READ getZ WRITE setZ NOTIFY onZChanged)
public:
float getX();
float getY();
float getZ();
void setX(float val){ x = val;}
void setY(float val){ y = val;}
void setZ(float val){ z = val;}
static void registerQMLType()
{
qmlRegisterType<MyClass >("MyLibrary", 1, 0, "MyClass ");
}
//...the functions that will recieve data from bluetooth, set them to the local variables and every time
// any local variable changes you will emit the appropriate signal. Mandatory to achieve binding
signals:
void onXChanged(float val);
void onYChanged(float val);
void onZChanged(float val);
private:
float x;
float y;
float z;
};
[/CODE]
In the main function before loading the qml file you will place the following code to register your C++ class to the QML:
[CODE]
MyClass::registerQMLType();
[/CODE]
Then in the qml file you want to use it :
[CODE]
import Qt 4.7
import MyLibrary 1.0
Rectangle
{
x:bluetoothReciever.x
y:bluetoothReciever.y
z:bluetoothReciever.z
MyClass{
id:bluetoothReciever
}
}
[/CODE]
This is a way to do what you want. Of course you need to add the bluetooth functions etc. but you get the idea.
This is exactly what I'm looking for, thanks favoritas!