Qt C++ 프로그램이 있습니다. 메인 드라이버 MainWindow
및 TCPClient
클래스가 있습니다. TCPClient
클래스는 원격 서버와 통신하고 TCP를 통해 일부 데이터를 전송하며 데이터 처리를 요청하고 서버에서 처리 된 데이터를 수신하는 데 사용됩니다. 내 TCPClient
클래스에서 QAbstractSocket
신호 disconnected
을 사용하고 있습니다. 이 연결은 서버와의 연결이 끊어지면 발생합니다. 이 disconnect
신호 (ifDisconnected
)를 처리하는 기능 (슬롯)에서는 MainWindow
의 onCompletionCallback
기능이 호출됩니다. 이제 내 질문은 어떻게 실행 완료 후 TCPClient
실행의 다시 전송을 방지 할 수 있습니다. 다음은 문제를 설명하는 불완전한 코드입니다. Qt 신호가 출력 될 때 마지막으로 실행 된 기능으로 실행 제어가 전송되는 것을 어떻게 막을 수 있습니까?
void TCPClient::connectToServer(QJsonDocument requestJson)
{
// Removed code of other connect signals
connect(tcpSocket_, &QTcpSocket::disconnected, this, &TCPClient::ifDisconnected);
}
void TCPClient::ifDisconnected()
{
// Here the callback is called. After the callback finishes executing, I don't want execution to return to `TCPClient`.
onCompletionCallback_();
return;
}
TCPClient.cpp
void MainWindow::on_connectButton_clicked()
{
std::function<void(void)> callback std::bind(&MainWindow::onCompletetionCallback, this);
tcpClient_ = new TCPClient(callback)->connectToServer(someData);
}
void MainWindow::onCompletetionCallback()
{
if(tcpClient_->isRequestSuccess())
{
QJsonDocument responseJson = tcpClient_->getResponse();
return; //When this finishes executing, I want to prevent the execution control to go back to TCPClient
}
}
mainwindow.cpp
QAbstractSocket
은 연결이 가능한지 확인하기위한 유틸리티 기능을 제공하지 않기 때문에 신호
disconnected
을 사용해야합니다.