Solved How do you know if it is connected?
-
This is my code.But I have a problem. When the Internet is disconnected, it will show that the Internet is not disconnected for 1 minute.Thanks,I need your help.
QHostInfo::lookupHost("www.baidu.com", this, &MainWindow::netWorkStatus); bool MainWindow::netWorkStatus(const QHostInfo& host) { if (host.error() != QHostInfo::NoError) { qDebug() << "Lookup failed:" << host.errorString(); ui->label_2->setText("Broken network"); return false; }else{ qDebug()<<""; ui->label_2->setText("Connected to the Internet"); // QHostInfo::abortHostLookup(lookupID); return true; } }
-
-
Firstly, thanks your reply!However,I think this isn't solve my question.In China, Google is not accessible.
-
I use a method to solve this problem.This is my code:
bool MainWindow::check() { QTcpSocket* sock = new QTcpSocket(this); sock->connectToHost("www.google.com", 80); bool connected = sock->waitForConnected(30000);//ms if (!connected) { sock->abort(); return false; } sock->close(); return true; }
-
I think I have a better idea of using threads to monitor network state. The way to do this is to use the above method in the thread and use QTimer.