Can't connect SIGNAL to SLOT in same classe and also two differents classes
-
I am developing an QT application under QT5.13, and I am trying to connect a signal with a slot of the same class because the end goal was to connect the signal of this class with the slot of a seconde class but it doesn't work in both cases and so I tried to validate it in the same class before going to the second step.
There is my code :
udpserver.h :
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QUdpSocket> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class UDPServer : public QObject { Q_OBJECT public: explicit UDPServer(QObject *parent = nullptr); QUdpSocket *getSocket() const; void Send(QString d); void pMsg(QByteArray App_Msg); signals: void ack_gui(QString ack_msg); public slots: void readyRead(); void ackRead(QString _ack_msg); private: QUdpSocket *socket; }; #endif // UDPSERVER_H
udpserver.cpp:
#include "udpserver.h" UDPServer::UDPServer(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QTextStream(stdout) << "Socket Server created ! " << endl; socket->bind(QHostAddress::LocalHost, QT_SERVER_PORT); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString))); } QUdpSocket *UDPServer::getSocket() const { return socket; } void UDPServer::SendData(QString d) { keyprod prod1; QByteArray Data; QJsonObject Js_command = prod1.ObjectFromString(d); Data.append(d); socket->writeDatagram(Data, QHostAddress::LocalHost, PYTHON_SERVER_PORT); qDebug() << "catch! " << endl; } void UDPServer::pMsg(QByteArray App_Msg) { QJsonDocument JsonDocument = QJsonDocument::fromJson(App_Msg); QJsonObject JsonApp_Msg = JsonDocument.object(); QString Typo = JsonApp_Msg["no"].toString(); emit ack_gui(Typo); } void UDPServer::readyRead() { QByteArray buffer; buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); pMsg(buffer); } void UDPServer::ackRead(QString _ack_msg) { qDebug() << "Message : " << _ack_msg; }
As you can see it is a very classic class nothing very complicated in QT,the first connects works, but the second where I use my personal signal does not work :
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
The code are compiled, but when i lunch it, i got this :
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
I also tried the new connect syntax in QT5 but it didn't work.
I know this topic has been touched on several times already but i have already checked these topics and still haven't found a solution can you help me please.
Thank you in advance for your help !
-
@jawad_soft
ok we can fix that because client is stack allocated not heap and a local variable,BUT
thats only fix of the bug here's an underlying bigger issue, client should probably be a class member, because it will be destroyed, as soon as the constructor exits!
The "Fix"
connect(&client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
i Suppose with new syntaxe you can't have slots with same name and differents arguments haha
of course you can thats what
qOverload
is for :D
https://doc.qt.io/qt-5/qtglobal.html#qOverload -
I am developing an QT application under QT5.13, and I am trying to connect a signal with a slot of the same class because the end goal was to connect the signal of this class with the slot of a seconde class but it doesn't work in both cases and so I tried to validate it in the same class before going to the second step.
There is my code :
udpserver.h :
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QUdpSocket> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class UDPServer : public QObject { Q_OBJECT public: explicit UDPServer(QObject *parent = nullptr); QUdpSocket *getSocket() const; void Send(QString d); void pMsg(QByteArray App_Msg); signals: void ack_gui(QString ack_msg); public slots: void readyRead(); void ackRead(QString _ack_msg); private: QUdpSocket *socket; }; #endif // UDPSERVER_H
udpserver.cpp:
#include "udpserver.h" UDPServer::UDPServer(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QTextStream(stdout) << "Socket Server created ! " << endl; socket->bind(QHostAddress::LocalHost, QT_SERVER_PORT); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString))); } QUdpSocket *UDPServer::getSocket() const { return socket; } void UDPServer::SendData(QString d) { keyprod prod1; QByteArray Data; QJsonObject Js_command = prod1.ObjectFromString(d); Data.append(d); socket->writeDatagram(Data, QHostAddress::LocalHost, PYTHON_SERVER_PORT); qDebug() << "catch! " << endl; } void UDPServer::pMsg(QByteArray App_Msg) { QJsonDocument JsonDocument = QJsonDocument::fromJson(App_Msg); QJsonObject JsonApp_Msg = JsonDocument.object(); QString Typo = JsonApp_Msg["no"].toString(); emit ack_gui(Typo); } void UDPServer::readyRead() { QByteArray buffer; buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); pMsg(buffer); } void UDPServer::ackRead(QString _ack_msg) { qDebug() << "Message : " << _ack_msg; }
As you can see it is a very classic class nothing very complicated in QT,the first connects works, but the second where I use my personal signal does not work :
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
The code are compiled, but when i lunch it, i got this :
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
I also tried the new connect syntax in QT5 but it didn't work.
I know this topic has been touched on several times already but i have already checked these topics and still haven't found a solution can you help me please.
Thank you in advance for your help !
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
Isn't this error clear? There is no such signal in QUdpSocket.
Don't you want this actually:connect(this, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
-
I am developing an QT application under QT5.13, and I am trying to connect a signal with a slot of the same class because the end goal was to connect the signal of this class with the slot of a seconde class but it doesn't work in both cases and so I tried to validate it in the same class before going to the second step.
There is my code :
udpserver.h :
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QUdpSocket> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class UDPServer : public QObject { Q_OBJECT public: explicit UDPServer(QObject *parent = nullptr); QUdpSocket *getSocket() const; void Send(QString d); void pMsg(QByteArray App_Msg); signals: void ack_gui(QString ack_msg); public slots: void readyRead(); void ackRead(QString _ack_msg); private: QUdpSocket *socket; }; #endif // UDPSERVER_H
udpserver.cpp:
#include "udpserver.h" UDPServer::UDPServer(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QTextStream(stdout) << "Socket Server created ! " << endl; socket->bind(QHostAddress::LocalHost, QT_SERVER_PORT); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString))); } QUdpSocket *UDPServer::getSocket() const { return socket; } void UDPServer::SendData(QString d) { keyprod prod1; QByteArray Data; QJsonObject Js_command = prod1.ObjectFromString(d); Data.append(d); socket->writeDatagram(Data, QHostAddress::LocalHost, PYTHON_SERVER_PORT); qDebug() << "catch! " << endl; } void UDPServer::pMsg(QByteArray App_Msg) { QJsonDocument JsonDocument = QJsonDocument::fromJson(App_Msg); QJsonObject JsonApp_Msg = JsonDocument.object(); QString Typo = JsonApp_Msg["no"].toString(); emit ack_gui(Typo); } void UDPServer::readyRead() { QByteArray buffer; buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); pMsg(buffer); } void UDPServer::ackRead(QString _ack_msg) { qDebug() << "Message : " << _ack_msg; }
As you can see it is a very classic class nothing very complicated in QT,the first connects works, but the second where I use my personal signal does not work :
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
The code are compiled, but when i lunch it, i got this :
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
I also tried the new connect syntax in QT5 but it didn't work.
I know this topic has been touched on several times already but i have already checked these topics and still haven't found a solution can you help me please.
Thank you in advance for your help !
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
QUdpSocket class doesn't have a
ack_gui
signal.
You might want changesocket
tothis
?Edit: too slow :)
-
I am developing an QT application under QT5.13, and I am trying to connect a signal with a slot of the same class because the end goal was to connect the signal of this class with the slot of a seconde class but it doesn't work in both cases and so I tried to validate it in the same class before going to the second step.
There is my code :
udpserver.h :
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QUdpSocket> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class UDPServer : public QObject { Q_OBJECT public: explicit UDPServer(QObject *parent = nullptr); QUdpSocket *getSocket() const; void Send(QString d); void pMsg(QByteArray App_Msg); signals: void ack_gui(QString ack_msg); public slots: void readyRead(); void ackRead(QString _ack_msg); private: QUdpSocket *socket; }; #endif // UDPSERVER_H
udpserver.cpp:
#include "udpserver.h" UDPServer::UDPServer(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QTextStream(stdout) << "Socket Server created ! " << endl; socket->bind(QHostAddress::LocalHost, QT_SERVER_PORT); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString))); } QUdpSocket *UDPServer::getSocket() const { return socket; } void UDPServer::SendData(QString d) { keyprod prod1; QByteArray Data; QJsonObject Js_command = prod1.ObjectFromString(d); Data.append(d); socket->writeDatagram(Data, QHostAddress::LocalHost, PYTHON_SERVER_PORT); qDebug() << "catch! " << endl; } void UDPServer::pMsg(QByteArray App_Msg) { QJsonDocument JsonDocument = QJsonDocument::fromJson(App_Msg); QJsonObject JsonApp_Msg = JsonDocument.object(); QString Typo = JsonApp_Msg["no"].toString(); emit ack_gui(Typo); } void UDPServer::readyRead() { QByteArray buffer; buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); pMsg(buffer); } void UDPServer::ackRead(QString _ack_msg) { qDebug() << "Message : " << _ack_msg; }
As you can see it is a very classic class nothing very complicated in QT,the first connects works, but the second where I use my personal signal does not work :
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
The code are compiled, but when i lunch it, i got this :
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
I also tried the new connect syntax in QT5 but it didn't work.
I know this topic has been touched on several times already but i have already checked these topics and still haven't found a solution can you help me please.
Thank you in advance for your help !
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
this
connect(this, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));or (better)
connect(this, &UDPServer::ack_gui, this, &UDPServer::ackRead); -
I am developing an QT application under QT5.13, and I am trying to connect a signal with a slot of the same class because the end goal was to connect the signal of this class with the slot of a seconde class but it doesn't work in both cases and so I tried to validate it in the same class before going to the second step.
There is my code :
udpserver.h :
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QObject> #include <QUdpSocket> #include <QDebug> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class UDPServer : public QObject { Q_OBJECT public: explicit UDPServer(QObject *parent = nullptr); QUdpSocket *getSocket() const; void Send(QString d); void pMsg(QByteArray App_Msg); signals: void ack_gui(QString ack_msg); public slots: void readyRead(); void ackRead(QString _ack_msg); private: QUdpSocket *socket; }; #endif // UDPSERVER_H
udpserver.cpp:
#include "udpserver.h" UDPServer::UDPServer(QObject *parent) : QObject(parent) { socket = new QUdpSocket(this); QTextStream(stdout) << "Socket Server created ! " << endl; socket->bind(QHostAddress::LocalHost, QT_SERVER_PORT); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString))); } QUdpSocket *UDPServer::getSocket() const { return socket; } void UDPServer::SendData(QString d) { keyprod prod1; QByteArray Data; QJsonObject Js_command = prod1.ObjectFromString(d); Data.append(d); socket->writeDatagram(Data, QHostAddress::LocalHost, PYTHON_SERVER_PORT); qDebug() << "catch! " << endl; } void UDPServer::pMsg(QByteArray App_Msg) { QJsonDocument JsonDocument = QJsonDocument::fromJson(App_Msg); QJsonObject JsonApp_Msg = JsonDocument.object(); QString Typo = JsonApp_Msg["no"].toString(); emit ack_gui(Typo); } void UDPServer::readyRead() { QByteArray buffer; buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); pMsg(buffer); } void UDPServer::ackRead(QString _ack_msg) { qDebug() << "Message : " << _ack_msg; }
As you can see it is a very classic class nothing very complicated in QT,the first connects works, but the second where I use my personal signal does not work :
connect(socket, SIGNAL(ack_gui(QString)), this, SLOT(ackRead(QString)));
The code are compiled, but when i lunch it, i got this :
QObject::connect: No such signal QUdpSocket::ack_gui(QString) in ../../udpserver.cpp
I also tried the new connect syntax in QT5 but it didn't work.
I know this topic has been touched on several times already but i have already checked these topics and still haven't found a solution can you help me please.
Thank you in advance for your help !
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
I also tried the new connect syntax in QT5 but it didn't work.
As others have said: please change over to the new-style syntax always! No need to ever use
SIGNAL
/SLOT()
macros. If the new-style "doesn't work", at least it's giving you compile-time help on why not, which you must address! -
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
I also tried the new connect syntax in QT5 but it didn't work.
As others have said: please change over to the new-style syntax always! No need to ever use
SIGNAL
/SLOT()
macros. If the new-style "doesn't work", at least it's giving you compile-time help on why not, which you must address!@JonB said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
No need to ever use SIGNAL/SLOT() macros
I have at least 1 use case where the use of old macros is essential 😉
Fringe case, but it exists -
@JonB said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
No need to ever use SIGNAL/SLOT() macros
I have at least 1 use case where the use of old macros is essential 😉
Fringe case, but it exists -
@J-Hilk
Of course I respect your comment/case. However, do you think this is good advice for the OP here? Or do you think he would be better advised not using this as an excuse to stay with old-style but would be better using new-style for all his cases? -
@J-Hilk @raven-worx ! Thanks for reply, effectively it works better, I don't see the connection failure message, but what about the arguments of signal and slot ? :D I will even take the opportunity to use the new syntax.
my goal was to connect the signal ack_gui that i created in this class with the slot of my classe mainwindows class, should i integrate it this way :
mainwindows .h :
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QApplication> #include <QDesktopWidget> #include <QObject> #include <QWidget> #include <QLabel> #include <QTextStream> #include <QTimer> #include <QLoggingCategory> #include <QGraphicsView> #include <QGraphicsRectItem> #include <QVariantAnimation> #include <QGraphicsScene> #include <QGraphicsProxyWidget> #include "udpserver.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT private: QColor menuButtonColor; Ui::MainWindow *m_ui; QPushButton *m_Button; void init(); private slots: void onLaunchButton(QString ackMsg); void onLaunchButton(); protected: bool event(QEvent *event) override; public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow() override; public slots: void update_data(); }; #endif // MAINWINDOW_H
mainwindows .cpp :
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent, Qt::FramelessWindowHint), m_ui(new Ui::MainWindow) { UDPServer client; QObject::connect(m_Button, SIGNAL(clicked()),this, SLOT(onLaunchButton())); connect(client.getSocket(), &UDPServer::ack_gui, this, &MainWindow::onLaunchButton(QString)); }
With function client.getSocket() i will get socket private attribut of UDPServer , I feel like I'm going to make the same error again how I refer to the UDP socket class from the class MainWindow ?
Thank you again for your help
-
@J-Hilk @raven-worx ! Thanks for reply, effectively it works better, I don't see the connection failure message, but what about the arguments of signal and slot ? :D I will even take the opportunity to use the new syntax.
my goal was to connect the signal ack_gui that i created in this class with the slot of my classe mainwindows class, should i integrate it this way :
mainwindows .h :
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QApplication> #include <QDesktopWidget> #include <QObject> #include <QWidget> #include <QLabel> #include <QTextStream> #include <QTimer> #include <QLoggingCategory> #include <QGraphicsView> #include <QGraphicsRectItem> #include <QVariantAnimation> #include <QGraphicsScene> #include <QGraphicsProxyWidget> #include "udpserver.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT private: QColor menuButtonColor; Ui::MainWindow *m_ui; QPushButton *m_Button; void init(); private slots: void onLaunchButton(QString ackMsg); void onLaunchButton(); protected: bool event(QEvent *event) override; public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow() override; public slots: void update_data(); }; #endif // MAINWINDOW_H
mainwindows .cpp :
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent, Qt::FramelessWindowHint), m_ui(new Ui::MainWindow) { UDPServer client; QObject::connect(m_Button, SIGNAL(clicked()),this, SLOT(onLaunchButton())); connect(client.getSocket(), &UDPServer::ack_gui, this, &MainWindow::onLaunchButton(QString)); }
With function client.getSocket() i will get socket private attribut of UDPServer , I feel like I'm going to make the same error again how I refer to the UDP socket class from the class MainWindow ?
Thank you again for your help
@jawad_soft said in Can't connect SIGNAL to SLOT in same classe and also two differents classes:
With function client.getSocket() i will get socket private attribut of UDPServer , I feel like I'm going to make the same error again how I refer to the UDP socket class from the class MainWindow ?
correct,
UDPServer::ack_gui
is part of the UDPServer class and your instance of that class is (apparently) simplyclient
therefore:connect(client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
Edit:
but you have to drop the QString argument, the new syntax does not require / allow that -
@J-Hilk ! Ok , i Suppose with new syntaxe you can't have slots with same name and differents arguments haha !!
I got error when i implement the line :connect(client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
Errore =>
no matching member function for call to 'connect'
I try to add this :
QObject::connect(client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
But nothing change !?
-
@jawad_soft
ok we can fix that because client is stack allocated not heap and a local variable,BUT
thats only fix of the bug here's an underlying bigger issue, client should probably be a class member, because it will be destroyed, as soon as the constructor exits!
The "Fix"
connect(&client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
i Suppose with new syntaxe you can't have slots with same name and differents arguments haha
of course you can thats what
qOverload
is for :D
https://doc.qt.io/qt-5/qtglobal.html#qOverload -
@jawad_soft
ok we can fix that because client is stack allocated not heap and a local variable,BUT
thats only fix of the bug here's an underlying bigger issue, client should probably be a class member, because it will be destroyed, as soon as the constructor exits!
The "Fix"
connect(&client, &UDPServer::ack_gui, this, &MainWindow::onLaunchButton);
i Suppose with new syntaxe you can't have slots with same name and differents arguments haha
of course you can thats what
qOverload
is for :D
https://doc.qt.io/qt-5/qtglobal.html#qOverload@J-Hilk Great explanation ! I solved my problem and at the same time I learned more things.