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. Signal Slot not working
Forum Updated to NodeBB v4.3 + New Features

Signal Slot not working

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Avtansh Sharma
    wrote on last edited by VRonin
    #1

    HI,

    header

    #ifndef DEVICEDATA_H
    #define DEVICEDATA_H
    #include<QString>
    
    #include<QtSql/QSqlDatabase>
    #include<QSqlQuery>
    #include<QSqlError>
    #include <QString>
    #include <QObject>
    #include <QtBluetooth/QBluetoothSocket>
    QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
    
    QT_USE_NAMESPACE
    
    
    
    class DeviceData : public QObject
    {
        Q_OBJECT
    
    
    
    
    public:
        explicit DeviceData(QObject *parent=0);
        ~DeviceData();
        int SaveData(QString s_Data,QString Str_Sensor);
        QList<QString> getData(QString tbl_sensor);
        int syncDevice(bool b_terminate);
        int syncDeviceData(bool b_terminate);
        bool m_bterminate;
        void stopClient();
    
    
    private:
        QSqlDatabase m_db;
        QString getMax(QString Str_Table);
    
        //QString ReadData();
    QBluetoothSocket *m_bluetoothSocket;
    signals:
        void messageReceived(const QString &sender, const QString &message);
        void connected(const QString &name);
        void disconnected();
    
    private slots:
        void readSocket();
        void connected();
    public slots:
        void sendMessage(const QString &message);
    
    
    
    };
    
    #endif // DEVICEDATA_H
    

    cpp file

    #include <devicedata.h>
    #include <QDebug>
    #include <QDateTime>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/rfcomm.h>
    #include <QDebug>
    #include <devicedata.h>
    #include <QtBluetooth/QBluetoothSocket>
    /*
     *
     *
     *
     *
    sqllite3 db_Health.db;
    
    create table tbl_BodyTemperature
    (
    c_id primarykey integer,
    c_Temperature text,
    c_TimeStamp text
    );
    
    create table tbl_HeartSensor
    (
    c_id integer,
    c_HeartBeatPulse integer,
    c_TimeStamp text
    );
    
    create table tbl_BeatInterval
    (
    c_id primarykey integer,
    c_BeatInterval text,
    c_TimeStamp text
    );
    *
    *
    *
    **/
    
    
    DeviceData::DeviceData(QObject *parent)
    :   QObject(parent), m_bluetoothSocket(0)
    {
        m_bterminate=false;
      
    }
    
    DeviceData::~DeviceData()
    {
        stopClient();
    }
    
    
    
    void DeviceData::stopClient()
    {
        delete m_bluetoothSocket;
        m_bluetoothSocket = 0;
    }
    //! [stopClient]
    
    //! [readSocket]
    void DeviceData::readSocket()
    {
        if (!m_bluetoothSocket)
            return;
    
        while (1||m_bluetoothSocket->canReadLine()) {
            QByteArray line = m_bluetoothSocket->readLine();
            qInfo()<< QString::fromUtf8(line.constData(), line.length());
            emit messageReceived(m_bluetoothSocket->peerName(),
                                QString::fromUtf8(line.constData(), line.length()));
        }
    }
    //! [readSocket]
    
    //! [sendMessage]
    void DeviceData::sendMessage(const QString &message)
    {
        QByteArray text = message.toUtf8() + '\n';
        m_bluetoothSocket->write(text);
    }
    //! [sendMessage]
    
    //! [connected]
    void DeviceData::connected()
    {
        emit connected(m_bluetoothSocket->peerName());
    }
    /*
     * DeviceData::DeviceData()
    {
    
    }
    */
    int DeviceData::syncDeviceData(bool b_terminate)
    {
    
            m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
    
    
        connect(m_bluetoothSocket, SIGNAL(readyReadn()), this, SLOT(readSocket()));
        connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected()));
        connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
    
        m_bluetoothSocket->connectToService((QBluetoothAddress)"B8:27:EB:54:81:BE",(quint16)1);
    
      //  qInfo()<<m_bluetoothSocket->canReadLine();
    
      //  connect(m_bluetoothSocket, SIGNAL(), this, SIGNAL(disconnected()));
    
    return 0;
    
    }
    
    int DeviceData::syncDevice(bool b_terminate)
    {
    syncDeviceData(b_terminate);
        return 0;
    
    }
    
    

    Regards,
    Avtansh Sharma

    VRoninV 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      check if connects fails
      Q_ASSUME ( connect(m_bluetoothSocket, SIGNAL(readyReadn()), this, SLOT(readSocket())) );
      Q_ASSUME ( connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected())));
      Q_ASSUME ( connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SIGNAL(disconnected())));

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Avtansh Sharma
        wrote on last edited by
        #3

        @mrjj said in Signal Slot not working:

        Q_ASSUME

        No difference.

        Connect fails if i change the names of slot. Hence correct connections are being done.

        On server side I am getting a notification that a client is connected.

        mrjjM 1 Reply Last reply
        0
        • A Avtansh Sharma

          @mrjj said in Signal Slot not working:

          Q_ASSUME

          No difference.

          Connect fails if i change the names of slot. Hence correct connections are being done.

          On server side I am getting a notification that a client is connected.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Avtansh-Sharma
          Do you run it in a thread ?
          If not, maybe your while loop strangulate the event loop.

          Anyway, i think you will need to use the debugger to find the cause of "not working"

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Avtansh Sharma
            wrote on last edited by
            #5

            I created a custom signal slot . It is working in this class.

            No I am not using a thread

            mrjjM 1 Reply Last reply
            0
            • A Avtansh Sharma

              I created a custom signal slot . It is working in this class.

              No I am not using a thread

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Avtansh-Sharma

              Ok so what exactly is not working ?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Avtansh Sharma
                wrote on last edited by
                #7

                The signal slot mechanism of QBluetooth
                Socket is not working
                readSocket,connected are never getting called even after server says a client has made a connection

                mrjjM 1 Reply Last reply
                0
                • A Avtansh Sharma

                  The signal slot mechanism of QBluetooth
                  Socket is not working
                  readSocket,connected are never getting called even after server says a client has made a connection

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Avtansh-Sharma

                  Ok, might be a bug or something im not seeing in the code.
                  You can check https://bugreports.qt.io

                  Is other signals being sent ? like disconnected ?

                  make sure all connects are using Q_ASSUME so its not something trivial.

                  There is nothing that spring to mind. Maybe others can spot something.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Avtansh Sharma
                    wrote on last edited by Avtansh Sharma
                    #9

                    @mrjj Thanks for help.

                    I remember you were saying something about thread can you please explain int detail. I will be using thread anyway in future. Maybe it will work in thread.

                    mrjjM 1 Reply Last reply
                    0
                    • A Avtansh Sharma

                      HI,

                      header

                      #ifndef DEVICEDATA_H
                      #define DEVICEDATA_H
                      #include<QString>
                      
                      #include<QtSql/QSqlDatabase>
                      #include<QSqlQuery>
                      #include<QSqlError>
                      #include <QString>
                      #include <QObject>
                      #include <QtBluetooth/QBluetoothSocket>
                      QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
                      
                      QT_USE_NAMESPACE
                      
                      
                      
                      class DeviceData : public QObject
                      {
                          Q_OBJECT
                      
                      
                      
                      
                      public:
                          explicit DeviceData(QObject *parent=0);
                          ~DeviceData();
                          int SaveData(QString s_Data,QString Str_Sensor);
                          QList<QString> getData(QString tbl_sensor);
                          int syncDevice(bool b_terminate);
                          int syncDeviceData(bool b_terminate);
                          bool m_bterminate;
                          void stopClient();
                      
                      
                      private:
                          QSqlDatabase m_db;
                          QString getMax(QString Str_Table);
                      
                          //QString ReadData();
                      QBluetoothSocket *m_bluetoothSocket;
                      signals:
                          void messageReceived(const QString &sender, const QString &message);
                          void connected(const QString &name);
                          void disconnected();
                      
                      private slots:
                          void readSocket();
                          void connected();
                      public slots:
                          void sendMessage(const QString &message);
                      
                      
                      
                      };
                      
                      #endif // DEVICEDATA_H
                      

                      cpp file

                      #include <devicedata.h>
                      #include <QDebug>
                      #include <QDateTime>
                      #include <stdio.h>
                      #include <unistd.h>
                      #include <sys/socket.h>
                      #include <bluetooth/bluetooth.h>
                      #include <bluetooth/rfcomm.h>
                      #include <QDebug>
                      #include <devicedata.h>
                      #include <QtBluetooth/QBluetoothSocket>
                      /*
                       *
                       *
                       *
                       *
                      sqllite3 db_Health.db;
                      
                      create table tbl_BodyTemperature
                      (
                      c_id primarykey integer,
                      c_Temperature text,
                      c_TimeStamp text
                      );
                      
                      create table tbl_HeartSensor
                      (
                      c_id integer,
                      c_HeartBeatPulse integer,
                      c_TimeStamp text
                      );
                      
                      create table tbl_BeatInterval
                      (
                      c_id primarykey integer,
                      c_BeatInterval text,
                      c_TimeStamp text
                      );
                      *
                      *
                      *
                      **/
                      
                      
                      DeviceData::DeviceData(QObject *parent)
                      :   QObject(parent), m_bluetoothSocket(0)
                      {
                          m_bterminate=false;
                        
                      }
                      
                      DeviceData::~DeviceData()
                      {
                          stopClient();
                      }
                      
                      
                      
                      void DeviceData::stopClient()
                      {
                          delete m_bluetoothSocket;
                          m_bluetoothSocket = 0;
                      }
                      //! [stopClient]
                      
                      //! [readSocket]
                      void DeviceData::readSocket()
                      {
                          if (!m_bluetoothSocket)
                              return;
                      
                          while (1||m_bluetoothSocket->canReadLine()) {
                              QByteArray line = m_bluetoothSocket->readLine();
                              qInfo()<< QString::fromUtf8(line.constData(), line.length());
                              emit messageReceived(m_bluetoothSocket->peerName(),
                                                  QString::fromUtf8(line.constData(), line.length()));
                          }
                      }
                      //! [readSocket]
                      
                      //! [sendMessage]
                      void DeviceData::sendMessage(const QString &message)
                      {
                          QByteArray text = message.toUtf8() + '\n';
                          m_bluetoothSocket->write(text);
                      }
                      //! [sendMessage]
                      
                      //! [connected]
                      void DeviceData::connected()
                      {
                          emit connected(m_bluetoothSocket->peerName());
                      }
                      /*
                       * DeviceData::DeviceData()
                      {
                      
                      }
                      */
                      int DeviceData::syncDeviceData(bool b_terminate)
                      {
                      
                              m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
                      
                      
                          connect(m_bluetoothSocket, SIGNAL(readyReadn()), this, SLOT(readSocket()));
                          connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected()));
                          connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
                      
                          m_bluetoothSocket->connectToService((QBluetoothAddress)"B8:27:EB:54:81:BE",(quint16)1);
                      
                        //  qInfo()<<m_bluetoothSocket->canReadLine();
                      
                        //  connect(m_bluetoothSocket, SIGNAL(), this, SIGNAL(disconnected()));
                      
                      return 0;
                      
                      }
                      
                      int DeviceData::syncDevice(bool b_terminate)
                      {
                      syncDeviceData(b_terminate);
                          return 0;
                      
                      }
                      
                      

                      Regards,
                      Avtansh Sharma

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @Avtansh-Sharma said in Signal Slot not working:

                      connect(m_bluetoothSocket, SIGNAL(readyReadn())

                      Typo? should be connect(m_bluetoothSocket, SIGNAL(readyRead()) or, better, using Qt5 syntax: connect(m_bluetoothSocket,&QBluetoothSocket::readyRead,this,&DeviceData::readSocket);

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      2
                      • A Avtansh Sharma

                        @mrjj Thanks for help.

                        I remember you were saying something about thread can you please explain int detail. I will be using thread anyway in future. Maybe it will work in thread.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Avtansh-Sharma
                        Hi
                        Using a thread will most likely not make any difference with the current issue but
                        when you use signals from inside a thread , it's sometimes necessary to add Qt::QueuedConnection
                        to the connect statement.
                        https://woboq.com/blog/how-qt-signals-slots-work-part3-queuedconnection.html

                        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