From 236ac54faf4cc5a81386aedb307d6d02c15525d1 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Mon, 27 Jul 2020 17:26:21 +0800 Subject: [PATCH] Update. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- main_windows.cpp | 35 +++++++++++++++++++++++++++++++++-- resources/qml/main.qml | 29 ++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/main_windows.cpp b/main_windows.cpp index bca4931..7535256 100644 --- a/main_windows.cpp +++ b/main_windows.cpp @@ -7,9 +7,38 @@ #include "framelessquickhelper.h" #include #endif +#include #include #include +class FramelessWidget : public QWidget +{ + Q_OBJECT + Q_DISABLE_COPY_MOVE(FramelessWidget) + +public: + explicit FramelessWidget(QWidget *parent = nullptr) : QWidget(parent) {} + ~FramelessWidget() override = default; + +protected: + void paintEvent(QPaintEvent *event) override + { + QWidget::paintEvent(event); + QPainter painter(this); + painter.save(); + painter.setPen(isActiveWindow() ? borderColor_active : borderColor_inactive); + painter.drawLine(0, 0, width(), 0); + painter.drawLine(0, height(), width(), height()); + painter.drawLine(0, 0, 0, height()); + painter.drawLine(width(), 0, width(), height()); + painter.restore(); + } + +private: + const QColor borderColor_active = {"#707070"}; + const QColor borderColor_inactive = {"#aaaaaa"}; +}; + int main(int argc, char *argv[]) { // High DPI scaling is enabled by default from Qt 6 @@ -41,8 +70,8 @@ int main(int argc, char *argv[]) QApplication application(argc, argv); // Qt Widgets example: - QWidget widget; - widget.setContentsMargins(0, 0, 0, 0); + FramelessWidget widget; + widget.setContentsMargins(2, 2, 2, 2); QLabel *label = new QLabel; QObject::connect(&widget, &QWidget::windowTitleChanged, label, &QLabel::setText); QPushButton *minimizeButton = new QPushButton; @@ -113,3 +142,5 @@ int main(int argc, char *argv[]) return QApplication::exec(); } + +#include "main_windows.moc" diff --git a/resources/qml/main.qml b/resources/qml/main.qml index 597c255..1b1ecc4 100644 --- a/resources/qml/main.qml +++ b/resources/qml/main.qml @@ -13,13 +13,23 @@ Window { id: framelessHelper } + Rectangle { + anchors.fill: parent + border.color: Qt.application.state === Qt.ApplicationActive ? "#707070" : "#aaaaaa" + } + Rectangle { id: titleBar height: framelessHelper.titleBarHeight color: "white" - anchors.top: parent.top - anchors.left: parent.left - anchors.right: parent.right + anchors { + top: parent.top + topMargin: 1 + left: parent.left + leftMargin: 1 + right: parent.right + rightMargin: 1 + } Text { id: titleBarText @@ -69,10 +79,15 @@ Window { Rectangle { id: content color: "#f0f0f0" - anchors.top: titleBar.bottom - anchors.bottom: parent.bottom - anchors.left: parent.left - anchors.right: parent.right + anchors { + top: titleBar.bottom + bottom: parent.bottom + bottomMargin: 1 + left: parent.left + leftMargin: 1 + right: parent.right + rightMargin: 1 + } } Component.onCompleted: framelessHelper.removeWindowFrame()