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. Can't get signal from custom class in Q_PROPERTY
Forum Updated to NodeBB v4.3 + New Features

Can't get signal from custom class in Q_PROPERTY

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 211 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 Mihaill
    #1

    Hi!
    I have custom class Bluetooth.

    #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);
        ~Bluetooth();
        bool operator != (const Bluetooth &bluetooth) {return m_bluetoothUuidService != bluetooth.m_bluetoothUuidService;}
        bool operator = (const Bluetooth &bluetooth) {return this;}
    
    
        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
    
    

    In main class I add Bluetooth to Q_PROPERTY:

    Q_PROPERTY(Bluetooth bluetooth   MEMBER m_bluetooth NOTIFY bluetoothChanged)
    

    In Bluetooth i emit signal:

            setProperty("speed", m_speed);
            emit speedChanged(m_speed);
    

    But in QML I dont get this signal:

    
        Connections {
            target: AppCore.bluetooth
    
            function onSpeedChanged(speed) {
                console.log("*********************************", speed)
          }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mihaill
      wrote on last edited by
      #2

      Iadd in main and then it worked:

      qRegisterMetaType<Bluetooth>("Bluetooth");
      and
      engine.rootContext()->setContextProperty("bluetooth", &appCore->m_bluetooth);
      
      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