Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A UDP socket send/receive.
Qt 6.12 LTS is out! See what's new in the release blog

A UDP socket send/receive.

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 4 Posters 11.0k Views
  • 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.
  • jsulmJ jsulm

    @jenya7 said in A UDP socket send/receive.:

    Now what signal do I emit?

    One you need to define in your MyUDP class as I wrote before:

    class MyUDP...
    {
    signals:
        void showMessage(const QString& msg);
    ...
    void MyUDP::ReadyRead()
    {
       ...
       showMessage(QString(buffer));
    

    Please read https://doc.qt.io/qt-5/signalsandslots.html

    J Offline
    J Offline
    jenya7
    wrote on last edited by
    #12

    @jsulm
    Wow. That's cool. And I can implement showMessage(const QString& msg); in any file?
    Say ui->textEditNetRX->append(msg); is available in "mainwindow.cpp.

    jsulmJ 1 Reply Last reply
    0
    • J jenya7

      @jsulm
      Wow. That's cool. And I can implement showMessage(const QString& msg); in any file?
      Say ui->textEditNetRX->append(msg); is available in "mainwindow.cpp.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #13

      @jenya7 said in A UDP socket send/receive.:

      in any file?

      Yes, just add a slot where you need it and connect it to the signal.
      Signals/slots is a very powerful concept and it also helps to decouple classes from each other. For example your MyUDP class does not need to know who connects to its signal, it just emits the signal. You can connect 0..n slots to a signal, you can connect a signal to 0..n other signals.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      2
      • jsulmJ jsulm

        @jenya7 said in A UDP socket send/receive.:

        in any file?

        Yes, just add a slot where you need it and connect it to the signal.
        Signals/slots is a very powerful concept and it also helps to decouple classes from each other. For example your MyUDP class does not need to know who connects to its signal, it just emits the signal. You can connect 0..n slots to a signal, you can connect a signal to 0..n other signals.

        J Offline
        J Offline
        jenya7
        wrote on last edited by
        #14

        @jsulm
        Thank you very much.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jenya7
          wrote on last edited by
          #15

          In mainwindow.h I created a slot

          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
              MyUDP udp;
          
          public slots:
              void ShowUdpMessage(const QString& msg, const QByteArray data);
          //and so on...
          

          in mainwindow.cpp

          void MainWindow::ShowUdpMessage(const QString& msg, const QByteArray data)
          {
              QByteArray arr = data;
              ui->textEditNetRX->append(msg);
          }
          

          now in main.cpp

          
          MainWindow w;
           w.show();
          
          QObject::connect(&???, &???, &w, &MainWindow::ShowUdpMessage);
          

          How do I tie the signal source?
          The signal generates by MyUDP udp that resides in the MainWindow class.

          jsulmJ 1 Reply Last reply
          0
          • J jenya7

            In mainwindow.h I created a slot

            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
                MyUDP udp;
            
            public slots:
                void ShowUdpMessage(const QString& msg, const QByteArray data);
            //and so on...
            

            in mainwindow.cpp

            void MainWindow::ShowUdpMessage(const QString& msg, const QByteArray data)
            {
                QByteArray arr = data;
                ui->textEditNetRX->append(msg);
            }
            

            now in main.cpp

            
            MainWindow w;
             w.show();
            
            QObject::connect(&???, &???, &w, &MainWindow::ShowUdpMessage);
            

            How do I tie the signal source?
            The signal generates by MyUDP udp that resides in the MainWindow class.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #16

            @jenya7

            QObject::connect(&udp, &MyUDP::showMessage, &w, &MainWindow::ShowUdpMessage);
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 1 Reply Last reply
            1
            • jsulmJ jsulm

              @jenya7

              QObject::connect(&udp, &MyUDP::showMessage, &w, &MainWindow::ShowUdpMessage);
              
              J Offline
              J Offline
              jenya7
              wrote on last edited by
              #17

              @jsulm
              QObject::connect(&udp, &MyUDP::showMessage, &w, &MainWindow::ShowUdpMessage);
              &udp - is not visible in the scope. also MainWindow::udp <- udp not recognized although it declared public:MyUDP udp; in MainWindow.

              jsulmJ 1 Reply Last reply
              0
              • J jenya7

                @jsulm
                QObject::connect(&udp, &MyUDP::showMessage, &w, &MainWindow::ShowUdpMessage);
                &udp - is not visible in the scope. also MainWindow::udp <- udp not recognized although it declared public:MyUDP udp; in MainWindow.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #18

                @jenya7 You should do the connection inside MainWindow (for example in its constructor), because MainWindow needs the signal and it also has the MyUDP instance...

                "although it declared public:MyUDP udp" - don't declare it public, just do the connection inside MainWindow.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • J Offline
                  J Offline
                  jenya7
                  wrote on last edited by jenya7
                  #19

                  I try in the constructor

                  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                      ui->tabWidget->setCurrentIndex(0);
                      ui->textEditTerminalTx->installEventFilter(this);
                      
                       QObject::connect(&udp, &MyUDP::ShowUdpMessage, &MainWindow, &MainWindow::ShowUdpMessage);
                  }
                  

                  third argument - &MainWindow <- 'MainWindow' does not refer to a value.

                  JonBJ 1 Reply Last reply
                  0
                  • J jenya7

                    I try in the constructor

                    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        ui->tabWidget->setCurrentIndex(0);
                        ui->textEditTerminalTx->installEventFilter(this);
                        
                         QObject::connect(&udp, &MyUDP::ShowUdpMessage, &MainWindow, &MainWindow::ShowUdpMessage);
                    }
                    

                    third argument - &MainWindow <- 'MainWindow' does not refer to a value.

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

                    @jenya7
                    &MainWindow should be this. And you ought understand why (it's an instance of MainWindow).

                    1 Reply Last reply
                    3
                    • J Offline
                      J Offline
                      jenya7
                      wrote on last edited by
                      #21

                      OMG! It's working! Thank you so much!

                      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
                      • Unsolved