Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add custom class to Q_PROPERTY
Forum Updated to NodeBB v4.3 + New Features

Add custom class to Q_PROPERTY

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 623 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihaill
    wrote on last edited by
    #1

    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
    
    
    jsulmJ 1 Reply Last reply
    0
    • M Mihaill

      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
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mihaill
        wrote on last edited by
        #3

        I add operators !=, = and it's works

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved