本页内容

Drawer QML Type

可通过滑动手势打开和关闭的侧边栏。更多...

Import Statement: import QtQuick.Controls
Inherits:

Popup

属性

详细说明

Drawer 提供了一个基于滑动操作的侧边面板,类似于触摸界面中常用的侧边面板,用于提供一个集中式的导航位置。

Drawer 可定位在内容项的任意一侧边缘。上图中的 Drawer 位于窗口左侧边缘。通过从窗口左侧边缘“拖出”即可打开 Drawer。

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: window
    visible: true

    Drawer {
        id: drawer
        width: 0.66 * window.width
        height: window.height

        Label {
            text: "Content goes here!"
            anchors.centerIn: parent
        }
    }
}

Drawer 是一种特殊类型的弹出窗口,驻留在窗口的某个边缘edges 。默认情况下,Drawer 会将自身父元素重设为窗口overlay ,因此基于窗口坐标系运行。也可以手动将parent 设置为其他值,使抽屉在特定的坐标系中运行。

可以配置 Drawer 使其仅覆盖窗口边缘的一部分。以下示例演示了如何定位 Drawer,使其显示在窗口标题栏下方:

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: window
    visible: true

    header: ToolBar { }

    Drawer {
        y: header.height
        width: window.width * 0.6
        height: window.height - header.height
    }
}

position 属性通过在0.0 和1.0 之间的数值,决定Drawer的可见范围。无法将Drawer的x坐标(或水平边距)设置在窗口左侧或右侧边缘,也无法将其y坐标(或垂直边距)设置在窗口顶部或底部边缘。

在上图中,应用程序的内容被“推”到了屏幕的另一侧。这是通过对内容应用平移操作实现的:

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: window
    width: 200
    height: 228
    visible: true

    Drawer {
        id: drawer
        width: 0.66 * window.width
        height: window.height
    }

    Label {
        id: content

        text: "Aa"
        font.pixelSize: 96
        anchors.fill: parent
        verticalAlignment: Label.AlignVCenter
        horizontalAlignment: Label.AlignHCenter

        transform: Translate {
            x: drawer.position * content.width * 0.33
        }
    }
}

若希望在抽屉打开时应用程序内容保持原位,请勿应用平移操作。

通过将 Drawer 设置为non-modal 和non-interactive ,可将其配置为不可关闭的持久侧边面板。更多详情请参阅“示例库”中的示例。

注意:在 某些平台上 ,特定边缘可能被系统手势预留,因此无法与Drawer配合使用。例如,在Android和iOS系统中,顶部和底部边缘可能被预留用于系统通知和控制中心。

另请参阅 SwipeView 、自定义 Drawer、导航控件和弹出控件。

属性文档

dragMargin : real

此属性用于指定距离屏幕边缘一定范围内的区域,在此范围内执行拖动操作将打开抽屉。将该值设置为0 或更小,可防止通过拖动操作打开抽屉。

默认值为Application.styleHints.startDragDistance 。

另请参阅 interactive 。

edge : enumeration

该属性指定抽屉开启时所对准的窗口边缘。允许的取值包括:

常量描述
Qt.TopEdge窗口的顶部边缘。
Qt.LeftEdge窗口的左边缘(默认)。
Qt.RightEdge窗口的右边缘。
Qt.BottomEdge窗口的底部边缘。

interactive : bool [since QtQuick.Controls 2.2 (Qt 5.9)]

该属性用于指定抽屉是否具有交互性。非交互式抽屉不会对滑动操作做出反应。

默认值为true 。

该属性在 QtQuick.Controls 2.2(Qt 5.9)中引入。

另请参阅 dragMargin 。

position : real

该属性保存了抽屉相对于其最终目标位置的当前位置。也就是说,当抽屉完全关闭时,其位置为0.0 ;当完全打开时,其位置为1.0 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.