From 4844257fc827cb946eb506f80c838d0b8942ec5d Mon Sep 17 00:00:00 2001 From: 1bbb <4358982127@qq.com> Date: Thu, 8 Dec 2022 10:23:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B5=8C=E5=A5=97=E6=97=A0?= =?UTF-8?q?=E8=BE=B9=E6=A1=86=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/mainwindow/CMakeLists.txt | 4 +++ examples/mainwindow/mainwindow.cpp | 12 ++++++++ .../Quick/private/framelessquickhelper_p.h | 3 -- src/core/framelesshelper_win.cpp | 2 -- src/quick/framelessquickhelper.cpp | 30 ------------------- src/widgets/framelessdialog.cpp | 6 ++++ src/widgets/framelessmainwindow.cpp | 6 ++++ src/widgets/framelesswidget.cpp | 8 ++++- src/widgets/framelesswidgetshelper.cpp | 21 ++----------- src/widgets/standardtitlebar.cpp | 2 +- src/widgets/widgetssharedhelper.cpp | 10 ------- 11 files changed, 38 insertions(+), 66 deletions(-) diff --git a/examples/mainwindow/CMakeLists.txt b/examples/mainwindow/CMakeLists.txt index 2165dc1..fa4c7ee 100644 --- a/examples/mainwindow/CMakeLists.txt +++ b/examples/mainwindow/CMakeLists.txt @@ -27,6 +27,10 @@ set(SOURCES ../shared/log.cpp ../shared/settings.h ../shared/settings.cpp + ../dialog/dialog.h + ../dialog/dialog.cpp + ../widget/widget.h + ../widget/widget.cpp mainwindow.ui mainwindow.h mainwindow.cpp diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 2c263f6..060e1b6 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -31,6 +31,8 @@ #include #include #include "../shared/settings.h" +#include "../widget/widget.h" +#include "../dialog/dialog.h" FRAMELESSHELPER_USE_NAMESPACE @@ -106,4 +108,14 @@ QMenuBar::item:pressed { setWindowTitle(tr("FramelessHelper demo application - Qt MainWindow")); setWindowIcon(QFileIconProvider().icon(QFileIconProvider::Computer)); + connect(m_mainWindow->pushButton, &QPushButton::clicked, this, [this]{ + const auto dialog = new Dialog(this); + dialog->setAttribute(Qt::WA_DeleteOnClose); + dialog->exec(); + }); + connect(m_mainWindow->pushButton_2, &QPushButton::clicked, this, [this]{ + const auto widget = new Widget(this); + widget->setAttribute(Qt::WA_DeleteOnClose); + widget->show(); + }); } diff --git a/include/FramelessHelper/Quick/private/framelessquickhelper_p.h b/include/FramelessHelper/Quick/private/framelessquickhelper_p.h index cd43185..8a02ea7 100644 --- a/include/FramelessHelper/Quick/private/framelessquickhelper_p.h +++ b/include/FramelessHelper/Quick/private/framelessquickhelper_p.h @@ -85,9 +85,6 @@ public: Q_NODISCARD static FramelessQuickHelper *findOrCreateFramelessHelper(QObject *object); -protected: - Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override; - private: Q_NODISCARD QRect mapItemGeometryToScene(const QQuickItem * const item) const; Q_NODISCARD bool isInSystemButtons(const QPoint &pos, QuickGlobal::SystemButtonType *button) const; diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 4cd150a..80a4b9d 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -530,8 +530,6 @@ void FramelessHelperWin::addWindow(const SystemParameters ¶ms) } g_win32Helper()->mutex.unlock(); DEBUG.noquote() << "The DPI of window" << hwnd2str(windowId) << "is" << data.dpi; - // Some Qt internals have to be corrected. - Utils::maybeFixupQtInternals(windowId); // Qt maintains a frame margin internally, we need to update it accordingly // otherwise we'll get lots of warning messages when we change the window // geometry, it will also affect the final window geometry because QPA will diff --git a/src/quick/framelessquickhelper.cpp b/src/quick/framelessquickhelper.cpp index 4faa40f..4bd69f7 100644 --- a/src/quick/framelessquickhelper.cpp +++ b/src/quick/framelessquickhelper.cpp @@ -186,8 +186,6 @@ void FramelessQuickHelperPrivate::attach() } g_quickHelper()->mutex.unlock(); - window->installEventFilter(this); - SystemParameters params = {}; params.getWindowId = [window]() -> WId { return window->winId(); }; params.getWindowFlags = [window]() -> Qt::WindowFlags { return window->flags(); }; @@ -260,7 +258,6 @@ void FramelessQuickHelperPrivate::detach() return; } g_quickHelper()->data.remove(windowId); - w->removeEventFilter(this); FramelessManager::instance()->removeWindow(windowId); } @@ -656,33 +653,6 @@ FramelessQuickHelper *FramelessQuickHelperPrivate::findOrCreateFramelessHelper(Q return instance; } -bool FramelessQuickHelperPrivate::eventFilter(QObject *object, QEvent *event) -{ - Q_ASSERT(object); - Q_ASSERT(event); - if (!object || !event) { - return false; - } -#ifdef Q_OS_WINDOWS - if (!object->isWindowType() || (event->type() != QEvent::WindowStateChange) - || FramelessConfig::instance()->isSet(Option::UseCrossPlatformQtImplementation)) { - return QObject::eventFilter(object, event); - } - const auto window = qobject_cast(object); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) - if (Utils::windowStatesToWindowState(window->windowStates()) != Qt::WindowFullScreen) { -#else - if (window->windowState() != Qt::WindowFullScreen) { -#endif - const auto changeEvent = static_cast(event); - if (Utils::windowStatesToWindowState(changeEvent->oldState()) == Qt::WindowFullScreen) { - Utils::maybeFixupQtInternals(window->winId()); - } - } -#endif - return QObject::eventFilter(object, event); -} - QRect FramelessQuickHelperPrivate::mapItemGeometryToScene(const QQuickItem * const item) const { Q_ASSERT(item); diff --git a/src/widgets/framelessdialog.cpp b/src/widgets/framelessdialog.cpp index 602fb7d..71bb414 100644 --- a/src/widgets/framelessdialog.cpp +++ b/src/widgets/framelessdialog.cpp @@ -79,6 +79,12 @@ const FramelessDialogPrivate *FramelessDialogPrivate::get(const FramelessDialog void FramelessDialogPrivate::initialize() { Q_Q(FramelessDialog); + // Without this flag, Qt will always create an invisible native parent window + // for any native widgets which will intercept some win32 messages and confuse + // our own native event filter, so to prevent some weired bugs from happening, + // just disable this feature. + q->setAttribute(Qt::WA_DontCreateNativeAncestors); + q->setAttribute(Qt::WA_NativeWindow); FramelessWidgetsHelper::get(q)->extendsContentIntoTitleBar(); m_sharedHelper = new WidgetsSharedHelper(this); m_sharedHelper->setup(q); diff --git a/src/widgets/framelessmainwindow.cpp b/src/widgets/framelessmainwindow.cpp index f690134..37c65b6 100644 --- a/src/widgets/framelessmainwindow.cpp +++ b/src/widgets/framelessmainwindow.cpp @@ -79,6 +79,12 @@ const FramelessMainWindowPrivate *FramelessMainWindowPrivate::get(const Frameles void FramelessMainWindowPrivate::initialize() { Q_Q(FramelessMainWindow); + // Without this flag, Qt will always create an invisible native parent window + // for any native widgets which will intercept some win32 messages and confuse + // our own native event filter, so to prevent some weired bugs from happening, + // just disable this feature. + q->setAttribute(Qt::WA_DontCreateNativeAncestors); + q->setAttribute(Qt::WA_NativeWindow); FramelessWidgetsHelper::get(q)->extendsContentIntoTitleBar(); m_sharedHelper = new WidgetsSharedHelper(this); m_sharedHelper->setup(q); diff --git a/src/widgets/framelesswidget.cpp b/src/widgets/framelesswidget.cpp index 2840bf0..da314bc 100644 --- a/src/widgets/framelesswidget.cpp +++ b/src/widgets/framelesswidget.cpp @@ -79,6 +79,12 @@ const FramelessWidgetPrivate *FramelessWidgetPrivate::get(const FramelessWidget void FramelessWidgetPrivate::initialize() { Q_Q(FramelessWidget); + // Without this flag, Qt will always create an invisible native parent window + // for any native widgets which will intercept some win32 messages and confuse + // our own native event filter, so to prevent some weired bugs from happening, + // just disable this feature. + q->setAttribute(Qt::WA_DontCreateNativeAncestors); + q->setAttribute(Qt::WA_NativeWindow); FramelessWidgetsHelper::get(q)->extendsContentIntoTitleBar(); m_sharedHelper = new WidgetsSharedHelper(this); m_sharedHelper->setup(q); @@ -123,7 +129,7 @@ WidgetsSharedHelper *FramelessWidgetPrivate::widgetsSharedHelper() const } FramelessWidget::FramelessWidget(QWidget *parent) - : QWidget(parent), d_ptr(new FramelessWidgetPrivate(this)) + : QWidget(parent, Qt::Window), d_ptr(new FramelessWidgetPrivate(this)) { } diff --git a/src/widgets/framelesswidgetshelper.cpp b/src/widgets/framelesswidgetshelper.cpp index 1e2deb7..5e6acc9 100644 --- a/src/widgets/framelesswidgetshelper.cpp +++ b/src/widgets/framelesswidgetshelper.cpp @@ -298,8 +298,7 @@ WidgetsSharedHelper *FramelessWidgetsHelperPrivate::findOrCreateSharedHelper(QWi return dialogPriv->widgetsSharedHelper(); } } - QWidget * const topLevelWindow = (window->nativeParentWidget() - ? window->nativeParentWidget() : window->window()); + QWidget * const topLevelWindow = window->window(); WidgetsSharedHelper *helper = topLevelWindow->findChild(); if (!helper) { helper = new WidgetsSharedHelper; @@ -317,11 +316,7 @@ FramelessWidgetsHelper *FramelessWidgetsHelperPrivate::findOrCreateFramelessHelp } QObject *parent = nullptr; if (const auto widget = qobject_cast(object)) { - if (QWidget * const nativeParent = widget->nativeParentWidget()) { - parent = nativeParent; - } else { - parent = widget->window(); - } + parent = widget->window(); } else { parent = object; } @@ -435,15 +430,6 @@ void FramelessWidgetsHelperPrivate::attach() } g_widgetsHelper()->mutex.unlock(); - // Without this flag, Qt will always create an invisible native parent window - // for any native widgets which will intercept some win32 messages and confuse - // our own native event filter, so to prevent some weired bugs from happening, - // just disable this feature. - window->setAttribute(Qt::WA_DontCreateNativeAncestors); - // Force the widget become a native window now so that we can deal with its - // win32 events as soon as possible. - window->setAttribute(Qt::WA_NativeWindow); - SystemParameters params = {}; params.getWindowId = [window]() -> WId { return window->winId(); }; params.getWindowFlags = [window]() -> Qt::WindowFlags { return window->windowFlags(); }; @@ -540,9 +526,6 @@ QWidget *FramelessWidgetsHelperPrivate::findTopLevelWindow() const Q_ASSERT(p); if (p) { if (const auto parentWidget = qobject_cast(p)) { - if (QWidget * const nativeParent = parentWidget->nativeParentWidget()) { - return nativeParent; - } return parentWidget->window(); } } diff --git a/src/widgets/standardtitlebar.cpp b/src/widgets/standardtitlebar.cpp index f0a8fa5..d9e79b5 100644 --- a/src/widgets/standardtitlebar.cpp +++ b/src/widgets/standardtitlebar.cpp @@ -441,7 +441,7 @@ bool StandardTitleBarPrivate::eventFilter(QObject *object, QEvent *event) void StandardTitleBarPrivate::initialize() { Q_Q(StandardTitleBar); - m_window = (q->nativeParentWidget() ? q->nativeParentWidget() : q->window()); + m_window = q->window(); m_chromePalette = new ChromePalette(this); connect(m_chromePalette, &ChromePalette::titleBarColorChanged, this, &StandardTitleBarPrivate::updateTitleBarColor); diff --git a/src/widgets/widgetssharedhelper.cpp b/src/widgets/widgetssharedhelper.cpp index 5b268e3..b380cb3 100644 --- a/src/widgets/widgetssharedhelper.cpp +++ b/src/widgets/widgetssharedhelper.cpp @@ -195,16 +195,6 @@ void WidgetsSharedHelper::changeEventHandler(QEvent *event) QMetaObject::invokeMethod(m_targetWidget, "zoomedChanged"); } } -#ifdef Q_OS_WINDOWS - if (!FramelessConfig::instance()->isSet(Option::UseCrossPlatformQtImplementation)) { - if (Utils::windowStatesToWindowState(m_targetWidget->windowState()) != Qt::WindowFullScreen) { - const auto changeEvent = static_cast(event); - if (Utils::windowStatesToWindowState(changeEvent->oldState()) == Qt::WindowFullScreen) { - Utils::maybeFixupQtInternals(m_targetWidget->winId()); - } - } - } -#endif } void WidgetsSharedHelper::paintEventHandler(QPaintEvent *event)