You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qml无边框窗口还原时左上角出现标题的问题求助

解决QML无边框窗口最小化还原后左上角出现标题的问题

问题分析

你使用Qt.FramelessWindowHint实现无边框窗口,但点击最小化按钮还原后,窗口左上角出现了标题。这是因为ApplicationWindow默认启用了标题栏组件,即便设置了无边框标志,在窗口状态切换时,系统或Qt内部可能会触发标题栏的短暂渲染,导致标题显示。

解决方案

只需在ApplicationWindow中添加两个关键设置:

  • 设置title: "":清空窗口标题,避免系统显示默认标题文本
  • 设置titleBarVisible: false:彻底禁用ApplicationWindow自带的标题栏组件

修改后的完整代码

import QtQuick
import QtQuick.Window
import QtQuick.Controls

ApplicationWindow {
    id: window
    width: 700
    height:500
    color: "#00000000"

    flags: Qt.Window | Qt.FramelessWindowHint
    // 新增的关键设置
    title: ""
    titleBarVisible: false

    property alias windowBg: windowBg
    property alias btnMinimize: btnMinimize
    property alias btnMaximize: btnMaximize
    property alias btnClose: btnClose

    // Propeties
    property int windowStatus: 1
    property int windowMargin: 30

    // Internal functions
    QtObject{
        id: internal

        function resetResizeBorders(){
            // Resize visibility
            resizeLeft.visible = true
            resizeRight.visible = true
            resizeBottom.visible = true
        }

        function maximizeRestore(){
            if(windowStatus == 0){
                window.showMaximized()
                windowStatus = 1
                windowMargin = 0
                // Resize visibility
                resizeLeft.visible = false
                resizeRight.visible = false
                resizeBottom.visible = false
            }
            else{
                window.showNormal()
                windowStatus = 0
                windowMargin = 30
                // Resize visibility
                internal.resetResizeBorders()
            }
        }

        function ifMaximizedWindowRestore(){
            if(windowStatus == 1){
                window.showNormal()
                windowStatus = 0
                windowMargin = 30
                internal.resetResizeBorders()
            }
        }

        function restoreMargins(){
            windowStatus = 0
            windowMargin = 30
            internal.resetResizeBorders()
        }
    }

    Rectangle {
        id: windowBg
        radius: windowStatus ? 0 : 6
        anchors.fill: parent
        anchors.margins: windowMargin
        z: 1

        Row {
            id: rowBtns
            width: 105
            height: 35
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.topMargin: 0
            anchors.rightMargin: 20
            z: 1
            Button{
                id: btnMinimize
                width: 40
                height: width
                objectName: 'minimize'
                onClicked: {
                    window.showMinimized()
                    internal.restoreMargins()
                }
                background: Rectangle{
                    color: 'red'
                }
            }

            Button {
                id: btnMaximize
                width: 40
                height: width
                objectName: 'maximize'
                onClicked: internal.maximizeRestore()
                background: Rectangle{
                    color: 'green'
                }
            }

            Button {
                id: btnClose
                width: 40
                height: width
                objectName: 'close'
                onClicked: window.close()
                background: Rectangle{
                    color: 'yellow'
                }
            }
        }
    }

    MouseArea {
        id: resizeLeft
        width: 10
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.leftMargin: 0
        anchors.bottomMargin: 10
        anchors.topMargin: 10
        cursorShape: Qt.SizeHorCursor

        DragHandler{
            target: null
            onActiveChanged: {
                if (active) { window.startSystemResize(Qt.LeftEdge) }}
        }
    }

    MouseArea {
        id: resizeRight
        width: 10
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.rightMargin: 0
        anchors.bottomMargin: 10
        anchors.topMargin: 10
        cursorShape: Qt.SizeHorCursor

        DragHandler{
            target: null
            onActiveChanged: if (active) { window.startSystemResize(Qt.RightEdge) }
        }
    }

    MouseArea {
        id: resizeBottom
        height: 10
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.rightMargin: 10
        anchors.leftMargin: 10
        anchors.bottomMargin: 0
        cursorShape: Qt.SizeVerCursor

        DragHandler{
            target: null
            onActiveChanged: if (active) { window.startSystemResize(Qt.BottomEdge)}
        }
    }

    MouseArea {
        id: moveWindow
        height: 40
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.topMargin: 0
        anchors.rightMargin: 0
        anchors.leftMargin: 10

        DragHandler {
            target: null
            onActiveChanged: if(active){
                                 window.startSystemMove()
                                 internal.ifMaximizedWindowRestore()
                             }
        }
        onDoubleClicked: internal.maximizeRestore()
    }

    Component.onCompleted:{
        internal.maximizeRestore()
    }
}

额外说明

如果上述设置后仍有问题,可以尝试在restoreMargins函数中重新设置窗口flags,确保无边框状态始终生效:

function restoreMargins(){
    windowStatus = 0
    windowMargin = 30
    internal.resetResizeBorders()
    // 重新设置flags,确保无边框状态
    window.flags = Qt.Window | Qt.FramelessWindowHint
}

内容的提问来源于stack exchange,提问作者Tolia Shpag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 07:53:18