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. Signals are not triggering common slot

Signals are not triggering common slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 860 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.
  • RomanoFXR Offline
    RomanoFXR Offline
    RomanoFX
    wrote on last edited by
    #1

    Hi!

    I am using QUdpSocket from multicast. m_listUDP is a QList<QUdpSocket*>.
    Each one is connected to a common signal : connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));

    The problem : readPendingDatagrams is never triggered. If I use a just a QUdpSocket* it is working. It seems the connection is not working cause to the QList<QUdpSocket*>.

    Maybe I forgot something?

    Classic::PrepareAndSend()
    {
        udpSocketSend = new QUdpSocket(this);
        bcast = new QHostAddress("239.255.255.250");
    
        m_listHost = QNetworkInterface::allInterfaces();
        for(int i(0); i < m_listHost.count(); i++)
        {
    
            if(m_listHost.at(i).flags().testFlag(QNetworkInterface::IsUp)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::IsRunning)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanBroadcast)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanMulticast) )
            {
                
    			//Get IP address of interface
                QNetworkInterface iface = QNetworkInterface::interfaceFromName(m_listHost.at(i).name());
                QList<QNetworkAddressEntry> entries = iface.addressEntries();
                QNetworkAddressEntry entry = entries.at(1);
                QHostAddress ip = entry.ip();
    
    	    QUdpSocket *socketToAppend = new QUdpSocket(this);
    
                socketToAppend->bind(ip, udpSocketSend->localPort());
                connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    			
                m_listUDP.append(socketToAppend);
            }
    
        }
    
        udpSocketSend->connectToHost(*bcast, 1950);
    
        QByteArray datagram;
        datagram.append("SEARCH");
    	
        udpSocketSend->writeDatagram(datagram.data(), datagram.size(),
                                     QHostAddress::Broadcast, 1950);
    
    
    }
    
    void Classic::readPendingDatagrams()
    {
        QUdpSocket* socketToAppend = qobject_cast<QUdpSocket*>(sender());
        while (socketToAppend->hasPendingDatagrams())
        {
            QByteArray datagram;
            datagram.resize(socketToAppend->pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;
    
            socketToAppend->readDatagram(datagram.data(), datagram.size(),
                                        &sender, &senderPort);
    
            qDebug() << datagram;
        }
    }
    

    Thank you

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      connectToHost is asynchronous, either you add udpSocketSend->waitForConnected(); after udpSocketSend->connectToHost(*bcast, 1950); or you write the datagram in a slot connected to the connected() signal of udpSocketSend

      P.S.
      if your compiler supports C++11 the sender() monster should be always avoidable:

      add the sender as argument
      void Classic::readPendingDatagrams(QUdpSocket* sender)

      and connect using a lambda:
      connect(socketToAppend, &QUdpSocket::readyRead, [socketToAppend,this]()->void{readPendingDatagrams(socketToAppend);});

      "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
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        in addition previous post, it is not issue with connect. If your loop is executing, it connect works perfectly. However issue may be other end of the UDP socket may not be sending the data at all to you end point. So readyRead() signal may not be emitted at all. Please check whether other udp end is sending the data.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        3

        • Login

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