Qt scrollview QML中的多个GridView

Qt scrollview QML中的多个GridView,qt,qml,Qt,Qml,我有一个scrollview,我想在其中放置两个具有相同委托但具有不同模型的GridView。但是,我希望它们“共享”突出显示功能,尽管我希望模型以不同的方式突出显示。这可能吗?现在,GridView在scrollview中重叠 ScrollView { anchors.fill: parent GridView{ id: grid anchors.fill: parent cellHeight: 30 ce

我有一个scrollview,我想在其中放置两个具有相同委托但具有不同模型的GridView。但是,我希望它们“共享”突出显示功能,尽管我希望模型以不同的方式突出显示。这可能吗?现在,GridView在scrollview中重叠

    ScrollView {
    anchors.fill: parent
    GridView{
        id: grid
        anchors.fill: parent
        cellHeight: 30
        cellWidth: parent.width
        model: model1
        delegate: delegate
        highlight: Rectangle {
            color: "red";
            radius: 5
            focus:true
        }
    }


    GridView {
        focus: true
        model: model2
        delegate: delegate
    }

当您位于第一个模型的最后一个项目和最后一个模型的第一个项目上时,可以使用KeyNavigation attached属性替代向上/向下按钮

使用ListView的示例:

Column {
    anchors.fill: parent
    ListView{
        id: list1
        focus: true
        onFocusChanged: focus ? currentIndex = model1.count-1 : currentIndex = -1
        height: contentHeight
        width: parent.width
        model: model1
        delegate: Text { text: name }

        highlight: Rectangle { color: "red"; radius: 5 }
        KeyNavigation.down: list2
        onCurrentIndexChanged: {
            if (currentIndex == model1.count-1)
                KeyNavigation.priority=KeyNavigation.BeforeItem
            else
                KeyNavigation.priority=KeyNavigation.AfterItem
        }
    }
    ListView {
        id:list2
        focus: false
        onFocusChanged: focus ? currentIndex = 0 : currentIndex = -1
        currentIndex: -1
        model: model2
        height: contentHeight
        width: parent.width
        delegate: Text { text: name }
        highlight: Rectangle { color: "blue"; radius: 5 }
        KeyNavigation.up: list1
        onKeyNavigationWrapsChanged: console.log("k")
        onCurrentIndexChanged: {
            if (currentIndex == 0)
                KeyNavigation.priority=KeyNavigation.BeforeItem
            else
                KeyNavigation.priority=KeyNavigation.AfterItem
        }
    }
}

在本例中,这似乎是一个单一的模型,但它们将使用不同的高亮灯。

您说“我希望它们“共享”高亮显示功能,尽管我希望模型以不同的方式高亮显示”。这到底意味着什么?我希望gridview中的一些行以一种颜色高亮显示,另一些以另一种颜色高亮显示。例如,您希望高亮显示具有红色背景的奇数索引的项目,否则背景必须为绿色。有道理吗?有点。突出显示的颜色取决于条目的文本颜色。文本颜色或多或少是“随机”的。另外,我希望在突出显示时将文本颜色更改为给定的颜色。我仍然不确定您想要实现什么,无论如何,看起来您可以使用
color:model.get(grid.currentIndex)。您的\u字段\u here==您的\u条件\u here?“红色”:“黄色”在高亮显示组件中。您可以对委托执行相同的操作,这样您就可以更改文本颜色,即使您可以从委托中访问整个项目,因此您不需要对网格元素进行这样的引用。