From f645ae48cda10b80ffe9d83ba9487671c7f535c0 Mon Sep 17 00:00:00 2001 From: Zhao Yuhang <2546789017@qq.com> Date: 周日, 15 9月 2024 13:26:04 +0800 Subject: [PATCH] update demo code --- examples/qml/main.qml | 55 ++++++++++++++--- examples/qml/main.cpp | 24 ++++++- examples/mainwindow/main.cpp | 28 ++------ examples/qml/QWKButton.qml | 1 examples/mainwindow/mainwindow.h | 2 examples/mainwindow/mainwindow.cpp | 37 +++++++---- 6 files changed, 96 insertions(+), 51 deletions(-) diff --git a/examples/mainwindow/main.cpp b/examples/mainwindow/main.cpp index aeafc29..844dfcb 100644 --- a/examples/mainwindow/main.cpp +++ b/examples/mainwindow/main.cpp @@ -9,34 +9,22 @@ int main(int argc, char *argv[]) { qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); qputenv("QSG_INFO", "1"); -#if 0 - qputenv("QT_WIDGETS_RHI", "1"); - qputenv("QSG_RHI_BACKEND", "d3d12"); - qputenv("QSG_RHI_HDR", "scrgb"); - qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1"); - + //qputenv("QT_WIDGETS_HIGHDPI_DOWNSCALE", "1"); + //qputenv("QT_WIDGETS_RHI", "1"); + //qputenv("QSG_RHI_BACKEND", "d3d12"); + //qputenv("QSG_RHI_HDR", "scrgb"); + //qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1"); + +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QGuiApplication::setHighDpiScaleFactorRoundingPolicy( Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif - QGuiApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); + QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); QApplication a(argc, argv); - -#if 0 && defined(Q_OS_WINDOWS) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - QApplication::setFont([]() { - QFont f("Microsoft YaHei"); - f.setStyleStrategy(QFont::PreferAntialias); - f.setPixelSize(15); - return f; - }()); -#endif MainWindow w; w.show(); -#if 0 - QMainWindow w2; - w2.show(); -#endif return a.exec(); } diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 14227ea..7492b43 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -37,11 +37,14 @@ protected: void timerEvent(QTimerEvent *event) override { + QLabel::timerEvent(event); setText(QTime::currentTime().toString(QStringLiteral("hh:mm:ss"))); } }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { + setAttribute(Qt::WA_DontCreateNativeAncestors); + installWindowAgent(); #if 1 @@ -128,14 +131,6 @@ return QMainWindow::event(event); } - -void MainWindow::closeEvent(QCloseEvent *event) { - // if (!(qApp->keyboardModifiers() & Qt::ControlModifier)) { - // QTimer::singleShot(1000, this, &QWidget::show); - // } - event->accept(); -} - void MainWindow::installWindowAgent() { // 1. Setup window agent windowAgent = new QWK::WidgetWindowAgent(this); @@ -166,6 +161,11 @@ }); #ifdef Q_OS_WIN + auto noneAction = new QAction(tr("None"), menuBar); + noneAction->setData(QStringLiteral("none")); + noneAction->setCheckable(true); + noneAction->setChecked(true); + auto dwmBlurAction = new QAction(tr("Enable DWM blur"), menuBar); dwmBlurAction->setData(QStringLiteral("dwm-blur")); dwmBlurAction->setCheckable(true); @@ -183,19 +183,27 @@ micaAltAction->setCheckable(true); auto winStyleGroup = new QActionGroup(menuBar); - // At most one action can be checked at any one time. The actions can also be all unchecked. - winStyleGroup->setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional); + winStyleGroup->addAction(noneAction); winStyleGroup->addAction(dwmBlurAction); winStyleGroup->addAction(acrylicAction); winStyleGroup->addAction(micaAction); winStyleGroup->addAction(micaAltAction); connect(winStyleGroup, &QActionGroup::triggered, this, [this, winStyleGroup](QAction *action) { // Unset all custom style attributes first, otherwise the style will not display correctly - for (auto _act : winStyleGroup->actions()) { - windowAgent->setWindowAttribute(_act->data().toString(), false); + for (const QAction* _act : winStyleGroup->actions()) { + const QString data = _act->data().toString(); + if (data.isEmpty() || data == QStringLiteral("none")) { + continue; + } + windowAgent->setWindowAttribute(data, false); } - windowAgent->setWindowAttribute(action->data().toString(), action->isChecked()); - setProperty("custom-style", action->isChecked()); + const QString data = action->data().toString(); + if (data == QStringLiteral("none")) { + setProperty("custom-style", false); + } else if (!data.isEmpty()) { + windowAgent->setWindowAttribute(data, true); + setProperty("custom-style", true); + } style()->polish(this); }); @@ -248,6 +256,7 @@ #ifdef Q_OS_WIN settings->addSeparator(); + settings->addAction(noneAction); settings->addAction(dwmBlurAction); settings->addAction(acrylicAction); settings->addAction(micaAction); diff --git a/examples/mainwindow/mainwindow.h b/examples/mainwindow/mainwindow.h index 9af4d73..53514a1 100644 --- a/examples/mainwindow/mainwindow.h +++ b/examples/mainwindow/mainwindow.h @@ -30,8 +30,6 @@ protected: bool event(QEvent *event) override; - void closeEvent(QCloseEvent *event) override; - private: void installWindowAgent(); void loadStyleSheet(Theme theme); diff --git a/examples/qml/QWKButton.qml b/examples/qml/QWKButton.qml index 91569f7..e781d69 100644 --- a/examples/qml/QWKButton.qml +++ b/examples/qml/QWKButton.qml @@ -20,6 +20,7 @@ mipmap: true width: 12 height: 12 + fillMode: Image.PreserveAspectFit } } background: Rectangle { diff --git a/examples/qml/main.cpp b/examples/qml/main.cpp index 3daf57f..be303df 100644 --- a/examples/qml/main.cpp +++ b/examples/qml/main.cpp @@ -4,22 +4,32 @@ #include <QtGui/QGuiApplication> #include <QtQml/QQmlApplicationEngine> +#include <QtQml/QQmlContext> #include <QtQuick/QQuickWindow> #include <QWKQuick/qwkquickglobal.h> +#ifdef Q_OS_WIN +// Indicates to hybrid graphics systems to prefer the discrete part by default. +extern "C" { + Q_DECL_EXPORT unsigned long NvOptimusEnablement = 0x00000001; + Q_DECL_EXPORT int AmdPowerXpressRequestHighPerformance = 1; +} +#endif + int main(int argc, char *argv[]) { - qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); + qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); // or "new": create a separate console window qputenv("QSG_INFO", "1"); + qputenv("QSG_NO_VSYNC", "1"); #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) qputenv("QT_QUICK_CONTROLS_STYLE", "Basic"); #else qputenv("QT_QUICK_CONTROLS_STYLE", "Default"); #endif -#if 0 - qputenv("QSG_RHI_BACKEND", "opengl"); - //qputenv("QSG_RHI_HDR", "scrgb"); + //qputenv("QSG_RHI_BACKEND", "opengl"); // other options: d3d11, d3d12, vulkan + //qputenv("QSG_RHI_HDR", "scrgb"); // other options: hdr10, p3 //qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1"); +#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QGuiApplication::setHighDpiScaleFactorRoundingPolicy( Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif @@ -27,6 +37,12 @@ // Make sure alpha channel is requested, our special effects on Windows depends on it. QQuickWindow::setDefaultAlphaBuffer(true); QQmlApplicationEngine engine; +#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) + const bool curveRenderingAvailable = true; +#else + const bool curveRenderingAvailable = false; +#endif + engine.rootContext()->setContextProperty(QStringLiteral("$curveRenderingAvailable"), QVariant(curveRenderingAvailable)); QWK::registerTypes(&engine); engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); return application.exec(); diff --git a/examples/qml/main.qml b/examples/qml/main.qml index e27239c..d9ba183 100644 --- a/examples/qml/main.qml +++ b/examples/qml/main.qml @@ -12,6 +12,7 @@ title: qsTr("Hello, world!") Component.onCompleted: { windowAgent.setup(window) + windowAgent.setWindowAttribute("dark-mode", true) window.visible = true } @@ -35,10 +36,9 @@ id: windowAgent } - MouseArea { - anchors.fill: parent + TapHandler { acceptedButtons: Qt.RightButton - onClicked: contextMenu.open() + onTapped: contextMenu.open() } Rectangle { @@ -63,6 +63,7 @@ height: 18 mipmap: true source: "qrc:///app/example.png" + fillMode: Image.PreserveAspectFit Component.onCompleted: windowAgent.setSystemButton(WindowAgent.WindowIcon, iconButton) } @@ -140,6 +141,11 @@ bold: true } color: "#FEFEFE" + Component.onCompleted: { + if ($curveRenderingAvailable) { + timeLabel.renderType = Text.CurveRendering + } + } } Menu { @@ -163,6 +169,7 @@ MenuItem { text: qsTr("Dark") checkable: true + checked: true onTriggered: windowAgent.setWindowAttribute("dark-mode", true) } } @@ -178,11 +185,28 @@ MenuItem { enabled: Qt.platform.os === "windows" + text: qsTr("None") + checkable: true + checked: true + onTriggered: { + window.color = darkStyle.windowBackgroundColor + windowAgent.setWindowAttribute("dwm-blur", false) + windowAgent.setWindowAttribute("acrylic-material", false) + windowAgent.setWindowAttribute("mica", false) + windowAgent.setWindowAttribute("mica-alt", false) + } + } + + MenuItem { + enabled: Qt.platform.os === "windows" text: qsTr("DWM blur") checkable: true onTriggered: { - window.color = checked ? "transparent" : darkStyle.windowBackgroundColor - windowAgent.setWindowAttribute("dwm-blur", checked) + window.color = "transparent" + windowAgent.setWindowAttribute("acrylic-material", false) + windowAgent.setWindowAttribute("mica", false) + windowAgent.setWindowAttribute("mica-alt", false) + windowAgent.setWindowAttribute("dwm-blur", true) } } @@ -191,8 +215,11 @@ text: qsTr("Acrylic material") checkable: true onTriggered: { - window.color = checked ? "transparent" : darkStyle.windowBackgroundColor - windowAgent.setWindowAttribute("acrylic-material", checked) + window.color = "transparent" + windowAgent.setWindowAttribute("dwm-blur", false) + windowAgent.setWindowAttribute("mica", false) + windowAgent.setWindowAttribute("mica-alt", false) + windowAgent.setWindowAttribute("acrylic-material", true) } } @@ -201,8 +228,11 @@ text: qsTr("Mica") checkable: true onTriggered: { - window.color = checked ? "transparent" : darkStyle.windowBackgroundColor - windowAgent.setWindowAttribute("mica", checked) + window.color = "transparent" + windowAgent.setWindowAttribute("dwm-blur", false) + windowAgent.setWindowAttribute("acrylic-material", false) + windowAgent.setWindowAttribute("mica-alt", false) + windowAgent.setWindowAttribute("mica", true) } } @@ -211,8 +241,11 @@ text: qsTr("Mica Alt") checkable: true onTriggered: { - window.color = checked ? "transparent" : darkStyle.windowBackgroundColor - windowAgent.setWindowAttribute("mica-alt", checked) + window.color = "transparent" + windowAgent.setWindowAttribute("dwm-blur", false) + windowAgent.setWindowAttribute("acrylic-material", false) + windowAgent.setWindowAttribute("mica", false) + windowAgent.setWindowAttribute("mica-alt", true) } } } -- Gitblit v1.9.1