Signal Slot not working
-
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 -
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()))); -
@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.
-
@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.
@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"
-
I created a custom signal slot . It is working in this class.
No I am not using a thread
-
I created a custom signal slot . It is working in this class.
No I am not using a thread
Ok so what exactly is not working ?
-
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 -
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 connectionOk, might be a bug or something im not seeing in the code.
You can check https://bugreports.qt.ioIs 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.
-
@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.
-
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@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);
-
@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.
@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