From 08d29fb44c7fad5fadce117dbac827378c323ff8 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Tue, 31 Aug 2021 16:19:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core_frameless/framelessdialog.cpp | 7 +++- core_frameless/framelessmainwindow.cpp | 7 +++- core_frameless/framelesswidget.cpp | 7 +++- core_qui/quihelper.cpp | 58 +++++++++++++++++++++----- core_qui/quihelper.h | 7 ++-- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/core_frameless/framelessdialog.cpp b/core_frameless/framelessdialog.cpp index 3c63413..5e10e65 100644 --- a/core_frameless/framelessdialog.cpp +++ b/core_frameless/framelessdialog.cpp @@ -5,6 +5,7 @@ #ifdef Q_OS_WIN #include "windows.h" +#include "windowsx.h" #pragma comment (lib,"user32.lib") #endif @@ -274,8 +275,10 @@ bool FramelessDialog::nativeEvent(const QByteArray &eventType, void *message, lo return true; } else if (msg->message == WM_NCHITTEST) { //计算鼠标对应的屏幕坐标 - long x = LOWORD(msg->lParam); - long y = HIWORD(msg->lParam); + //这里最开始用的 LOWORD HIWORD 在多屏幕的时候会有问题 + //官方说明在这里 https://docs.microsoft.com/zh-cn/windows/win32/inputdev/wm-nchittest + long x = GET_X_LPARAM(msg->lParam); + long y = GET_Y_LPARAM(msg->lParam); QPoint pos = mapFromGlobal(QPoint(x, y)); //判断当前鼠标位置在哪个区域 diff --git a/core_frameless/framelessmainwindow.cpp b/core_frameless/framelessmainwindow.cpp index e23b030..66a24cf 100644 --- a/core_frameless/framelessmainwindow.cpp +++ b/core_frameless/framelessmainwindow.cpp @@ -5,6 +5,7 @@ #ifdef Q_OS_WIN #include "windows.h" +#include "windowsx.h" #pragma comment (lib,"user32.lib") #endif @@ -274,8 +275,10 @@ bool FramelessMainWindow::nativeEvent(const QByteArray &eventType, void *message return true; } else if (msg->message == WM_NCHITTEST) { //计算鼠标对应的屏幕坐标 - long x = LOWORD(msg->lParam); - long y = HIWORD(msg->lParam); + //这里最开始用的 LOWORD HIWORD 在多屏幕的时候会有问题 + //官方说明在这里 https://docs.microsoft.com/zh-cn/windows/win32/inputdev/wm-nchittest + long x = GET_X_LPARAM(msg->lParam); + long y = GET_Y_LPARAM(msg->lParam); QPoint pos = mapFromGlobal(QPoint(x, y)); //判断当前鼠标位置在哪个区域 diff --git a/core_frameless/framelesswidget.cpp b/core_frameless/framelesswidget.cpp index 2acb4ee..37632f1 100644 --- a/core_frameless/framelesswidget.cpp +++ b/core_frameless/framelesswidget.cpp @@ -5,6 +5,7 @@ #ifdef Q_OS_WIN #include "windows.h" +#include "windowsx.h" #pragma comment (lib,"user32.lib") #endif @@ -274,8 +275,10 @@ bool FramelessWidget::nativeEvent(const QByteArray &eventType, void *message, lo return true; } else if (msg->message == WM_NCHITTEST) { //计算鼠标对应的屏幕坐标 - long x = LOWORD(msg->lParam); - long y = HIWORD(msg->lParam); + //这里最开始用的 LOWORD HIWORD 在多屏幕的时候会有问题 + //官方说明在这里 https://docs.microsoft.com/zh-cn/windows/win32/inputdev/wm-nchittest + long x = GET_X_LPARAM(msg->lParam); + long y = GET_Y_LPARAM(msg->lParam); QPoint pos = mapFromGlobal(QPoint(x, y)); //判断当前鼠标位置在哪个区域 diff --git a/core_qui/quihelper.cpp b/core_qui/quihelper.cpp index 2b2fe2f..0c8cc7b 100644 --- a/core_qui/quihelper.cpp +++ b/core_qui/quihelper.cpp @@ -300,7 +300,11 @@ void QUIHelper::setFramelessForm(QWidget *widgetMain, bool tool, bool top, bool widgetMain->setProperty("canMove", true); //根据设定逐个追加属性 +#ifdef __arm__ widgetMain->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); +#else + widgetMain->setWindowFlags(Qt::FramelessWindowHint); +#endif if (tool) { widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::Tool); } @@ -323,8 +327,12 @@ void QUIHelper::setFramelessForm(QWidget *widgetMain, QWidget *widgetTitle, widgetMain->setProperty("form", true); widgetMain->setProperty("canMove", true); - //根据设定逐个追加属性 去掉了 Qt::X11BypassWindowManagerHint + //根据设定逐个追加属性 +#ifdef __arm__ + widgetMain->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); +#else widgetMain->setWindowFlags(Qt::FramelessWindowHint); +#endif if (tool) { widgetMain->setWindowFlags(widgetMain->windowFlags() | Qt::Tool); } @@ -982,24 +990,58 @@ bool QUIHelper::ipLive(const QString &ip, int port, int timeout) timer.setSingleShot(true); timer.start(timeout); + //主动测试下连接 QTcpSocket tcpSocket; QObject::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) +bool QUIHelper::download(const QString &url, const QString &fileName, int timeout) { + QByteArray data = getHtml(url, timeout); + if (!data.isEmpty()) { + QFile file(fileName); + if (file.open(QFile::WriteOnly | QFile::Truncate)) { + file.write(data); + file.close(); + } + return true; + } + + return false; +} + +QByteArray QUIHelper::getHtml(const QString &url, int timeout) +{ + //初始化网络请求对象 QNetworkAccessManager manager; - QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(url))); + //局部的事件循环,不卡主界面 QEventLoop eventLoop; - QObject::connect(&manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit())); + + //设置超时 5.15开始自带了超时时间函数 默认30秒 +#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0)) + manager.setTransferTimeout(timeout); +#else + QTimer timer; + QObject::connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); + timer.setSingleShot(true); + timer.start(timeout); +#endif + + //启动网络请求 + QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(url))); + QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit())); eventLoop.exec(); + + //读取结果 QByteArray data = reply->readAll(); reply->deleteLater(); - return QString(data); + return data; } QString QUIHelper::getNetIP(const QString &html) @@ -1083,12 +1125,6 @@ QString QUIHelper::getValue(quint8 value) return result; } -bool QUIHelper::isWebOk() -{ - //能接通百度IP 115.239.211.112 说明可以通外网 - return ipLive("www.baidu.com", 80); -} - bool QUIHelper::isCustomUI = false; int QUIHelper::showMessageBox(const QString &info, int type, int closeSec, bool exec) { diff --git a/core_qui/quihelper.h b/core_qui/quihelper.h index e3a099a..d8b46c1 100644 --- a/core_qui/quihelper.h +++ b/core_qui/quihelper.h @@ -164,8 +164,11 @@ public: //判断IP地址及端口是否在线 static bool ipLive(const QString &ip, int port, int timeout = 1000); + //下载网络文件 + static bool download(const QString &url, const QString &fileName, int timeout = 1000); //获取网页所有源代码 - static QString getHtml(const QString &url); + static QByteArray getHtml(const QString &url, int timeout = 1000); + //获取本机公网IP地址 static QString getNetIP(const QString &html); //获取本机IP @@ -177,8 +180,6 @@ public: //字符串补全 static QString getValue(quint8 value); - //判断是否通外网 - static bool isWebOk(); //定义标志位启用系统的还是自定义的对话框 static bool isCustomUI;