From 9a70ebaaa1146320e57374513f0ab48abd190ea1 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Mon, 11 May 2020 22:40:13 +0800 Subject: [PATCH] Update Qt Quick example. Fixes: #11 Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- main_windows.cpp | 90 ++++++++++++++---------------------------- resources/qml/main.qml | 29 +++++++------- 2 files changed, 43 insertions(+), 76 deletions(-) diff --git a/main_windows.cpp b/main_windows.cpp index d9f93b6..3117b1e 100644 --- a/main_windows.cpp +++ b/main_windows.cpp @@ -4,37 +4,14 @@ #include #include #ifdef QT_QUICK_LIB -#include -#include -#include +#include +#include #endif #include #include static const int m_defaultButtonWidth = 45; -#ifdef QT_QUICK_LIB -class MyQuickView : public QQuickView { - Q_OBJECT - Q_DISABLE_COPY_MOVE(MyQuickView) - -public: - explicit MyQuickView(QWindow *parent = nullptr) : QQuickView(parent) { - setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView); - } - ~MyQuickView() override = default; - -protected: - void resizeEvent(QResizeEvent *event) override { - QQuickView::resizeEvent(event); - Q_EMIT windowSizeChanged(event->size()); - } - -Q_SIGNALS: - void windowSizeChanged(const QSize &); -}; -#endif - int main(int argc, char *argv[]) { // High DPI scaling is enabled by default from Qt 6 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) @@ -119,46 +96,37 @@ int main(int argc, char *argv[]) { #ifdef QT_QUICK_LIB // Qt Quick example: - MyQuickView view; - const auto hWnd_qml = reinterpret_cast(view.winId()); - const int tbh_qml = WinNativeEventFilter::getSystemMetric( - hWnd_qml, WinNativeEventFilter::SystemMetric::TitleBarHeight, false); - view.rootContext()->setContextProperty(QString::fromUtf8("$TitleBarHeight"), - tbh_qml); - view.setSource(QUrl(QString::fromUtf8("qrc:///qml/main.qml"))); + QQmlApplicationEngine engine; + const QUrl url(QString::fromUtf8("qrc:///qml/main.qml")); QObject::connect( - &view, &MyQuickView::windowSizeChanged, [hWnd_qml](const QSize &size) { - const auto data_qml = WinNativeEventFilter::windowData(hWnd_qml); - if (data_qml) { - const int tbh_qml = WinNativeEventFilter::getSystemMetric( - hWnd_qml, - WinNativeEventFilter::SystemMetric::TitleBarHeight, false); - data_qml->draggableAreas = { - {0, 0, (size.width() - (m_defaultButtonWidth * 3)), - tbh_qml}}; + &engine, &QQmlApplicationEngine::objectCreated, &application, + [&url](QObject *obj, const QUrl &objUrl) { + if (!obj && (url == objUrl)) { + QCoreApplication::exit(-1); + } + }, + Qt::QueuedConnection); + engine.load(url); + const auto window = qobject_cast(engine.rootObjects().at(0)); + Q_ASSERT(window); + const auto hWnd_qml = reinterpret_cast(window->winId()); + WinNativeEventFilter::addFramelessWindow(hWnd_qml, nullptr, true); + QObject::connect( + window, &QWindow::widthChanged, [window, hWnd_qml](const int arg) { + if (arg >= window->minimumWidth()) { + const auto data = WinNativeEventFilter::windowData(hWnd_qml); + if (data) { + const int tbh_qml = WinNativeEventFilter::getSystemMetric( + hWnd_qml, + WinNativeEventFilter::SystemMetric::TitleBarHeight, + false); + data->draggableAreas = { + {0, 0, (window->width() - (m_defaultButtonWidth * 3)), + tbh_qml}}; + } } }); - const QQuickItem *const rootObject = view.rootObject(); - Q_ASSERT(rootObject); - // We can't use the Qt5 syntax here because we can't get the function - // pointers of the signals written in QML. - QObject::connect(rootObject, SIGNAL(minimizeButtonClicked()), &view, - SLOT(showMinimized())); - QObject::connect(rootObject, SIGNAL(maximizeButtonClicked()), &view, - SLOT(showMaximized())); - QObject::connect(rootObject, SIGNAL(restoreButtonClicked()), &view, - SLOT(showNormal())); - QObject::connect(rootObject, SIGNAL(closeButtonClicked()), &view, - SLOT(close())); - WinNativeEventFilter::addFramelessWindow(hWnd_qml); - view.resize(800, 600); - WinNativeEventFilter::moveWindowToDesktopCenter(hWnd_qml); - view.show(); #endif return QApplication::exec(); } - -#ifdef QT_QUICK_LIB -#include "main_windows.moc" -#endif diff --git a/resources/qml/main.qml b/resources/qml/main.qml index 88ef828..662380f 100644 --- a/resources/qml/main.qml +++ b/resources/qml/main.qml @@ -1,16 +1,16 @@ import QtQuick 2.15 +import QtQuick.Window 2.15 -Item { +Window { id: root - - signal minimizeButtonClicked - signal maximizeButtonClicked - signal restoreButtonClicked - signal closeButtonClicked + visible: true + width: 800 + height: 600 + title: qsTr("Hello, World!") Rectangle { id: titleBar - height: $TitleBarHeight + height: 30 color: "white" anchors.top: parent.top anchors.left: parent.left @@ -18,9 +18,9 @@ Item { Text { id: titleBarText - text: qsTr("Hello, World!") + text: root.title font.family: "Noto Sans CJK SC" - font.pointSize: 15 + font.pointSize: 13 color: "black" anchors.left: parent.left anchors.leftMargin: 15 @@ -32,23 +32,22 @@ Item { anchors.right: parent.right MinimizeButton { - onClicked: root.minimizeButtonClicked() + onClicked: root.showMinimized() } MaximizeButton { + maximized: root.visibility === 4 onClicked: { if (maximized) { - root.restoreButtonClicked() - maximized = false + root.showNormal() } else { - root.maximizeButtonClicked() - maximized = true + root.showMaximized() } } } CloseButton { - onClicked: root.closeButtonClicked() + onClicked: root.close() } } }