QML实现新闻热点向上滚动的效果

31 篇文章 11 订阅

实现思路:
一个可视的Rectangle设置其高度等于ListView的item的高度,这样保证显示一个。
通过移动ListView的x值得位置实现向上滚动的效果。利用动画修改x的值,达到动态效果。

效果如下:

截图效果不好哈

具体代码如下
用了两种方式,推荐使用第一种方式,看个人。重点是实现思路。

import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("图片切换")

	//实现1 利用ListView方便添加数据和修改
    Rectangle{
        anchors.centerIn: parent
        width: 200;
        height: 40;
        border.color: "red"
        clip: true

        ListModel {
            id : listmode
             ListElement {
                 name: "补课费成家庭最大支出"
             }
             ListElement {
                 name: "n教育问题在教育外求解"

             }
             ListElement {
                 name: "朝鲜举行国庆70周年盛大阅兵"

             }
             ListElement {
                 name: "补课费成家庭最大支出2"
             }
         }

        ListView {
            id: listtext
             width: 180;
             height: listmode.count*40 //必须给定
             y : 0
             model: listmode
             delegate: Text {
                 lineHeightMode : Text.FixedHeight
                 lineHeight : 40
                 horizontalAlignment : Text.AlignHCenter
                 verticalAlignment : Text.AlignVCenter
                 text: name

                 MouseArea{
                     anchors.fill: parent
                     hoverEnabled : true
                     onEntered: {
                         timer.stop();
                         console.log("enter");
                     }
                     onExited:{
                         console.log("exit");
                         timer.start();
                     }
                     onClicked: {
                         console.log(name);
                     }
                 }
             }
         }
    }
    PropertyAnimation { id: animation;
                             target: listtext;
                             property: "y";
                             to: listtext.y-40;
                             duration: 200
    }
    Timer{
        id : timer
        interval: 2000; running: true; repeat: true
        onTriggered: {
            if (listtext.y == (listtext.count-1)*-40)
            {
                listtext.y = 0;
            }
            animation.start();
        }
    }


    /*
	//实现2 Text实现,不方便修改增加值
    //热点新闻轮播
    Rectangle{
        anchors.fill: parent
        Rectangle{
            anchors.centerIn: parent
            width: 200;
            height: 40;
            border.color: "red"
            clip: true
            Text {
                id: listtext
                lineHeightMode : Text.FixedHeight
                lineHeight : 40
                horizontalAlignment : Text.AlignHCenter
                verticalAlignment : Text.AlignVCenter

                 text: qsTr("补课费成家庭最大支出\n教育问题在教育外求解\n朝鲜举行国庆70周年盛大阅兵\n女兵踢整齐鹅式正步前进")
                 MouseArea{
                     anchors.fill: parent
                     hoverEnabled : true
                     onEntered: {
                         timer.stop();
                         console.log("enter");
                     }
                     onExited:{
                         console.log("exit");
                         timer.start();
                     }
                 }
            }


        }
    }
    Component.onCompleted: {
        console.log(listtext.lineCount);
    }

    PropertyAnimation { id: animation;
                             target: listtext;
                             property: "y";
                             to: listtext.y-40;
                             duration: 200
    }
    Timer{
        id : timer
        interval: 2000; running: true; repeat: true
        onTriggered: {
            if (listtext.y == (listtext.lineCount-1)*-40)
            {
                listtext.y = 0;
            }
            animation.start();
        }
    }

    */
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
QML 中,我们可以使用 Rectangle 或 Item 来实现滚动效果。下面是一个简单的例子: ```qml import QtQuick 2.12 import QtQuick.Controls 2.12 Rectangle { id: container width: 200 height: 200 clip: true // 遮罩 Rectangle { id: content width: container.width height: 400 // 内容高度大于容器高度,以便出现滚动条 color: "blue" Text { text: "Scrollable content" anchors.centerIn: parent color: "white" } } ScrollBar.vertical: ScrollBar {} ScrollIndicator.vertical: ScrollIndicator {} ScrollArea { id: scrollArea anchors.fill: parent contentItem: content clip: true // 滚动条跟随内容变化 onContentWidthChanged: { if (content.width > container.width) { scrollBar.visible = true } else { scrollBar.visible = false } } onContentHeightChanged: { if (content.height > container.height) { scrollBar.visible = true } else { scrollBar.visible = false } } } } ``` 上述代码中,我们使用了一个包含了蓝色背景和一行文本的 Rectangle 作为内容。然后我们创建了一个容器 Rectangle 作为滚动区域,并将其 clip 属性设置为 true,以便实现遮罩效果。接着我们创建了一个 ScrollArea,将内容 Rectangle 设置为其 contentItem 属性,以实现滚动效果。最后,我们还添加了一个垂直滚动条 ScrollBar 和一个滚动指示器 ScrollIndicator。 当内容高度或宽度大于容器高度或宽度时,滚动条会自动出现。滚动条的位置和大小会随着内容的滚动而变化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值