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

    JonBJ 1 Reply Last reply
    0
    • F fari35

      @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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      2
      • JonBJ JonB

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

        JonBJ 1 Reply Last reply
        0
        • F fari35

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          1
          • JonBJ JonB

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

            JonBJ 1 Reply Last reply
            0
            • F fari35

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              1
              • JonBJ JonB

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

                JonBJ 1 Reply Last reply
                0
                • F fari35

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  0
                  • JonBJ JonB

                    @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 last edited by
                    #12
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      JonBJ 1 Reply Last reply
                      0
                      • F fari35

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        1
                        • JonBJ JonB

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