QML控件类型:Drawer

本文介绍了QtQuick Controls中的Drawer组件,它是一个可从屏幕边缘滑动打开和关闭的侧边栏。Drawer可以从上下左右四个方向打开,并且可以通过设置dragMargin、edge和interactive属性进行定制。示例代码展示了如何改变背景颜色、添加边框以及与内容元素交互。此外,还讨论了position属性,用于控制Drawer的打开程度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、描述

Drawer 提供一个可以使用滑动手势打开和关闭的侧面板。继承自 Popup

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 定位为显示在窗口标题下方:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts 1.12

ApplicationWindow {
    id: window
    visible: true

    header: ToolBar
    {
        RowLayout {
          anchors.fill: parent
          ToolButton {
              text: "测试1"
          }
          ToolButton {
              text: "测试2"
          }
        }
    }

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

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

position 属性确定 Drawer 的可见程度,值介于 0.0 和 1.0 之间。以下代码使用一个和 Drawer 同级的 Label 来演示“Label 被 Drawer 推动”的效果:

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属性变化规律
            x: drawer.position * content.width * 0.33
        }
    }
}

 

二、自定义Drawer示例

import QtQuick
import QtQuick.Controls

ApplicationWindow {
    id: window
    visible: true

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

        background: Rectangle {
            color: "#128bf1"
            Rectangle {
                x: parent.width - 3
                width: 3
                height: parent.height
                color: "#ff0000"
            }
        }

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

三、属性成员

1、dragMargin : real

与屏幕边缘的距离,在该距离内拖动操作将打开 Drawer。设置为 小于等于 0 可禁用通过拖动打开Drawer。

默认值为 Qt.styleHints.startDragDistance

2、edge : enumeration

打开 Drawer 的窗口边缘。

  • Qt.TopEdge:上边缘。
  • Qt.LeftEdge:左边缘(默认)。
  • Qt.RightEdge:右边缘。
  • Qt.BottomEdge:底部边缘。

3、interactive : bool

Drawer 是否是交互式的。 非交互式不会对滑动做出反应。默认为 true。

4、position : real

Drawer 相对于其最终目的地的位置。完全关闭时位置为 0.0,完全打开时位置为 1.0。

### Qt Design Studio 中 Drawer 组件的使用 Drawer 是一种侧边栏控件,在移动应用和桌面应用程序中非常常见。这种组件允许用户通过滑动或点击按钮来打开关闭菜单[^1]。 #### 创建 Drawer 的基本结构 要创建一个简单的 Drawer,可以按照如下 QML 代码定义: ```qml import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 640 height: 480 Drawer { id: myDrawer width: parent.width * 0.75 height: parent.height Column { anchors.fill: parent Label { text: "Item 1" } Label { text: "Item 2" } Label { text: "Item 3" } } closePolicy: Drawer.CloseOnEscape | Drawer.CloseOnClickOutside } Button { text: qsTr("Open Drawer") onClicked: myDrawer.open() } } ``` 这段代码展示了如何设置 Drawer 并将其与按钮关联起来以便于操作[^1]。 #### 自定义外观样式 对于希望自定义 Drawer 外观的应用程序开发者来说,可以通过修改 `background` 属性来自定义背景颜色或其他视觉效果;也可以调整内部布局以适应特定需求[^2]。 例如,如果想要改变默认白色背景为灰色,则可以在 Drawer 定义内加入以下属性: ```qml background: Rectangle { color: "#f0f0f0"; radius: 8 } ``` #### 响应事件处理 为了增强用户体验,通常还需要监听某些交互动作并作出相应反应。比如当抽屉完全展开时触发某个函数执行特殊逻辑等。这可通过连接信号槽机制实现[^3]。 ```qml onOpenedChanged: { if (opened) console.log('The drawer has been fully opened.'); else console.log('The drawer is now closed.'); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值