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. readyread() signal doesn't emit after sendmessage() signal on pushbutton

readyread() signal doesn't emit after sendmessage() signal on pushbutton

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 8 Posters 2.7k 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
    #1

    I have created a UI for a multi threaded server in Qt. Everything is working fine but now when I emit a signal sendmessage() by clicking the send button to send the message to client then after emitting this signal, readyread() signal in thread class of Qthread doesn't emit. It emits until send button is not clicked but once send button is clicked and sendmessage() signal is emitted readyread() and disconnected signals doesn't emit. Can anyone tell me what is the reason for this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Unless you get some answer from the server there's no reason for readyRead to be emitted.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Unless you get some answer from the server there's no reason for readyRead to be emitted.

        F Offline
        F Offline
        fari35
        wrote on last edited by fari35
        #3

        @SGaist yes the client is sending the message to the server and the server is emitting the readyread() signal even if the client sends 10 messages to the server readyread() will be emitted until i press the send button and as soon as the sendmessage() signal is emitted readyread() signal doesn't emit after that

        C 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I would guess it's a threading problem - don't use threads when you don't need them (and don't understand it's consequences). The first thing which shows me that you don't really understand what you're doing is that you're using Qt::DirectConnection - it's not needed in 99,99% of all usecases. Qt knows what connection type to use.
          And since you're using thread I would guess that the QTcpSocket is not created in the htread from where you access it (either reading or writing, don't know your code) and therefore you kill the socket notification which exactly explains your findings.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          F 1 Reply Last reply
          1
          • F fari35

            @SGaist yes the client is sending the message to the server and the server is emitting the readyread() signal even if the client sends 10 messages to the server readyread() will be emitted until i press the send button and as soon as the sendmessage() signal is emitted readyread() signal doesn't emit after that

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            The code emits a sendmessage() signal from the MainWindow class. Nothing shown connects the signal to anything. It is entirely possible nothing is listening to it and therefore nothing happens.

            The code has a sendmessage() function on a class called fthread. The function is not related to the signal in MainWindow. It's not clear whether you think it is or not.

            F 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              I would guess it's a threading problem - don't use threads when you don't need them (and don't understand it's consequences). The first thing which shows me that you don't really understand what you're doing is that you're using Qt::DirectConnection - it's not needed in 99,99% of all usecases. Qt knows what connection type to use.
              And since you're using thread I would guess that the QTcpSocket is not created in the htread from where you access it (either reading or writing, don't know your code) and therefore you kill the socket notification which exactly explains your findings.

              F Offline
              F Offline
              fari35
              wrote on last edited by fari35
              #6

              @Christian-Ehrlicher I have attached my code in the question and I have created a QTcpSocket. The whole program is working fine until I press the send button and after that the readyread() and disconnected() signla doesn't emit.

              1 Reply Last reply
              0
              • C ChrisW67

                The code emits a sendmessage() signal from the MainWindow class. Nothing shown connects the signal to anything. It is entirely possible nothing is listening to it and therefore nothing happens.

                The code has a sendmessage() function on a class called fthread. The function is not related to the signal in MainWindow. It's not clear whether you think it is or not.

                F Offline
                F Offline
                fari35
                wrote on last edited by
                #7

                @ChrisW67 I have attached the code for sendmessage() signal

                mrjjM 1 Reply Last reply
                0
                • F fari35

                  @ChrisW67 I have attached the code for sendmessage() signal

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @fari35

                  Hi
                  Did you put a break point in each sendmessage and gotNewMesssage functions to make sure you did not
                  create a ping-pong situation where signals are kept firing?

                  F 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @fari35

                    Hi
                    Did you put a break point in each sendmessage and gotNewMesssage functions to make sure you did not
                    create a ping-pong situation where signals are kept firing?

                    F Offline
                    F Offline
                    fari35
                    wrote on last edited by fari35
                    #9

                    @mrjj as soon as the signal is emitted it executes a slot and then returns and continue its execution. There is some problem in threading. Like I also checked after putting a sleep statement in readyread() signal in fthread.cpp file and it worked fine for like some time and after that it crashed and then didin't work

                    mrjjM 1 Reply Last reply
                    0
                    • F fari35

                      @mrjj as soon as the signal is emitted it executes a slot and then returns and continue its execution. There is some problem in threading. Like I also checked after putting a sleep statement in readyread() signal in fthread.cpp file and it worked fine for like some time and after that it crashed and then didin't work

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @fari35

                      well put breakpoints on the slots and check they are called just once when you press the send button.

                      F 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @fari35

                        well put breakpoints on the slots and check they are called just once when you press the send button.

                        F Offline
                        F Offline
                        fari35
                        wrote on last edited by
                        #11

                        @mrjj Yes, I have checked right nnow the slots are only called once

                        JonBJ 1 Reply Last reply
                        0
                        • F fari35

                          @mrjj Yes, I have checked right nnow the slots are only called once

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

                          @fari35
                          I have told you before to put qDebug() statements in. You are supposed to do more investigation yourself and report the exact problem rather than just keep throwing your whole code at the community for answer....

                          F 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @fari35
                            I have told you before to put qDebug() statements in. You are supposed to do more investigation yourself and report the exact problem rather than just keep throwing your whole code at the community for answer....

                            F Offline
                            F Offline
                            fari35
                            wrote on last edited by fari35
                            #13

                            @JonB I didn't attch the whole code initially but after they asked for it , I've attached my whole code, but now I have cut it short. the problem I'm getting is that readyread() siggnal is not emitting after send button is pressed. Before pressing the send button it emits even if the client sends 10 messages it emits 10 times but as soon as the send button is pressed it stops emitting

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              The code still does not show where the QTcpSocket is created - and when it's like I guess you implemented it, it's still created in the wrong thread. Please provide a minimal, compilable example!

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              F 1 Reply Last reply
                              1
                              • Christian EhrlicherC Christian Ehrlicher

                                The code still does not show where the QTcpSocket is created - and when it's like I guess you implemented it, it's still created in the wrong thread. Please provide a minimal, compilable example!

                                F Offline
                                F Offline
                                fari35
                                wrote on last edited by fari35
                                #15

                                @Christian-Ehrlicher here is the code for run() where QTcpSocket is created:

                                void fthread::run()
                                {
                                
                                
                                    tcpSocket = new QTcpSocket();
                                
                                    if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
                                        emit error(tcpSocket->error());
                                        return;
                                    }
                                
                                    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
                                
                                    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
                                
                                    exec();
                                
                                }
                                
                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @fari35 said in readyread() signal doesn't emit after sendmessage() signal on pushbutton:

                                  connect( this, SIGNAL(sendmessage(QString)),thread_obj, SLOT(sendmessage(QString)));

                                  As I said - the function sendmessage(QString) is executed in the main thread. Please read the docs about QThread or don't use QThread at all when you don't know the basics. There is no reason to use threads at all...

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  F 1 Reply Last reply
                                  1
                                  • Christian EhrlicherC Christian Ehrlicher

                                    @fari35 said in readyread() signal doesn't emit after sendmessage() signal on pushbutton:

                                    connect( this, SIGNAL(sendmessage(QString)),thread_obj, SLOT(sendmessage(QString)));

                                    As I said - the function sendmessage(QString) is executed in the main thread. Please read the docs about QThread or don't use QThread at all when you don't know the basics. There is no reason to use threads at all...

                                    F Offline
                                    F Offline
                                    fari35
                                    wrote on last edited by fari35
                                    #17

                                    @Christian-Ehrlicher Actually I need to make a multi threaded server for my project and I'm using Qt for that. I want to send the message to all the clients who have been assigned a separate thread, Can you please tell me how can I do that because I'm not getting it.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • F fari35

                                      @Christian-Ehrlicher Actually I need to make a multi threaded server for my project and I'm using Qt for that. I want to send the message to all the clients who have been assigned a separate thread, Can you please tell me how can I do that because I'm not getting it.

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

                                      @fari35
                                      What is it you need beyond the Threaded Fortune Server Example ? How much are you based on that?

                                      F 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @fari35
                                        What is it you need beyond the Threaded Fortune Server Example ? How much are you based on that?

                                        F Offline
                                        F Offline
                                        fari35
                                        wrote on last edited by
                                        #19

                                        @JonB In this example it's not shown how to send the message to the client if the signal is emitted from the main thread like here I'm making a UI for the server so I want to send the message to the client as soon as the send button is clicked, but I don't know how to do it. I'm not even getting help from anywhere.

                                        1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          Again: if you want to use thread (for whatever reason, maybe just to show that you're a famous programmer...), make sure you understand what you're doing and read the documentation and examples:

                                          "It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots and invoked methods will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread."

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          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