如何在 QML 中为矩形创建滚动条 [英] how to create a scrollbar for rectangle in QML

查看:27
本文介绍了如何在 QML 中为矩形创建滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像网页一样,当内容超出矩形时,会有一个滚动条.还有其他人可以帮助我吗?我试过用listview,但不能在矩形中使用它

解决方案

文档中有一个例子,如何使用

导入QtQuick 2.7导入 QtQuick.Controls 2.0长方形 {编号:框架剪辑:真宽度:160身高:160边框颜色:黑色"anchors.centerIn: 父级文本 {id: 内容文字:ABC"字体.像素大小:160x:-hbar.position * 宽度y:-vbar.position * 高度}滚动条 {编号:vbar悬停启用:真活跃:悬停||按下方向:Qt.Vertical尺寸:frame.height/content.heightanchors.top: 父级.top锚点.right: 父级.rightanchors.bottom:父级.bottom}滚动条 {编号:hbar悬停启用:真活跃:悬停||按下方向:Qt.Horizo​​ntal尺寸:frame.width/content.widthanchors.left: 父级.left锚点.right: 父级.rightanchors.bottom:父级.bottom}}

like the web pages,when content's high beyond the rectangle,there is a scrollbar. Is there anyone else who can help me? I have tried with listview,but I can't use it in a rectangle

解决方案

There is an example in the docs, how to use ScrollBar without a Flickable:

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle {
    id: frame
    clip: true
    width: 160
    height: 160
    border.color: "black"
    anchors.centerIn: parent

    Text {
        id: content
        text: "ABC"
        font.pixelSize: 160
        x: -hbar.position * width
        y: -vbar.position * height
    }

    ScrollBar {
        id: vbar
        hoverEnabled: true
        active: hovered || pressed
        orientation: Qt.Vertical
        size: frame.height / content.height
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

    ScrollBar {
        id: hbar
        hoverEnabled: true
        active: hovered || pressed
        orientation: Qt.Horizontal
        size: frame.width / content.width
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }
}

这篇关于如何在 QML 中为矩形创建滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆