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 connect SIGNAL to SLOT in same classe and also two differents classes
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 854 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.
  • J jawad_soft

    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 !

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #4

    @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);


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    4
    • J jawad_soft

      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 !

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @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!

      J.HilkJ 1 Reply Last reply
      0
      • JonBJ JonB

        @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!

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #6

        @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

        alt text

        I have at least 1 use case where the use of old macros is essential 😉
        Fringe case, but it exists


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        JonBJ 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @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

          alt text

          I have at least 1 use case where the use of old macros is essential 😉
          Fringe case, but it exists

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @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.HilkJ 1 Reply Last reply
          0
          • JonBJ JonB

            @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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #8

            @JonB Fair enough point I'm only objecting to the generalization, there's a reason why you don't get a whole bunch of deprecated warning when you use the old syntax.

            Even though you should, in my opinion at least.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jawad_soft
              wrote on last edited by
              #9

              @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.HilkJ 1 Reply Last reply
              0
              • J jawad_soft

                @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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #10

                @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) simply client 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


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • J Offline
                  J Offline
                  jawad_soft
                  wrote on last edited by
                  #11

                  @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 !?

                  1 Reply Last reply
                  0
                  • J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #12

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    J 1 Reply Last reply
                    4
                    • J.HilkJ J.Hilk

                      @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 Offline
                      J Offline
                      jawad_soft
                      wrote on last edited by
                      #13

                      @J-Hilk Great explanation ! I solved my problem and at the same time I learned more things.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved