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. Connect one slot to multiple socket's readyRead() signal
Forum Updated to NodeBB v4.3 + New Features

Connect one slot to multiple socket's readyRead() signal

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.7k 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.
  • divergerD Offline
    divergerD Offline
    diverger
    wrote on last edited by
    #1

    I need to manipulate sockets connected to three servers and coordinate the sequence to read them. For convenience, I connect the three socket's readyRead() signal to just one slot "on_ready_read()", and distinguish them using

    socket = qobject_cast<QTcpSocket *>(QObject::sender());
    

    Then,

    if (socket == s1)
    {
         // read data, and show them in certain widgets, and do some thing, send data to socket if needed
         qDebug() << "s1";
    }
    else if (socket == s2)
    {
         // read data, and show them in certain widgets, and do some thing, send data to socket if needed
         qDebug() << "s2";
    }
    else if (socket == s3)
    {
        // read data, and show them in certain widgets, and do some thing, send data to socket if needed
    
        if (some_condition)
        {
              QMessageBox::critical();
        }
       
        qDebug() << "s3";
    }
    

    I noticed that when the message box is shown, "s3" stopped showing, but "s1" and "s2" are showing without blocked. I'm a little confused about this. You know, QMessageBox::xxxx() is blocking function, so "s3" won't show. But why "s2" and "s1" keep showing? Which thread my "on_ready_read()" slot is running in?

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

      Hi,

      That's because QMessabeBox::critical executes a local event loop.

      You could use QSignalMapper.

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

      divergerD 2 Replies Last reply
      2
      • SGaistS SGaist

        Hi,

        That's because QMessabeBox::critical executes a local event loop.

        You could use QSignalMapper.

        divergerD Offline
        divergerD Offline
        diverger
        wrote on last edited by
        #3

        @SGaist thanks. Yes, i can understand that read of s3 is blocked. But not very clear about why s1 and s2 keep read. You know, there is only one slot function. Are they running in different threads now? Can you explain more?

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

          No but you still have an event loop running thus there still event processing going on.

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

          1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            That's because QMessabeBox::critical executes a local event loop.

            You could use QSignalMapper.

            divergerD Offline
            divergerD Offline
            diverger
            wrote on last edited by
            #5

            @SGaist OK, I've understand that QMessageBox::xxxx executes a local event loop, so 's1' and 's2' can still read data.

            About using 'QSignalMapper'. I think all my code will execute in just on thread, the "GUI thread", right? So, the socket read event will be queued in the event loop. Even I use QSinalMapper, the all read event will come in sequential. So it is safe to just use one slot to handle them, am I right?

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

              What exactly are you trying to achieve ?

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

              divergerD 1 Reply Last reply
              1
              • SGaistS SGaist

                What exactly are you trying to achieve ?

                divergerD Offline
                divergerD Offline
                diverger
                wrote on last edited by diverger
                #7

                @SGaist The server data is not sent randomly. They have some sequence, such as, s1, s3, s2,...., so I need maintain a state machine on my side. I should parse the data read from them to decide which is the next socket to read from. So, split the 'read' operation in several 'slot's may make it more complicated. I think if I use just one 'slot', I can use 'goto' to jump around. Though it may be a little 'ugly'. Any good suggestions?

                VRoninV 1 Reply Last reply
                0
                • divergerD diverger

                  @SGaist The server data is not sent randomly. They have some sequence, such as, s1, s3, s2,...., so I need maintain a state machine on my side. I should parse the data read from them to decide which is the next socket to read from. So, split the 'read' operation in several 'slot's may make it more complicated. I think if I use just one 'slot', I can use 'goto' to jump around. Though it may be a little 'ugly'. Any good suggestions?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @diverger said in Connect one slot to multiple socket's readyRead() signal:

                  'goto' to jump around

                  I died a bit inside.

                  I strongly suggest rethinking your design.

                  if you want to stop processing signals from other sockets while the messagebox is displayed just add

                  QSignalBlocker blockS1(s1);
                  QSignalBlocker blockS2(s2);
                  

                  just before the call to QMessageBox::critical

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2

                  • Login

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