Hello!

There is a class in which two QTcpSocket and one QTimer.

StateTimer = new QTimer(this); ControlSock = new QTcpSocket(this); StatSock = new QTcpSocket(this); 

5 slots are defined for them inside the class and they are all connected like this

 QObject::connect(StateTimer, SIGNAL(timeout()), this, SLOT(Timer_slot())); QObject::connect(ControlSock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(ControlSockStateChange_slot(QAbstractSocket::SocketState))); QObject::connect(StatSock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(StatSockStateChange_slot(QAbstractSocket::SocketState))); QObject::connect(ControlSock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(ControlSockError_slot(QAbstractSocket::SocketError))); QObject::connect(StatSock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(StatSockError_slot(QAbstractSocket::SocketError))); 

in all slots the same function is called. In this function I did

 counter++; qDebug() << "call: " << (int) QThread::currentThreadId(); qDebug() << "counter: " <<counter; .... counter--; return; 

In my ideal world, something would not happen to the network and the timer function should always write "counter: 1". However, when the connection is broken (the cable was physically pulled out), when 2 sockets are collapsed at once, and there are also errors other than 2 state changes, 6 function calls occur. But not consistently as I expected, but in parallel! I get a record

call: 9600 counter: 1

call: 9600 counter: 2

call: 9600 counter: 3

call: 9600 counter: 4

call: 9600 counter: 5

Explain the good people, what the hell is going on? How can a function call from a single thread occur over events? Inside the function, of course, I don’t pull any message queue handlers.

And the second question, if I make a signal-slot connection, the connection is in auto mode, and then I transfer one of the objects to another three, will it automatically switch from one line to the next? Or it is necessary to monitor this and connect everything already after pushing the objects on the truss or immediately lay the correct types of connections? Or, with auto connection, the call type is already determined during operation?

  • The only explanation that I have found what is happening is this: inside the called function, the one in which the thread comes in, the ControlSock-> state () and ControlSock-> disconnectFromHost () methods are called several times; There is a great suspicion that the processing of a message queue with the selection and execution of a new message is twitching inside these methods .... - Andrey Golikov

1 answer 1

The reason for this behavior is that the function was working with sockets.

 if(ControlSock->state() != QAbstractSocket::UnconnectedState) ControlSock->abort(); 

or

 if(ControlSock->state() != QAbstractSocket::UnconnectedState) ControlSock->disconnectFromHost(); 

Within these methods, signals were also created that, once again, were directly called into the same function. Since the function is one for both sockets, I caught signals from both.