Qt Quick 实例(一)

QtQuick 无边框 透明窗体

main.cpp

#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);
    //设置无边框 背景透明窗体
    QQuickView viewer;
    viewer.setFlags(Qt::FramelessWindowHint);
    viewer.setColor(QColor(Qt::transparent));
    //加载qml qml添加到资源文件中避免qml暴露
    viewer.setSource(QUrl("qrc:/main.qml"));
    viewer.show();
    //将viewer设置为main.qml的属性
    viewer.rootContext()->setContextProperty("mainwindow", &viewer);
    return app.exec();
}

main.qml

import QtQuick 2.6
import QtQuick.Controls 2.1

Rectangle {
    width: 800
    height: 600

    color: Qt.rgba(0.8,0.8,0.8,0.6)
    MouseArea {
        id: dragRegion
        anchors.fill: parent
        property point clickPos: "0,0"
        onPressed: {
            clickPos = Qt.point(mouse.x, mouse.y)
        }
        onPositionChanged: {
            //鼠标偏移量
            var delta = Qt.point(mouse.x - clickPos.x, mouse.y - clickPos.y)
            mainwindow.setX(mainwindow.x + delta.x)
            mainwindow.setY(mainwindow.y + delta.y)
        }
    }
}

你可能感兴趣的:(QML)