2013-07-06 5 views
1

QTcpSocket으로 작업하는 클라이언트를 설정 중입니다. 서버와의 연결이 설정되는 부분은 스레드로 구현됩니다.C++/Qt - 한 스레드에서 다른 스레드 슬롯으로의 신호

이 내 클래스는 연결 기능합니다 (HPP)를 구현하는 같은 모습입니다 :

class Connector: public QObject { 
Q_OBJECT 
public: 
Connector(QString hexHost); 
virtual ~Connector(); 

// Start to establish a connection 
void startConnection(); 
// End thread 
void stopConnection(); 

signals: 
// Emitted, if connector should get to work 
void startTransforming(); 
// Emitted, if transformation from hex to dig is complete 
void transformationFinished(); 
// Emitted, if connection is established 
void connectionEstd(); 
// Emitted, if connection failed 
void connectionFailed(); 
// Emitted, if work is done 
void doneConnecting(); 

public slots: 
// Connect to server 
void connectToServer(); 

private: 
// IP of the server 
QString addressHost; 
// Socket 
QTcpSocket aQTcpSocket; 
}; 

을 그리고 이것은 내가이 클래스합니다 (CPP)를 구현하는 방법이다;

Connector::Connector(QString hexHost) 
{ 
// Save user data 
addressHost = hexHost; 

// Start thread 
bool result = QObject::connect(this, SIGNAL(startTransforming()), this, SLOT(transformIP())); 
Q_ASSERT(result); 

// Start trying to establish a connection 
result = QObject::connect(this, SIGNAL(transformationFinished()), this, SLOT(connectToServer())); 
Q_ASSERT(result); 

// End thread 
result = QObject::connect(this, SIGNAL(doneConnecting()), this, SLOT(deleteLater())); 
Q_ASSERT(result); 
Q_UNUSED(result); 
} 

Connector::~Connector() 
{ 
QObject::disconnect(this, SIGNAL(startTransforming()), this, SLOT(transformIP())); 
QObject::disconnect(this, SIGNAL(transformationFinished()), this, SLOT(connectToServer())); 
QObject::disconnect(this, SIGNAL(doneConnecting()), this, SLOT(deleteLater())); 
} 

void Connector::transformIP() 
{ 
[...] 

// Emit ready signal 
emit transformationFinished(); 
} 

void Connector::connectToServer() 
{ 
aQTcpSocket.connectToHost(addressHost, port); 
    result = aQTcpSocket.waitForConnected(5000); 

    if(result) 
{ 
    emit connectionFailed(); 
    emit doneConnecting(); 
     std::clog << "Connection failed!" << std::endl; // This debug message is printed during runtime 
    return; 
    } 

// Emit signal 
emit connectionEstd(); 
} 

void Connector::startConnection() 
{ 
emit startTransforming(); 
} 

void Connector::stopConnection() 
{ 
emit doneConnecting(); 
} 

내가하여 스레드를 시작 : 내가 프로그램을 테스트 할 때

[...] 

// Create new thread 
QThread *conThread = new QThread(); 
// Create connector object 
aConnector = new Connector(hexHost); 
aConnector->moveToThread(conThread); 
conThread->start(); 

// Clean up thread 
bool result = QObject::connect(aConnector, SIGNAL(destroyed()), conThread, SLOT(quit())); 
Q_ASSERT(result); 

result = QObject::connect(conThread, SIGNAL(finished()), conThread, SLOT(deleteLater())); 
Q_ASSERT(result); 

// Connect signals 
result = QObject::connect(aConnector, SIGNAL(connectionFailed()), this, SLOT(onConnectionFailed())); 
Q_ASSERT(result); 

result = QObject::connect(aConnector, SIGNAL(connectionEstd()), this, SLOT(onConnectionEstd())); 
Q_ASSERT(result); 
Q_UNUSED(result); 

// Get connector to work 
aConnector->startConnection(); 

[...] 

, 커넥터가 제대로 생성됩니다. 그는 transformIP() 및 connectToServer() 함수를 시작합니다. 현재 서버가 실행되지 않아 클라이언트가 연결할 수 없습니다. 결과적으로 신호가 방출됩니다. connectionFailed() 커넥터 객체로 스레드를 시작하는 클라이언트 클래스는이 신호를 수신해야하며 이에 응답해야합니다.

문제는 다음과 같습니다. 클라이언트 클래스가 반응하지 않기 때문에 신호가 방출되지 않는 것처럼 보입니다. 누군가가이 문제를 해결하는 방법에 대한 아이디어가 있다면, 감사합니다 :)

답변

0

그것은 보이는,

// Connect signals 
result = QObject::connect(aConnector, SIGNAL(connectionFailed()), this, SLOT(onConnectionFailed())); 
Q_ASSERT(result); 

이 좋은 것 : 여기 내가 특정 슬롯에 신호를 연결하는 클라이언트 클래스의 일부이다 QTcpSocket.connectToHost 반환 값은 void이며 호출은 비동기입니다. 당신이해야 할 무엇, QTcpSocket.connectToHost을 (전화) 및 QTcpSocket.waitForConnected (CONNECT_TIME_OUT)를 사용하여 완료 연결을 기다리고 나쁜 연결을

예 처리 다른 것입니다 : 대한

QTcpSocket socket; 
socket.connectToHost(QHostAddress("172.0.0.1", 8080); 
if (!socket.waitForConnected(5000)) { 
    emit connectionFailed(); 
} 
+0

감사를 당신의 빠른 반응! 내가 제안한 코드를 변경했지만 작동하지 않는 것 같습니다. CONNECT_TIME_OUT 값을 증가 시켰지만 아무 일도 일어나지 않았지만 신호는 여전히 방출되지 않습니다. –

+0

당신의 onConnectionFailed()에 무엇이 있습니까? – stepanbujnak

+0

그냥 다른 신호를 내고, 다른 신호는 다른 신호와 함께 작동합니다. 그것은 주로 말한다 : emit anotherSignal(); –