Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-07-27 17:26:21 +08:00
parent 1cd1b0cc2b
commit 236ac54faf
2 changed files with 55 additions and 9 deletions

View File

@ -7,9 +7,38 @@
#include "framelessquickhelper.h" #include "framelessquickhelper.h"
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#endif #endif
#include <QPainter>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QWidget> #include <QWidget>
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[]) 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
@ -41,8 +70,8 @@ int main(int argc, char *argv[])
QApplication application(argc, argv); QApplication application(argc, argv);
// Qt Widgets example: // Qt Widgets example:
QWidget widget; FramelessWidget widget;
widget.setContentsMargins(0, 0, 0, 0); widget.setContentsMargins(2, 2, 2, 2);
QLabel *label = new QLabel; QLabel *label = new QLabel;
QObject::connect(&widget, &QWidget::windowTitleChanged, label, &QLabel::setText); QObject::connect(&widget, &QWidget::windowTitleChanged, label, &QLabel::setText);
QPushButton *minimizeButton = new QPushButton; QPushButton *minimizeButton = new QPushButton;
@ -113,3 +142,5 @@ int main(int argc, char *argv[])
return QApplication::exec(); return QApplication::exec();
} }
#include "main_windows.moc"

View File

@ -13,13 +13,23 @@ Window {
id: framelessHelper id: framelessHelper
} }
Rectangle {
anchors.fill: parent
border.color: Qt.application.state === Qt.ApplicationActive ? "#707070" : "#aaaaaa"
}
Rectangle { Rectangle {
id: titleBar id: titleBar
height: framelessHelper.titleBarHeight height: framelessHelper.titleBarHeight
color: "white" color: "white"
anchors.top: parent.top anchors {
anchors.left: parent.left top: parent.top
anchors.right: parent.right topMargin: 1
left: parent.left
leftMargin: 1
right: parent.right
rightMargin: 1
}
Text { Text {
id: titleBarText id: titleBarText
@ -69,10 +79,15 @@ Window {
Rectangle { Rectangle {
id: content id: content
color: "#f0f0f0" color: "#f0f0f0"
anchors.top: titleBar.bottom anchors {
anchors.bottom: parent.bottom top: titleBar.bottom
anchors.left: parent.left bottom: parent.bottom
anchors.right: parent.right bottomMargin: 1
left: parent.left
leftMargin: 1
right: parent.right
rightMargin: 1
}
} }
Component.onCompleted: framelessHelper.removeWindowFrame() Component.onCompleted: framelessHelper.removeWindowFrame()