您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Qt函數(shù)名使用方法有哪些”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
//判斷IP地址及端口是否在線 static bool ipLive(const QString &ip, int port, int timeout = 1000); //獲取網(wǎng)頁(yè)所有源代碼 static QString getHtml(const QString &url); //獲取本機(jī)公網(wǎng)IP地址 static QString getNetIP(const QString &webCode); //獲取本機(jī)IP static QString getLocalIP(); //Url地址轉(zhuǎn)為IP地址 static QString urlToIP(const QString &url); //字符串補(bǔ)全 static QString getValue(quint8 value); //判斷是否通外網(wǎng) static bool isWebOk();
bool QUIHelper::ipLive(const QString &ip, int port, int timeout) { //局部的事件循環(huán),不卡主界面 QEventLoop eventLoop; //設(shè)置超時(shí) 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; } } //優(yōu)先取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說(shuō)明可以通外網(wǎng) return ipLive("115.239.211.112", 80); }
“Qt函數(shù)名使用方法有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。