如何在QML中为矩形创建滚动条

nic*_*ick 2 qt qml qtquick2 qtquickcontrols2

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

jpn*_*rmi 6

文档中有一个例子,如何在ScrollBar没有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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我觉得你可以在这样一个基本项目上获得这个功能真是太酷了.:) (2认同)

Mah*_*ili 5

将矩形添加到 flickable 解决了我的问题

import QtQuick.Controls 2.5
import QtQuick.Controls.Material 2.5
import QtQuick 2.8
Item {
    id: item1
    visible: true
    width: 800
    height: 600
    ScrollView {
        id: frame
        clip: true
        anchors.fill: parent
        //other properties
        ScrollBar.vertical.policy: ScrollBar.AlwaysOn
        Flickable {
            contentHeight: 2000
            width: parent.width
            Rectangle {
                id : rectangle
                color: "#a7c4c6"
                radius: 6
                //visible: !busyIndicator.running
                anchors.fill: parent
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)