qml lisrview嵌套GridView

博主花费两天时间研究QML中ListView嵌套GridView来实现带标题的网格布局,但遇到鼠标穿透等难题。主要难点包括动态加载数据、区分ListView和GridView的索引以及处理鼠标事件。解决方案包括设置GridView的interactive为false以消除滑动效果,以及利用JS拼接数据创建二维数组。文章提到的问题仍有待解决,如按钮状态恢复和离开视图时的交互问题。
摘要由CSDN通过智能技术生成

研究了2天时间,网上连个例子都没找到。这几天都失眠了!也没有大神指点下。。。我不想做这个了

list自带title效果,因此。如果想实现带title的网格布局时,只能仿照安卓的思路,listview嵌套gridview

代码如下:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3


Window {
    visible: true
    width: 1280
    height: 720
    title: qsTr("Hello World")

    property var fenlei_texts:
              [
                  ["KTV热歌","网络歌曲","现场音乐","背景音乐","经典","情歌"],
                  ["夜店","学习工作","咖啡馆","运动","睡前","旅行"],
                  ["伤感","安静","快乐","励志","治愈","思念"],
                  ["00年代","90年代","80年代","70年代"],
                  ["流行","电子","轻音乐","民谣","轻音乐","民谣","说唱","摇滚"],
                  ["英语","韩语","日语","国语"]
            ]
            ListModel{
                id: phoneModel
            }
            Component{
                id: sectionHeader

                Rectangle{
                    width: parent.width
                    height: childrenRect.height
                    color: "lightsteelblue"
                    Text{
                        text: section
                        font.bold: true
                        font.pointSize: 20
                    }
                }
            }//sectionHeader is end

        Component{
            id:lisview_gridview
            Rectangle{

            width: ListView.view.width
            height: 80
            color: ListView.isCurrentItem?"#157efb":"#53d769" //选中颜色设置
            border.color: Qt.lighter(color, 1.1)
            GridView{
                id:grid_view
                model:fenlei_texts[index%6]
                height:parent.height
                width:parent.width
         flickDeceleration:0
interactive:false
delegate: Component{ Rectangle{ id:back color:GridView.isCurrentItem?"yellow":"red" //选中颜色设置 height:80 width:80 Text{ anchors.centerIn: parent text:modelData } MouseArea { anchors.fill: parent acceptedButtons: Qt.AllButtons onClicked:{ grid_view.currentIndex = index //实现item切换 console.log("*******grid_currentIndex**********"+index) } onPressed: {back.color = "yellow"} onReleased: {back.color = "red"} } } } Component.onCompleted: { grid_view.currentIndex = -1; } } MouseArea { anchors.fill: parent propagateComposedEvents: true onPressed:{ listview.currentIndex = index //实现item切换 grid_view.currentIndex = -1 mouse.accepted = false console.log("*******List_currentIndex**********"+index) } } } } ListView { id:listview width: parent.width height: parent.height model:phoneModel clip:true delegate:lisview_gridview section.property: "manufacture" section.criteria: ViewSection.FullString section.delegate: sectionHeader } Component.onCompleted: { phoneModel.append({
    "manufacture":"主题"}) phoneModel.append({
    "manufacture":"场景"}) phoneModel.append({
    "manufacture":"心情"}) phoneModel.append({
    "manufacture":<
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值