本篇内容介绍了“Qt函数名使用方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
//判断IP地址及端口是否在线 static bool ipLive(const QString &ip, int port, int timeout = 1000); //获取网页所有源代码 static QString getHtml(const QString &url); //获取本机公网IP地址 static QString getNetIP(const QString &webCode); //获取本机IP static QString getLocalIP(); //Url地址转为IP地址 static QString urlToIP(const QString &url); //字符串补全 static QString getValue(quint8 value); //判断是否通外网 static bool isWebOk();
bool QUIHelper::ipLive(const QString &ip, int port, int timeout) { //局部的事件循环,不卡主界面 QEventLoop eventLoop; //设置超时 QTimer timer; connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); timer.setSingleShot(true); timer.start(timeout); QTcpSocket tcpSocket; connect(&tcpSocket, SIGNAL(connected()), &eventLoop, SLOT(quit())); tcpSocket.connectToHost(ip, port); eventLoop.exec(); bool ok = (tcpSocket.state() == QAbstractSocket::ConnectedState); return ok; } QString QUIHelper::getHtml(const QString &url) { QNetworkAccessManager *manager = new QNetworkAccessManager(); QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url))); QByteArray responseData; QEventLoop eventLoop; QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit())); eventLoop.exec(); responseData = reply->readAll(); return QString(responseData); } QString QUIHelper::getNetIP(const QString &webCode) { QString web = webCode; web = web.replace(' ', ""); web = web.replace("\r", ""); web = web.replace("\n", ""); QStringList list = web.split("<br/>"); QString tar = list.at(3); QStringList ip = tar.split("="); return ip.at(1); } QString QUIHelper::getLocalIP() { QStringList ips; QList<QHostAddress> addrs = QNetworkInterface::allAddresses(); foreach (QHostAddress addr, addrs) { QString ip = addr.toString(); if (QUIHelper::isIP(ip)) { ips << ip; } } //优先取192开头的IP,如果获取不到IP则取127.0.0.1 QString ip = "127.0.0.1"; foreach (QString str, ips) { if (str.startsWith("192.168.1") || str.startsWith("192")) { ip = str; break; } } return ip; } QString QUIHelper::urlToIP(const QString &url) { QHostInfo host = QHostInfo::fromName(url); return host.addresses().at(0).toString(); } QString QUIHelper::getValue(quint8 value) { QString result = QString::number(value); if (result.length() <= 1) { result = QString("0%1").arg(result); } return result; } bool QUIHelper::isWebOk() { //能接通百度IP说明可以通外网 return ipLive("115.239.211.112", 80); }
“Qt函数名使用方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。