QML无边框窗体怎么能调整大小?



  • 具体来说就是,窗口右下角那个东西用来调整大小的,但是无边框之后那个东西没有了

    ApplicationWindow {
        flags:  Qt.Window|Qt.FramelessWindowHint
    }
    

    无边框是这样设置的


  • 网站研运

    @hezhongfeng 还是可以通过MouseArea捕捉鼠标按下事件,然后借此通过修改ApplicationWindow的width和height可以制作更改窗口大小的小功能。



  • @jcy
    明白了,看来只能自己弄,并没有原生部件提供.Thanks



  • @hezhongfeng 写了一些代码。如下。希望能够提供一些思路给你。

    如下是: BackgroundItem.qml

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    
    Item {
        id: backgoundItem
        property ApplicationWindow mainWindow: null
        property real windowBorderWidth: 10
    
        readonly property rect leftTop:  Qt.rect(0, 0, windowBorderWidth, windowBorderWidth) // left top
        readonly property rect leftSide: Qt.rect(0, windowBorderWidth, windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // left side
        readonly property rect leftBottom: Qt.rect(0, backgoundItem.height-windowBorderWidth, windowBorderWidth, windowBorderWidth) // left bottom
        
        readonly property rect topSide: Qt.rect(windowBorderWidth, 0, backgoundItem.width-2*windowBorderWidth, windowBorderWidth) // top side
        readonly property rect center: Qt.rect(windowBorderWidth, windowBorderWidth, backgoundItem.width-2*windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // center
        readonly property rect bottomSide: Qt.rect(windowBorderWidth, backgoundItem.height-windowBorderWidth,
                                                   backgoundItem.width-2*windowBorderWidth,  backgoundItem.width-2*windowBorderWidth) // bottom
        
        readonly property rect rightTop: Qt.rect(backgoundItem.width-windowBorderWidth, 0, windowBorderWidth, windowBorderWidth) // right top
        readonly property rect rightSide: Qt.rect(backgoundItem.width-windowBorderWidth, windowBorderWidth, windowBorderWidth, backgoundItem.height - 2 * windowBorderWidth) // right side
        readonly property rect rightBottom: Qt.rect(backgoundItem.width-windowBorderWidth, backgoundItem.height-windowBorderWidth, windowBorderWidth, windowBorderWidth) // left bottom
    
        Rectangle {
            anchors.fill: parent
            // anchors.margins: windowBorderWidth
            color: "white"
            border.width: 1
            border.color: "#ccc"
            // 最底层
        }
    
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            property point oldPosition
    
            Component.onCompleted: {
                oldPosition = Qt.point(mouseX, mouseY);
            }
    
            onPositionChanged: {
                var currentPosition = Qt.point(mouse.x, mouse.y);
    
                var p = Qt.point(mouse.x, mouse.y);
    
                if(rectContains(leftTop, p) || rectContains(rightBottom, p)) {
                    cursorShape = Qt.SizeFDiagCursor;
                    if(pressed) {
    
    //                    mainWindow.x = mouse.x;
    //                    mainWindow.y = mouse.y;
                        var height_d = currentPosition.y - oldPosition.y;
                        var width_d =  currentPosition.x - oldPosition.x;
                        mainWindow.x += width_d
                         mainWindow.y += height_d
                        mainWindow.width += width_d;
                        mainWindow.height += height_d;
    
    
                        //console.log("pressed")
    
                    }
    
                    return;
                } else if(rectContains(leftBottom, p) || rectContains(rightTop, p)) {
                    cursorShape = Qt.SizeBDiagCursor;
                    return;
                } else if(rectContains(topSide, p) || rectContains(bottomSide, p)) {
                    cursorShape = Qt.SizeVerCursor;
                    return;
                } else if(rectContains(rightSide, p) || rectContains(leftSide, p)) {
                    cursorShape = Qt.SizeHorCursor;
                    return;
                }
    
                cursorShape = Qt.ArrowCursor;
                oldPosition = Qt.point(mouse.x, mouse.y);
            }
    
    
        }
        function rectContains(rect, point) {
            var x_d = point.x - rect.x;
            var y_d = point.y - rect.y;
    
            return (x_d > 0 && x_d < rect.width)
                    && (y_d > 0 && y_d < rect.height);
        }
    }
    

    main.qml 中使用:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Controls.Styles 1.4
    
    
    ApplicationWindow {
        id: app
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        flags:  Qt.Window|Qt.FramelessWindowHint
    
        property real windowBorderWidth: 10
    
        style: ApplicationWindowStyle {
            background:
                BackgoundItem {
                id: backgoundItem
                 windowBorderWidth: app.windowBorderWidth
                 mainWindow: app
            }
        }
    
        Rectangle {
            anchors.fill: parent
            anchors.margins: 10
            color: "yellow"
            focus: true
            Keys.onEscapePressed: Qt.quit()
        }
    }
    

    这样。当鼠标在窗体边上时,就是改变鼠标图标,然后实现一些逻辑即可。不过上诉代码仅仅只是思路,具体的代码逻辑需要自行解决。



  • @qyvlikQML无边框窗体怎么能调整大小? 中说:

    就是改变鼠标图标,然后实现一些逻辑即可

    谢谢。
    在官方论坛也问了,基本还是得自己做。
    思路和这个差不多
    目前还是学习阶段



  • @hezhongfeng 你这个问题解决了吗?我也在寻求相关的资料。菜鸟表示很头疼


 

最近的回复

  • 这个很有意思 ^_^

    阅读更多
  • 思路:
    1.把官方时钟demo改了,加了秒针,加了壁纸,加了小萝莉
    2.QWindow窗口嵌入到桌面

    0_1533891238039_20180810_164211.gif

    改版

    去掉秒针,把小萝莉正过来,沿着表盘走

    0_1533893719543_20180810_173309.gif

    源代码

    Fork me on Gitee

    阅读更多
  • 思路:
    1.识别鼠标移动事件
    2.QPropertyAnimation实现移动动画
    3.QLabel和QMovie使用gif

    0_1533808278715_20180809_174840.gif

    阅读更多
  • 很多小软件,实现了很好的功能。给你点赞!

    阅读更多

关注我们

微博
QQ群











召唤伊斯特瓦尔