Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. sending message to the client using Qt
QtWS25 Last Chance

sending message to the client using Qt

Scheduled Pinned Locked Moved Unsolved Qt 6
16 Posts 4 Posters 2.2k 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.
  • F Offline
    F Offline
    fari35
    wrote on 13 Jan 2021, 11:43 last edited by fari35
    #1

    I have made a UI of multi threaded server in C++ using Qt . The server is receiving the messages as soon as the client sends a message and it is handling all the clients at the same time. Now I want the server to send the message when I click on 'send' button and it should send the message to all the clients at the same time but I am not able to do that I have used readyread() function for receiving the messages. I want the server to send the message that I enter in the textbox as soon as I click on the send button. how can I do that? is there any pre defined function like readyread() for sending messages as well?

    J J 2 Replies Last reply 13 Jan 2021, 11:53
    0
    • F fari35
      13 Jan 2021, 11:43

      I have made a UI of multi threaded server in C++ using Qt . The server is receiving the messages as soon as the client sends a message and it is handling all the clients at the same time. Now I want the server to send the message when I click on 'send' button and it should send the message to all the clients at the same time but I am not able to do that I have used readyread() function for receiving the messages. I want the server to send the message that I enter in the textbox as soon as I click on the send button. how can I do that? is there any pre defined function like readyread() for sending messages as well?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 13 Jan 2021, 11:53 last edited by
      #2

      @fari35 There are various write methods: https://doc.qt.io/qt-5/qiodevice.html#write
      You also did not mention how you actually communicate? Networking?

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

      1 Reply Last reply
      2
      • F fari35
        13 Jan 2021, 11:43

        I have made a UI of multi threaded server in C++ using Qt . The server is receiving the messages as soon as the client sends a message and it is handling all the clients at the same time. Now I want the server to send the message when I click on 'send' button and it should send the message to all the clients at the same time but I am not able to do that I have used readyread() function for receiving the messages. I want the server to send the message that I enter in the textbox as soon as I click on the send button. how can I do that? is there any pre defined function like readyread() for sending messages as well?

        J Online
        J Online
        JonB
        wrote on 13 Jan 2021, 12:03 last edited by
        #3

        @fari35 said in sending message to the client using Qt:

        send the message to all the clients at the same time

        If you are not already aware, this will require you to write the message to each & every connected client separately in a loop. There is no "write data to all clients in one call" method.

        1 Reply Last reply
        2
        • F Offline
          F Offline
          fari35
          wrote on 13 Jan 2021, 12:42 last edited by fari35
          #4

          @JonB No I mean the server assigns a separate thread to each client and readyread() function is a predefined function in Qthread and it is executed in each thread separately. I am following the this example: https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
          so I want a function for sending the message just like readyread isfor receiving

          J 1 Reply Last reply 13 Jan 2021, 12:51
          0
          • F fari35
            13 Jan 2021, 12:42

            @JonB No I mean the server assigns a separate thread to each client and readyread() function is a predefined function in Qthread and it is executed in each thread separately. I am following the this example: https://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
            so I want a function for sending the message just like readyread isfor receiving

            J Online
            J Online
            JonB
            wrote on 13 Jan 2021, 12:51 last edited by JonB
            #5

            @fari35

            so I want a function for sending the message just like readyread isfor receiving

            readyRead is a signal, to indicate data has arrived. There is no "signal" in the same way for sending data, because there is nothing to signal. You just use write() as @jsulm has said.

            That is a separate matter from, perhaps, inventing your own signal out of the UI thread to instruct the other threads socketed to the clients that now is the time to send their data to the clients. But this will emanate from the signal on pressing the button in the UI, in the slot you choose to attach to that.

            F 1 Reply Last reply 14 Jan 2021, 08:23
            2
            • J JonB
              13 Jan 2021, 12:51

              @fari35

              so I want a function for sending the message just like readyread isfor receiving

              readyRead is a signal, to indicate data has arrived. There is no "signal" in the same way for sending data, because there is nothing to signal. You just use write() as @jsulm has said.

              That is a separate matter from, perhaps, inventing your own signal out of the UI thread to instruct the other threads socketed to the clients that now is the time to send their data to the clients. But this will emanate from the signal on pressing the button in the UI, in the slot you choose to attach to that.

              F Offline
              F Offline
              fari35
              wrote on 14 Jan 2021, 08:23 last edited by fari35
              #6

              @JonB I'm trying to make a slot in my fthread.cpp file and a signal in fserver file which will be connected to the signal of mainwindow.cpp, so that as soon as I'll emit the signal from mainwindow.cpp file it will execute the slot in fthread.cpp file. But here I'm not able to make an object of fserver class in fthread class, so how can I do this ?
              I have also attached my code in the question

              J 1 Reply Last reply 14 Jan 2021, 08:43
              0
              • F fari35
                14 Jan 2021, 08:23

                @JonB I'm trying to make a slot in my fthread.cpp file and a signal in fserver file which will be connected to the signal of mainwindow.cpp, so that as soon as I'll emit the signal from mainwindow.cpp file it will execute the slot in fthread.cpp file. But here I'm not able to make an object of fserver class in fthread class, so how can I do this ?
                I have also attached my code in the question

                J Online
                J Online
                JonB
                wrote on 14 Jan 2021, 08:43 last edited by JonB
                #7

                @fari35
                You won't "make an object of fserver class in fthread class". Just connect a signal out of the UI on button press to the slot in the thread to send the message to the client. As I wrote earlier, you'll have to do the connect() for each thread-client.

                Do not explicitly write the Qt::DirectConnections I see you have. Leave Qt to determine connection types (omit the parameter). When connecting across threads it will use Qt::QueuedConnection, which is what is required.

                Don't try to specify a &MainWindow::sendmessage in any non-MainWindow class. Make connections in a class which knows about both the sending-thread instance and receiving-thread instance. If necessary, declare your own signal not in MainWindow class but rather in server thread class. It is permissible for a class to emit a signal which is declared in another class.

                F 1 Reply Last reply 14 Jan 2021, 08:51
                1
                • J JonB
                  14 Jan 2021, 08:43

                  @fari35
                  You won't "make an object of fserver class in fthread class". Just connect a signal out of the UI on button press to the slot in the thread to send the message to the client. As I wrote earlier, you'll have to do the connect() for each thread-client.

                  Do not explicitly write the Qt::DirectConnections I see you have. Leave Qt to determine connection types (omit the parameter). When connecting across threads it will use Qt::QueuedConnection, which is what is required.

                  Don't try to specify a &MainWindow::sendmessage in any non-MainWindow class. Make connections in a class which knows about both the sending-thread instance and receiving-thread instance. If necessary, declare your own signal not in MainWindow class but rather in server thread class. It is permissible for a class to emit a signal which is declared in another class.

                  F Offline
                  F Offline
                  fari35
                  wrote on 14 Jan 2021, 08:51 last edited by fari35
                  #8

                  @JonB I am trying to connect the signal in mainwindow with the slot in fthread like this:

                  connect(mainobj, &MainWindow::sendmessage,this, &fthread::sendmessage);
                  

                  but I am not able to make the mainwindow object in fthread class. Is there any alternate way of doing this?

                  J 1 Reply Last reply 14 Jan 2021, 08:55
                  0
                  • F fari35
                    14 Jan 2021, 08:51

                    @JonB I am trying to connect the signal in mainwindow with the slot in fthread like this:

                    connect(mainobj, &MainWindow::sendmessage,this, &fthread::sendmessage);
                    

                    but I am not able to make the mainwindow object in fthread class. Is there any alternate way of doing this?

                    J Online
                    J Online
                    JonB
                    wrote on 14 Jan 2021, 08:55 last edited by JonB
                    #9

                    @fari35
                    I just answered that in my final paragraph above.

                    fthread knows nothing about either MainWindow or mainobj. One solution is to do this connection in the MainWindow instance, where main window knows about each fthread instance. Another solution is to define the signal in fthread class and have main window go emit fthreadIntance->fthreadSignal().

                    F 1 Reply Last reply 14 Jan 2021, 09:32
                    1
                    • J JonB
                      14 Jan 2021, 08:55

                      @fari35
                      I just answered that in my final paragraph above.

                      fthread knows nothing about either MainWindow or mainobj. One solution is to do this connection in the MainWindow instance, where main window knows about each fthread instance. Another solution is to define the signal in fthread class and have main window go emit fthreadIntance->fthreadSignal().

                      F Offline
                      F Offline
                      fari35
                      wrote on 14 Jan 2021, 09:32 last edited by fari35
                      #10

                      @JonB I am sending the message to client like this :

                        thread_obj->tcpSocket->write(block);
                      

                      from the send_onclicked method in main window but as soon as I click on the send button the program is crsahing

                      J 1 Reply Last reply 14 Jan 2021, 09:40
                      0
                      • F fari35
                        14 Jan 2021, 09:32

                        @JonB I am sending the message to client like this :

                          thread_obj->tcpSocket->write(block);
                        

                        from the send_onclicked method in main window but as soon as I click on the send button the program is crsahing

                        J Online
                        J Online
                        JonB
                        wrote on 14 Jan 2021, 09:40 last edited by
                        #11

                        @fari35
                        You are invoking the thread method directly from another thread. Don't do that. As I said, emit a signal, which Qt will connect with QueuedConnection because it is across threads.

                        F 2 Replies Last reply 14 Jan 2021, 09:59
                        0
                        • J JonB
                          14 Jan 2021, 09:40

                          @fari35
                          You are invoking the thread method directly from another thread. Don't do that. As I said, emit a signal, which Qt will connect with QueuedConnection because it is across threads.

                          F Offline
                          F Offline
                          fari35
                          wrote on 14 Jan 2021, 09:59 last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • J JonB
                            14 Jan 2021, 09:40

                            @fari35
                            You are invoking the thread method directly from another thread. Don't do that. As I said, emit a signal, which Qt will connect with QueuedConnection because it is across threads.

                            F Offline
                            F Offline
                            fari35
                            wrote on 14 Jan 2021, 12:33 last edited by
                            #13

                            @JonB But still if I'm emmiting a signal from mainwindow I need to connect the signal in mainwindow to the slot in fthread and for that I need a mainwindow class instance, so how can I do that. I cannot connect them in mainwindow class.

                            J 1 Reply Last reply 14 Jan 2021, 12:52
                            0
                            • F fari35
                              14 Jan 2021, 12:33

                              @JonB But still if I'm emmiting a signal from mainwindow I need to connect the signal in mainwindow to the slot in fthread and for that I need a mainwindow class instance, so how can I do that. I cannot connect them in mainwindow class.

                              J Online
                              J Online
                              JonB
                              wrote on 14 Jan 2021, 12:52 last edited by JonB
                              #14

                              @fari35 said in sending message to the client using Qt:

                              I cannot connect them in mainwindow class.

                              That is indeed where you should do the connection! It should connect the button press to a slot in each thread you create.

                              I never allow #include "mainwindow.h" in any file other than mainwindow.cpp. Other modules should know nothing about a main window, whether it exists or not.

                              Have the thread class export a slot. Include that into mainwindow.cpp. When a thread instance is created, have main window connect() its button signal to the thread slot, for each thread. (You can also create a custom signal wrapper if it's neater.) If necessary, have the thread emit a signal when it is created so that the main window knows to do the connection.

                              In a word, the main window can know about threads, but the threads should not know about the main window. Therefore the connect()s should not be in the threads but rather in the main window.

                              F 1 Reply Last reply 14 Jan 2021, 14:44
                              1
                              • J JonB
                                14 Jan 2021, 12:52

                                @fari35 said in sending message to the client using Qt:

                                I cannot connect them in mainwindow class.

                                That is indeed where you should do the connection! It should connect the button press to a slot in each thread you create.

                                I never allow #include "mainwindow.h" in any file other than mainwindow.cpp. Other modules should know nothing about a main window, whether it exists or not.

                                Have the thread class export a slot. Include that into mainwindow.cpp. When a thread instance is created, have main window connect() its button signal to the thread slot, for each thread. (You can also create a custom signal wrapper if it's neater.) If necessary, have the thread emit a signal when it is created so that the main window knows to do the connection.

                                In a word, the main window can know about threads, but the threads should not know about the main window. Therefore the connect()s should not be in the threads but rather in the main window.

                                F Offline
                                F Offline
                                fari35
                                wrote on 14 Jan 2021, 14:44 last edited by fari35
                                #15

                                @JonB I have done this but now When I emit a signal sendmessage() from mainwindow class (sendmessage() signal is working perfectly fine) to the slot of thread class so the thread class does not emit the gotnewmessage() signal when I want to send the data to mainwindow class that it needs to show in text box. Do you hav any idea why is that happening?

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  ghostkitchen
                                  Banned
                                  wrote on 22 Nov 2021, 09:29 last edited by
                                  #16
                                  This post is deleted!
                                  1 Reply Last reply
                                  0

                                  • Login

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