当前位置: 首页 > news >正文

QML ScrollBar如何一直保持到最底部

一、效果查看

在这里插入图片描述

二、源码分享

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Basic

Window {
    width: 1300
    height: 850
    visible: true
    minimumHeight: 850
    minimumWidth: 1300
    title: qsTr("日志查看")

    ColumnLayout {
        id: columnLayout
        anchors.fill: parent
        spacing: 0
        Row {
            id: row
            Layout.preferredHeight: parent.height/30
            Layout.fillWidth: true
            spacing: 5

            CheckBox {
                id: checkBoxLook
                width: 100
                height: 28
                text: qsTr("查看")
                checked: true
                anchors.verticalCenter: parent.verticalCenter
                background: Rectangle{
                    anchors.fill: parent
                    radius: parent.width/2
                    color:"steelblue"
                }
                indicator:Item{
                    width: parent.height-5
                    height: parent.height-5
                    anchors.verticalCenter: parent.verticalCenter
                    x:3
                    Rectangle{
                        anchors.fill: parent
                        radius: width/2
                        border.width: 3
                        border.color: "white"
                        color:parent.parent.checked?"green":"white"
                    }
                }
                contentItem: Text{
                    leftPadding: parent.indicator.width*2
                    anchors.fill: parent
                    text:parent.text
                    color:parent.checked?"white":"black"
                    font.pixelSize: parent.font.pixelSize
                    verticalAlignment: Text.AlignVCenter
                   // horizontalAlignment: Text.AlignHCenter
                }
            }

            CheckBox {
                id: checkBoxSave
                width: 100
                height: 28
                text: qsTr("保存")
                checked: true
                anchors.verticalCenter: parent.verticalCenter
                background: Rectangle{
                    anchors.fill: parent
                    radius: parent.width/2
                    color:"steelblue"
                }
                indicator:Item{
                    width: parent.height-5
                    height: parent.height-5
                    anchors.verticalCenter: parent.verticalCenter
                    x:3
                    Rectangle{
                        anchors.fill: parent
                        radius: width/2
                        border.width: 3
                        border.color: "white"
                        color:parent.parent.checked?"green":"white"
                    }
                }
                contentItem: Text{
                    leftPadding: parent.indicator.width*2
                    anchors.fill: parent
                    text:parent.text
                    color:parent.checked?"white":"black"
                    font.pixelSize: parent.font.pixelSize
                    verticalAlignment: Text.AlignVCenter
                   // horizontalAlignment: Text.AlignHCenter
                }
            }

            Button {
                id: btnClear
                width: 100
                height: 28
                text: qsTr("清空")
                anchors.verticalCenter: parent.verticalCenter
                background: Rectangle{
                    anchors.fill: parent
                    radius: 10
                    border.width: 1
                    color:btnClear.pressed?"lightblue":btnClear.hovered?"dodgerblue":"steelblue"
                }
                onClicked: {
                    textArea.clear()
                }
            }
        }
        ScrollView {
            id:scrollView
            Layout.fillHeight: true
            Layout.fillWidth: true
            clip: true
            ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
            ScrollBar.vertical: ScrollBar{                      // 自定义垂直方向ScrollView滚动条
                id:cusScrollBar
                anchors.top: scrollView.top
                anchors.bottom: scrollView.bottom
                anchors.right: scrollView.right
                policy: ScrollBar.AlwaysOn
            }
            TextArea {
                id: textArea
                color: "#0dfe3c"
                wrapMode: Text.WordWrap
                readOnly: true
                selectedTextColor: "#f9faf9"
                background: Rectangle{
                    //anchors.fill: parent
                    color:"black"
                }
            }
        }

    }
    function appendText(txt)
    {
        if(checkBoxLook.checked)
        {
            textArea.append(txt)
            var position = (scrollView.contentHeight - columnLayout.height+40) / scrollView.contentHeight
            scrollView.ScrollBar.vertical.position = position >= 0 ? position : 0
        }
    }

}

三、解析

首先定义ScrollBar

ScrollBar.vertical: ScrollBar{                      // 自定义垂直方向ScrollView滚动条
   id:cusScrollBar
   anchors.top: scrollView.top
   anchors.bottom: scrollView.bottom
   anchors.right: scrollView.right
   policy: ScrollBar.AlwaysOn
}

然后再添加内容的过程中改变ScrollBar的位置

    function appendText(txt)
    {
        if(checkBoxLook.checked)
        {
            textArea.append(txt)
            var position = (scrollView.contentHeight - columnLayout.height+40) / scrollView.contentHeight
            scrollView.ScrollBar.vertical.position = position >= 0 ? position : 0
        }
    }

相关文章:

  • 【C语言】fgetpos函数用法介绍
  • C# 中DevExpress的GridView中Appearance 属性
  • 搅局外卖,京东连出三张牌
  • Selenium实战案例2:东方财富网股吧评论爬取
  • 解决 Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
  • Prompt:创造性的系统分析者
  • 前端面试之Box盒子布局:核心知识与实战解析
  • 单调栈刷题
  • 1.21作业
  • 除了Axios,如何用fetch处理403错误?
  • 懒人美食帮(springboot论文源码调试讲解)
  • 【记录55】多个参数查询,无用参数不记查询
  • go 通过ssh连接linux golang.org/x/crypto/ssh
  • 网络安全设备防护原理 网络安全防护装置
  • WARNING: pip is configured with locations that require TLS/SSL
  • C语言的指针常量、常量指针、指针函数、函数指针的区别
  • 去中心化协作智能生态系统设计
  • 【Flink实战】Flink网络内存和托管内存
  • 可视化工具SciChart如何结合Deepseek快速创建一个React仪表板?
  • CT dicom 去除床板 去除床位,检查床去除
  • 《风林火山》千呼万唤始出来,戛纳首映后口碑崩盘?
  • 法律顾问被控配合他人诈骗酒店资产一审判8年,二审辩称无罪
  • 李伟任山东省委常委、省纪委书记
  • 董军同德国国防部长举行会谈
  • 申论|空间更新结合“青银共生”,助力青年发展型城区建设
  • 新任美国驻华大使庞德伟抵京履职,外交部回应