Add custom class to Q_PROPERTY
-
Hi!
I have custom class inheriting from QObject. How I can add this class to Q_PROPERTY?#ifndef BLUETOOTH_H #define BLUETOOTH_H #include <QObject> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QJsonValue> #include <QTime> #include <QTimer> #include <QBluetoothLocalDevice> #include <QBluetoothDeviceDiscoveryAgent> #include <QBluetoothDeviceInfo> #include <QBluetoothSocket> #include <QBluetoothServer> #include <QBluetoothServiceDiscoveryAgent> #include <QBluetoothLocalDevice> #include <QBluetoothUuid> #include <QBluetoothAddress> #include <QLowEnergyController> #include <QLowEnergyService> #include <QLowEnergyCharacteristic> #include "structures.h" class Bluetooth : public QObject { Q_OBJECT // Q_GADGET public: explicit Bluetooth(QObject *parent = nullptr); bool m_startStatus; bool m_isBody; int m_speed; int m_targetTime; int m_forceValue; bool m_redLed; Q_PROPERTY(bool startStatus MEMBER m_startStatus NOTIFY startStatusChanged) Q_PROPERTY(bool isBody MEMBER m_isBody NOTIFY isBodyChanged) Q_PROPERTY(int speed MEMBER m_speed NOTIFY speedChanged) Q_PROPERTY(int targetTime MEMBER m_targetTime NOTIFY targetTimeChanged) Q_PROPERTY(int forceValue MEMBER m_forceValue NOTIFY forceValueChanged) Q_PROPERTY(bool redLed MEMBER m_redLed NOTIFY redLedChanged) public slots: void startConnectToBluetooth(); void stopFindAvailableBluetooth(); void connectToBluetooth(); void disconnectToBluetooth(); void setStartStatus(bool value); void setRedLed(bool value); void setBody(bool value); void setTargetTime(int value); void setModuleSpeed(int value); void setReverSpeed(); void setForceValue(int value); signals: void startStatusChanged(bool value); void isBodyChanged(bool value); void speedChanged(int value); void targetTimeChanged(int value); void forceValueChanged(int value); void redLedChanged(bool value); void bluetoothFindedNewDevice(QString id, QString name, QString address); private: QBluetoothDeviceDiscoveryAgent * m_bluetoothDeviceDiscoveryAgent; QBluetoothServiceDiscoveryAgent * m_bluetoothServiceDiscoveryAgent; QBluetoothSocket *m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); QBluetoothServer* m_bluetoothServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); QLowEnergyController *m_lowEnergyController; QLowEnergyService *m_lowEnergyService; QBluetoothUuid m_bluetoothUuidService = QBluetoothUuid("{99999999-9999-c8c8-c8c8-d2d2d22d2d2d}"); QLowEnergyCharacteristic m_QLowEnergyCharacteristicWorkParameters; QLowEnergyCharacteristic m_QLowEnergyCharacteristicCycleSettings; QLowEnergyCharacteristic m_QLowEnergyCharacteristicWorkTime; QLowEnergyCharacteristic m_QLowEnergyCharacteristicFirmwareAndHardwareVersion; QTimer m_timerGetStateBluetooth; QVector<QBluetoothDeviceInfo> m_vectorBluetoothDevice; QVector<QBluetoothServiceInfo> m_vectorBluetoothService; QString m_idCurrentBluetoothDevice; private slots: void onBluetoothDeviceDiscovered(const QBluetoothDeviceInfo &info); void onBluetoothServiceDiscovered(const QBluetoothServiceInfo &info); void onBluetoothStateChanged(const QLowEnergyController::ControllerState &state); void onBluetoothConnected(); void onBluetoothDisconnected(); void onBluetoothError(QLowEnergyController::Error error); void onBleServiceDiscovered(const QBluetoothUuid &newService); void onBluetoothLowEnergyControllerStateChanged(QLowEnergyService::ServiceState newState); void onBluetoothLowEnergyControllerCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue); void onBluetoothLowEnergyControllerCharacteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue); void onBluetoothLowEnergyControllerDescriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue); void onBluetoothLowEnergyControllerCharacteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); void onBluetoothLowEnergyControllerDescriptorRead(const QLowEnergyDescriptor &descriptor, const QByteArray &value); void getStateBluetooth(); void setSpeed(int value); int byteArrayToInt(QByteArray arr); }; #endif // BLUETOOTH_H -
Hi!
I have custom class inheriting from QObject. How I can add this class to Q_PROPERTY?#ifndef BLUETOOTH_H #define BLUETOOTH_H #include <QObject> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QJsonValue> #include <QTime> #include <QTimer> #include <QBluetoothLocalDevice> #include <QBluetoothDeviceDiscoveryAgent> #include <QBluetoothDeviceInfo> #include <QBluetoothSocket> #include <QBluetoothServer> #include <QBluetoothServiceDiscoveryAgent> #include <QBluetoothLocalDevice> #include <QBluetoothUuid> #include <QBluetoothAddress> #include <QLowEnergyController> #include <QLowEnergyService> #include <QLowEnergyCharacteristic> #include "structures.h" class Bluetooth : public QObject { Q_OBJECT // Q_GADGET public: explicit Bluetooth(QObject *parent = nullptr); bool m_startStatus; bool m_isBody; int m_speed; int m_targetTime; int m_forceValue; bool m_redLed; Q_PROPERTY(bool startStatus MEMBER m_startStatus NOTIFY startStatusChanged) Q_PROPERTY(bool isBody MEMBER m_isBody NOTIFY isBodyChanged) Q_PROPERTY(int speed MEMBER m_speed NOTIFY speedChanged) Q_PROPERTY(int targetTime MEMBER m_targetTime NOTIFY targetTimeChanged) Q_PROPERTY(int forceValue MEMBER m_forceValue NOTIFY forceValueChanged) Q_PROPERTY(bool redLed MEMBER m_redLed NOTIFY redLedChanged) public slots: void startConnectToBluetooth(); void stopFindAvailableBluetooth(); void connectToBluetooth(); void disconnectToBluetooth(); void setStartStatus(bool value); void setRedLed(bool value); void setBody(bool value); void setTargetTime(int value); void setModuleSpeed(int value); void setReverSpeed(); void setForceValue(int value); signals: void startStatusChanged(bool value); void isBodyChanged(bool value); void speedChanged(int value); void targetTimeChanged(int value); void forceValueChanged(int value); void redLedChanged(bool value); void bluetoothFindedNewDevice(QString id, QString name, QString address); private: QBluetoothDeviceDiscoveryAgent * m_bluetoothDeviceDiscoveryAgent; QBluetoothServiceDiscoveryAgent * m_bluetoothServiceDiscoveryAgent; QBluetoothSocket *m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); QBluetoothServer* m_bluetoothServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); QLowEnergyController *m_lowEnergyController; QLowEnergyService *m_lowEnergyService; QBluetoothUuid m_bluetoothUuidService = QBluetoothUuid("{99999999-9999-c8c8-c8c8-d2d2d22d2d2d}"); QLowEnergyCharacteristic m_QLowEnergyCharacteristicWorkParameters; QLowEnergyCharacteristic m_QLowEnergyCharacteristicCycleSettings; QLowEnergyCharacteristic m_QLowEnergyCharacteristicWorkTime; QLowEnergyCharacteristic m_QLowEnergyCharacteristicFirmwareAndHardwareVersion; QTimer m_timerGetStateBluetooth; QVector<QBluetoothDeviceInfo> m_vectorBluetoothDevice; QVector<QBluetoothServiceInfo> m_vectorBluetoothService; QString m_idCurrentBluetoothDevice; private slots: void onBluetoothDeviceDiscovered(const QBluetoothDeviceInfo &info); void onBluetoothServiceDiscovered(const QBluetoothServiceInfo &info); void onBluetoothStateChanged(const QLowEnergyController::ControllerState &state); void onBluetoothConnected(); void onBluetoothDisconnected(); void onBluetoothError(QLowEnergyController::Error error); void onBleServiceDiscovered(const QBluetoothUuid &newService); void onBluetoothLowEnergyControllerStateChanged(QLowEnergyService::ServiceState newState); void onBluetoothLowEnergyControllerCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue); void onBluetoothLowEnergyControllerCharacteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue); void onBluetoothLowEnergyControllerDescriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue); void onBluetoothLowEnergyControllerCharacteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); void onBluetoothLowEnergyControllerDescriptorRead(const QLowEnergyDescriptor &descriptor, const QByteArray &value); void getStateBluetooth(); void setSpeed(int value); int byteArrayToInt(QByteArray arr); }; #endif // BLUETOOTH_H@Mihaill Like any other class. There are examples here: https://doc.qt.io/qt-6/properties.html
Q_PROPERTY(Bluetooth bluetooth READ getBluetooth WRITE setBluetooth)