jayen marco
jayen marco

Reputation: 103

Can't connect SIGNAL to SLOT in same classe and also two differents classes QT

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

Upvotes: 0

Views: 651

Answers (2)

G.M.
G.M.

Reputation: 12889

That's because the ack_gui signal is a member of your UDPServer class not QUdpSocket.

Use the new signal/slot syntax...

connect(this, &UDPServer::ack_gui, this, &UDPServer::ackRead);

Upvotes: 3

Roman Pavelka
Roman Pavelka

Reputation: 4181

  1. Do not use the old signal/slot connection, you will get better compile time warnings with the new one: https://wiki.qt.io/New_Signal_Slot_Syntax

  2. You are connecting QUdpSocket to your UDPServer. Your UdpServer has ack_gui signal but QUdpSocket has not!

Upvotes: 1

Related Questions