如何使滚动条在QML中的Flckable上始终可见

如何使滚动条在QML中的Flckable上始终可见

问题描述:

我想使Flickable区域中的Scrollbar.vertical始终可见。目前它在点击时可见。此外,文本区域与滚动条重叠。我怎样才能sepearate与文本区滚动条下面的QML代码enter image description here如何使滚动条在QML中的Flckable上始终可见

import QtQuick 2.0 
import QtQuick.Controls 2.0 

Flickable { 
    id: flickable 
    anchors.fill: parent 


    TextArea.flickable: TextArea { 
     text: "The Qt QML module provides a framework for developing applications and libraries with the QML language. 
It defines and implements the language and engine infrastructure, and provides an API to enable application developers to 
extend the QML language with custom types and integrate QML code with JavaScript and C++. 

The Qt QML module provides both a QML API and a C++ API. 
Note that while the Qt QML module provides the language and infrastructure for QML applications, 
the Qt Quick module provides many visual components, model-view support, an animation framework, 
and much more for building user interfaces. 
For those new to QML and Qt Quick, please see QML Applications for an introduction to writing QML applications." 
     wrapMode: TextArea.Wrap 
     font.pixelSize: 20 
    } 
    ScrollBar.vertical: ScrollBar { 
     width: 40 
    } 
} 
+0

您可以轻松创建一个“独立”项并通过绑定到flickable的属性来控制它,而不是使用默认的内置行为。 – dtech

+0

@ddriver:这是一个简单的解决方案。但我试图有效地使用默认的一个 – Bupa

+1

你可以尝试设置'active:true'。根据文档,当滚动条可见时这是真的。它用于例如[使滚动条的可见性依赖于彼此](http://doc.qt.io/qt-5/qml-qtquick-controls2-scrollbar.html#binding-the-active-state-of-horizo​​ntal-and-垂直滚动条)。 – BaCaRoZzo

看来active -property通过建议BaCaRoZzo由计时器覆盖每隔几秒钟,如果正好被设置为true。 因此,您可能每次都使用onActiveChanged-信号将其重置为true

但是,您也可以通过将contentItemopacity设置为1来禁用它。至少对我而言,这是个窍门。

为了适应您的定位:只需将其固定。

Flickable { 
    id: myflickable 
    ... 
    ScrollBar.vertical: myvertscroll 
    clip: true 
} 

ScrollBar { 
    id: myvertscroll 
    anchors.left: myflickable.right 
    contentItem.opacity: 1 
} 

如果升级到Qt的5.9,QtQuick.Controls 2.2引入了policy属性,可以帮助例如

import QtQuick 2.0 
import QtQuick.Controls 2.2 

Flickable { 
    id: flickable 
    ... 
    ScrollBar.vertical: ScrollBar { 
     width: 40 
     anchors.left: parent.right // adjust the anchor as suggested by derM 
     policy: ScrollBar.AlwaysOn 
    } 
}