Minor tweaks
Amends commit 0759f7f010
Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
parent
0759f7f010
commit
18d883b96a
|
@ -56,6 +56,8 @@ if(TARGET Qt${QT_VERSION_MAJOR}::Widgets)
|
|||
list(APPEND SOURCES
|
||||
qtacrylicwidget.h
|
||||
qtacrylicwidget.cpp
|
||||
qtacrylicmainwindow.h
|
||||
qtacrylicmainwindow.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
if(TARGET Qt${QT_VERSION_MAJOR}::Widgets)
|
||||
add_subdirectory(widget)
|
||||
add_subdirectory(mainwindow)
|
||||
endif()
|
||||
if(TARGET Qt${QT_VERSION_MAJOR}::Quick)
|
||||
add_subdirectory(quick)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
TEMPLATE = subdirs
|
||||
CONFIG -= ordered
|
||||
qtHaveModule(widgets): SUBDIRS += widget qmainwindow
|
||||
qtHaveModule(widgets): SUBDIRS += widget mainwindow
|
||||
qtHaveModule(quick): SUBDIRS += quick
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
|
||||
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
|
||||
|
||||
set(SOURCES
|
||||
../images.qrc
|
||||
TitleBar.ui
|
||||
MainWindow.ui
|
||||
main.cpp
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
enable_language(RC)
|
||||
list(APPEND SOURCES ../windows.rc ../windows.manifest)
|
||||
endif()
|
||||
|
||||
add_executable(MainWindow WIN32 ${SOURCES})
|
||||
|
||||
target_link_libraries(MainWindow PRIVATE
|
||||
Qt${QT_VERSION_MAJOR}::Widgets
|
||||
wangwenx190::FramelessHelper
|
||||
)
|
||||
|
||||
target_compile_definitions(MainWindow PRIVATE
|
||||
QT_NO_CAST_FROM_ASCII
|
||||
QT_NO_CAST_TO_ASCII
|
||||
QT_NO_KEYWORDS
|
||||
QT_DEPRECATED_WARNINGS
|
||||
QT_DISABLE_DEPRECATED_BEFORE=0x060000
|
||||
)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(MainWindow PRIVATE /utf-8)
|
||||
if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug"))
|
||||
target_compile_options(MainWindow PRIVATE /guard:cf)
|
||||
target_link_options(MainWindow PRIVATE /GUARD:CF)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(MainWindow PRIVATE user32 shell32 gdi32 dwmapi)
|
||||
endif()
|
|
@ -26,13 +26,10 @@
|
|||
#include "../../qtacrylicmainwindow.h"
|
||||
#include "ui_MainWindow.h"
|
||||
#include "ui_TitleBar.h"
|
||||
#include <QApplication>
|
||||
#include <QStyleOption>
|
||||
#include <QWidget>
|
||||
#include <QWindow>
|
||||
#ifdef WIN32
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
#include <QtWidgets/qapplication.h>
|
||||
#include <QtWidgets/qstyleoption.h>
|
||||
#include <QtWidgets/qwidget.h>
|
||||
#include <QtGui/qwindow.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -59,7 +56,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
QApplication application(argc, argv);
|
||||
|
||||
QtAcrylicMainWindow *mainWindow = new QtAcrylicMainWindow(NULL, Qt::WindowFlags());
|
||||
QtAcrylicMainWindow *mainWindow = new QtAcrylicMainWindow;
|
||||
mainWindow->setAcrylicEnabled(true);
|
||||
|
||||
Ui::MainWindow appMainWindow;
|
||||
|
@ -74,31 +71,22 @@ int main(int argc, char *argv[])
|
|||
|
||||
mainWindow->setMenuWidget(widget);
|
||||
|
||||
QObject::connect(mainWindow,
|
||||
&QMainWindow::windowIconChanged,
|
||||
titleBarWidget.iconButton,
|
||||
&QPushButton::setIcon);
|
||||
QObject::connect(mainWindow,
|
||||
&QMainWindow::windowTitleChanged,
|
||||
titleBarWidget.titleLabel,
|
||||
&QLabel::setText);
|
||||
QObject::connect(titleBarWidget.closeButton,
|
||||
&QPushButton::clicked,
|
||||
mainWindow,
|
||||
&QMainWindow::close);
|
||||
QObject::connect(titleBarWidget.minimizeButton,
|
||||
&QPushButton::clicked,
|
||||
mainWindow,
|
||||
&QMainWindow::showMinimized);
|
||||
QObject::connect(titleBarWidget.maximizeButton,
|
||||
&QPushButton::clicked,
|
||||
[mainWindow, titleBarWidget]() {
|
||||
if (mainWindow->isMaximized()) {
|
||||
QObject::connect(mainWindow, &QMainWindow::windowIconChanged, titleBarWidget.iconButton, &QPushButton::setIcon);
|
||||
QObject::connect(mainWindow, &QMainWindow::windowTitleChanged, titleBarWidget.titleLabel, &QLabel::setText);
|
||||
QObject::connect(titleBarWidget.closeButton, &QPushButton::clicked, mainWindow, &QMainWindow::close);
|
||||
QObject::connect(titleBarWidget.minimizeButton, &QPushButton::clicked, mainWindow, &QMainWindow::showMinimized);
|
||||
QObject::connect(titleBarWidget.maximizeButton, &QPushButton::clicked, [mainWindow](){
|
||||
if (mainWindow->isMaximized() || mainWindow->isFullScreen()) {
|
||||
mainWindow->showNormal();
|
||||
} else {
|
||||
mainWindow->showMaximized();
|
||||
}
|
||||
});
|
||||
QObject::connect(mainWindow, &QtAcrylicMainWindow::windowStateChanged, [mainWindow, titleBarWidget](){
|
||||
titleBarWidget.maximizeButton->setChecked(mainWindow->isMaximized());
|
||||
titleBarWidget.maximizeButton->setToolTip(mainWindow->isMaximized() ? QObject::tr("Restore") : QObject::tr("Maximize"));
|
||||
});
|
||||
QObject::connect(titleBarWidget.iconButton, &QPushButton::clicked, mainWindow, &QtAcrylicMainWindow::displaySystemMenu);
|
||||
|
||||
QStyleOption option;
|
||||
option.initFrom(mainWindow);
|
||||
|
@ -109,18 +97,7 @@ int main(int argc, char *argv[])
|
|||
mainWindow->createWinId(); // Qt's internal function, make sure it's a top level window.
|
||||
const QWindow *win = mainWindow->windowHandle();
|
||||
|
||||
QObject::connect(mainWindow,
|
||||
&QtAcrylicMainWindow::windowStateChanged,
|
||||
[mainWindow, titleBarWidget]() {
|
||||
titleBarWidget.maximizeButton->setChecked(mainWindow->isMaximized());
|
||||
titleBarWidget.maximizeButton->setToolTip(mainWindow->isMaximized() ? QObject::tr("Restore") : QObject::tr("Maximize"));
|
||||
});
|
||||
|
||||
QObject::connect(titleBarWidget.iconButton,
|
||||
&QPushButton::clicked,
|
||||
mainWindow,
|
||||
&QtAcrylicMainWindow::displaySystemMenu);
|
||||
|
||||
FramelessWindowsManager::addWindow(win);
|
||||
FramelessWindowsManager::addIgnoreObject(win, titleBarWidget.iconButton);
|
||||
FramelessWindowsManager::addIgnoreObject(win, titleBarWidget.minimizeButton);
|
||||
FramelessWindowsManager::addIgnoreObject(win, titleBarWidget.maximizeButton);
|
|
@ -1,4 +1,4 @@
|
|||
TARGET = QMainWindow
|
||||
TARGET = MainWindow
|
||||
TEMPLATE = app
|
||||
QT += widgets
|
||||
SOURCES += main.cpp
|
6
lib.pro
6
lib.pro
|
@ -24,9 +24,11 @@ SOURCES += \
|
|||
qtacryliceffecthelper.cpp
|
||||
qtHaveModule(widgets) {
|
||||
QT += widgets
|
||||
HEADERS += qtacrylicwidget.h \
|
||||
HEADERS += \
|
||||
qtacrylicwidget.h \
|
||||
qtacrylicmainwindow.h
|
||||
SOURCES += qtacrylicwidget.cpp \
|
||||
SOURCES += \
|
||||
qtacrylicwidget.cpp \
|
||||
qtacrylicmainwindow.cpp
|
||||
}
|
||||
qtHaveModule(quick) {
|
||||
|
|
|
@ -24,12 +24,13 @@
|
|||
|
||||
#include "qtacrylicmainwindow.h"
|
||||
#include "utilities.h"
|
||||
#include "framelesswindowsmanager.h"
|
||||
#include <QtCore/qdebug.h>
|
||||
#include <QtGui/qevent.h>
|
||||
#include <QtGui/qpainter.h>
|
||||
|
||||
QtAcrylicMainWindow::QtAcrylicMainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) {}
|
||||
QtAcrylicMainWindow::QtAcrylicMainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
|
||||
{
|
||||
}
|
||||
|
||||
QtAcrylicMainWindow::~QtAcrylicMainWindow() = default;
|
||||
|
||||
|
@ -150,6 +151,9 @@ void QtAcrylicMainWindow::setAcrylicEnabled(const bool value)
|
|||
setBackgroundRole(m_acrylicEnabled ? QPalette::Base : QPalette::Window);
|
||||
update();
|
||||
Q_EMIT acrylicEnabledChanged();
|
||||
if (m_acrylicEnabled) {
|
||||
m_acrylicHelper.showWarning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -158,7 +162,6 @@ void QtAcrylicMainWindow::showEvent(QShowEvent *event)
|
|||
QMainWindow::showEvent(event);
|
||||
updateContentMargin();
|
||||
if (!m_inited) {
|
||||
FramelessWindowsManager::addWindow(windowHandle());
|
||||
m_acrylicHelper.install(windowHandle());
|
||||
m_acrylicHelper.updateAcrylicBrush(tintColor());
|
||||
connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, qOverload<>(&QtAcrylicMainWindow::update));
|
||||
|
@ -168,7 +171,8 @@ void QtAcrylicMainWindow::showEvent(QShowEvent *event)
|
|||
|
||||
void QtAcrylicMainWindow::updateContentMargin()
|
||||
{
|
||||
const qreal m = isMaximized() ? 0.0 : 1.0 / devicePixelRatioF();
|
||||
const qreal margin = (isMaximized() || isFullScreen()) ? 0.0 : (1.0 / devicePixelRatioF());
|
||||
const int m = qRound(margin);
|
||||
setContentsMargins(m, m, m, m);
|
||||
}
|
||||
|
||||
|
@ -187,16 +191,16 @@ void QtAcrylicMainWindow::paintEvent(QPaintEvent *event)
|
|||
|
||||
void QtAcrylicMainWindow::changeEvent(QEvent *event)
|
||||
{
|
||||
QMainWindow::changeEvent(event);
|
||||
if (event->type() == QEvent::WindowStateChange) {
|
||||
updateContentMargin();
|
||||
Q_EMIT windowStateChanged();
|
||||
}
|
||||
QMainWindow::changeEvent(event);
|
||||
}
|
||||
|
||||
void QtAcrylicMainWindow::displaySystemMenu()
|
||||
{
|
||||
#ifdef WIN32
|
||||
#ifdef Q_OS_WINDOWS
|
||||
Utilities::displaySystemMenu(windowHandle());
|
||||
#endif
|
||||
}
|
|
@ -41,7 +41,7 @@ class FRAMELESSHELPER_EXPORT QtAcrylicMainWindow : public QMainWindow
|
|||
Q_PROPERTY(bool acrylicEnabled READ acrylicEnabled WRITE setAcrylicEnabled NOTIFY acrylicEnabledChanged)
|
||||
|
||||
public:
|
||||
explicit QtAcrylicMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
|
||||
explicit QtAcrylicMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = {});
|
||||
~QtAcrylicMainWindow() override;
|
||||
|
||||
QColor tintColor() const;
|
||||
|
@ -65,6 +65,9 @@ public:
|
|||
bool acrylicEnabled() const;
|
||||
void setAcrylicEnabled(const bool value);
|
||||
|
||||
public Q_SLOTS:
|
||||
void displaySystemMenu();
|
||||
|
||||
Q_SIGNALS:
|
||||
void tintColorChanged();
|
||||
void tintOpacityChanged();
|
||||
|
@ -75,18 +78,17 @@ Q_SIGNALS:
|
|||
void acrylicEnabledChanged();
|
||||
void windowStateChanged();
|
||||
|
||||
public Q_SLOTS:
|
||||
void displaySystemMenu();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent *event) override;
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void changeEvent(QEvent *event) override;
|
||||
|
||||
private:
|
||||
void updateContentMargin();
|
||||
|
||||
private:
|
||||
QtAcrylicEffectHelper m_acrylicHelper;
|
||||
bool m_frameVisible = true;
|
||||
bool m_acrylicEnabled = false;
|
||||
bool m_inited = false;
|
||||
void updateContentMargin();
|
||||
};
|
||||
|
|
|
@ -73,7 +73,6 @@ Q_SIGNALS:
|
|||
void frameColorChanged();
|
||||
void frameThicknessChanged();
|
||||
void acrylicEnabledChanged();
|
||||
void windowStateChanged();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent *event) override;
|
||||
|
|
Loading…
Reference in New Issue