简体   繁体   中英

Qt UDPsocket readyread() SIGNAL emit twice

I use QUDPsocket to send message. And I connect: connect(socket,SIGNAL(ReadyRead()),this,SLOT(processPendingDatagrams()));

when I call the sendmessage("123") in the main,the processPendingDatagrams() only run once.

but when I call the sendmessage("123") in the button click event, this runs twice . how to solve the problem?

sendmessage():

socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);

If the server notices it once or twice depending on what the client does, then it's the client's fault.

In your click event, add something like:

qDebug() << "Click event, sending message...";

You'll probably notice it's sent twice, on click down and when the button is released. Instead you can do something like:

connect(myButton, &QPushButton::clicked, [this]() {sendMessage("123");}

Or do your own slot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM