Thanks, this was helpful and quite easy to implement, but I have a problem, this is my boolean function that is called from another pieces of code. It is to return true if there's connection (it manages to connect to the specified host) and is to return false if it doesn't manage to connect to the specified host.
This *was* the code that hanged but worked fine (I had the opportunity to return true or false)
bool Global::connected_to_internet(){
sock.connectToHost("www.gamato.info", 80);
if (sock.waitForConnected(15000))
return true;
else
return false;
}
bool Global::connected_to_internet(){
QTcpSocket sock;
sock.connectToHost("www.gamato.info", 80);
if (sock.waitForConnected(15000))
return true;
else
return false;
}
To copy to clipboard, switch view to plain text mode
The piece of code using your suggestion will be:
bool Global::connected_to_internet(){
sock.connectToHost("www.gamato.info", 80);
}
void Global
::check_what_happened(QAbstractSocket::SocketType socktype
){ //< it goes through this function while the bollean is over
switch(socktype){
//etc
}
}
bool Global::connected_to_internet(){
sock = new QTcpSocket(this);
QObject::connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(check_what_happened(QAbstractSocket::SocketType)) );
sock.connectToHost("www.gamato.info", 80);
}
void Global::check_what_happened(QAbstractSocket::SocketType socktype){ //< it goes through this function while the bollean is over
switch(socktype){
case QAbstractSocket::hostFound():
//etc
}
}
To copy to clipboard, switch view to plain text mode
I don't know how to work this out. I need my boolean function to return true or false and by calling another void function (maybe can it be boolean and somehow grab true or false or to emit a signal?) I cannot alert somehow if I could connect to the host or not... :/
Bookmarks