forked from github_mirror/framelesshelper
Update Qt Quick example.
Fixes: #11 Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
parent
2df22bdb64
commit
9a70ebaaa1
|
@ -4,37 +4,14 @@
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#ifdef QT_QUICK_LIB
|
#ifdef QT_QUICK_LIB
|
||||||
#include <QQmlContext>
|
#include <QQmlApplicationEngine>
|
||||||
#include <QQuickItem>
|
#include <QWindow>
|
||||||
#include <QQuickView>
|
|
||||||
#endif
|
#endif
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
static const int m_defaultButtonWidth = 45;
|
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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// High DPI scaling is enabled by default from Qt 6
|
// High DPI scaling is enabled by default from Qt 6
|
||||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
@ -119,46 +96,37 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
#ifdef QT_QUICK_LIB
|
#ifdef QT_QUICK_LIB
|
||||||
// Qt Quick example:
|
// Qt Quick example:
|
||||||
MyQuickView view;
|
QQmlApplicationEngine engine;
|
||||||
const auto hWnd_qml = reinterpret_cast<HWND>(view.winId());
|
const QUrl url(QString::fromUtf8("qrc:///qml/main.qml"));
|
||||||
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")));
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
&view, &MyQuickView::windowSizeChanged, [hWnd_qml](const QSize &size) {
|
&engine, &QQmlApplicationEngine::objectCreated, &application,
|
||||||
const auto data_qml = WinNativeEventFilter::windowData(hWnd_qml);
|
[&url](QObject *obj, const QUrl &objUrl) {
|
||||||
if (data_qml) {
|
if (!obj && (url == objUrl)) {
|
||||||
|
QCoreApplication::exit(-1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Qt::QueuedConnection);
|
||||||
|
engine.load(url);
|
||||||
|
const auto window = qobject_cast<QWindow *>(engine.rootObjects().at(0));
|
||||||
|
Q_ASSERT(window);
|
||||||
|
const auto hWnd_qml = reinterpret_cast<HWND>(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(
|
const int tbh_qml = WinNativeEventFilter::getSystemMetric(
|
||||||
hWnd_qml,
|
hWnd_qml,
|
||||||
WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
WinNativeEventFilter::SystemMetric::TitleBarHeight,
|
||||||
data_qml->draggableAreas = {
|
false);
|
||||||
{0, 0, (size.width() - (m_defaultButtonWidth * 3)),
|
data->draggableAreas = {
|
||||||
|
{0, 0, (window->width() - (m_defaultButtonWidth * 3)),
|
||||||
tbh_qml}};
|
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
|
#endif
|
||||||
|
|
||||||
return QApplication::exec();
|
return QApplication::exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef QT_QUICK_LIB
|
|
||||||
#include "main_windows.moc"
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
import QtQuick 2.15
|
import QtQuick 2.15
|
||||||
|
import QtQuick.Window 2.15
|
||||||
|
|
||||||
Item {
|
Window {
|
||||||
id: root
|
id: root
|
||||||
|
visible: true
|
||||||
signal minimizeButtonClicked
|
width: 800
|
||||||
signal maximizeButtonClicked
|
height: 600
|
||||||
signal restoreButtonClicked
|
title: qsTr("Hello, World!")
|
||||||
signal closeButtonClicked
|
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
id: titleBar
|
id: titleBar
|
||||||
height: $TitleBarHeight
|
height: 30
|
||||||
color: "white"
|
color: "white"
|
||||||
anchors.top: parent.top
|
anchors.top: parent.top
|
||||||
anchors.left: parent.left
|
anchors.left: parent.left
|
||||||
|
@ -18,9 +18,9 @@ Item {
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
id: titleBarText
|
id: titleBarText
|
||||||
text: qsTr("Hello, World!")
|
text: root.title
|
||||||
font.family: "Noto Sans CJK SC"
|
font.family: "Noto Sans CJK SC"
|
||||||
font.pointSize: 15
|
font.pointSize: 13
|
||||||
color: "black"
|
color: "black"
|
||||||
anchors.left: parent.left
|
anchors.left: parent.left
|
||||||
anchors.leftMargin: 15
|
anchors.leftMargin: 15
|
||||||
|
@ -32,23 +32,22 @@ Item {
|
||||||
anchors.right: parent.right
|
anchors.right: parent.right
|
||||||
|
|
||||||
MinimizeButton {
|
MinimizeButton {
|
||||||
onClicked: root.minimizeButtonClicked()
|
onClicked: root.showMinimized()
|
||||||
}
|
}
|
||||||
|
|
||||||
MaximizeButton {
|
MaximizeButton {
|
||||||
|
maximized: root.visibility === 4
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (maximized) {
|
if (maximized) {
|
||||||
root.restoreButtonClicked()
|
root.showNormal()
|
||||||
maximized = false
|
|
||||||
} else {
|
} else {
|
||||||
root.maximizeButtonClicked()
|
root.showMaximized()
|
||||||
maximized = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseButton {
|
CloseButton {
|
||||||
onClicked: root.closeButtonClicked()
|
onClicked: root.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue