如何在两个视图中使用一个模型

时间:2011-05-23 17:31:49

标签: qml

我有一个图片模型,并希望同步在网格视图或列表视图(全屏)中显示它们。如果用户点击网格视图中的一个图像,我希望此图像以全屏模式显示(在列表视图中)。

我有一个解决方案,但我的列表视图滚动到“当前索引”。

THANX。

PhotoView.qml

Rectangle {
    width: settings.pageWidth
    height: settings.pageHeight
    anchors.fill: parent

    VisualDataModel {
        id: visualModel
        delegate: PhotoDelegate {}
        model: photosModel
    }


    ListView {
        id:fullscreen
        anchors.fill: parent;
        orientation: ListView.Horizontal
        snapMode: ListView.SnapOneItem;
        flickDeceleration: 500
        //highlightFollowsCurrentItem: true
        highlightRangeMode: ListView.StrictlyEnforceRange
        preferredHighlightBegin: 0; preferredHighlightEnd: 0
        cacheBuffer: width;
        spacing:  settings.largeMargin
        model: visualModel.parts.grid

    }

    GridView{
        id:grid
         width: settings.pageWidth
         height: settings.pageHeight
         anchors.fill: parent
         cellWidth: settings.gridCellWidth
         cellHeight: settings.gridCellHeight
         snapMode: GridView.SnapToRow
         cacheBuffer: settings.pageHeight
         clip: true
         model: visualModel.parts.grid
    }

//    // Menu/Back Button
//    IconButton{
//        id: backButton
//        iconSource: "qrc:///gfx/back_arrow.png"
//        anchors.right: parent.right
//        anchors.bottom: parent.bottom
//        onClicked: mainWindow.close();
//    }

    //  Fade Top
    Image{
        id:bottom_fade
        source:  "qrc:///gfx/bottom-page-fade.png"
        height: 33
        width: settings.pageWidth
        anchors.bottom: parent.bottom
        anchors.left:   parent.left
        anchors.right:  parent.right

    }
    //  Fade Bottom
    Image{
        id:top_fade
        source:  "qrc:///gfx/top-page-fade.png"
        height: 33
        width: settings.pageWidth
        anchors.top: parent.top
        anchors.left:   parent.left
        anchors.right:  parent.right

    }

}

PhotoDelegate.qml

import QtQuick 1.0
import Qt 4.7
import "../views"
import "../model"
import "../views/components"

Package {
    Item { id: listDelegate; Package.name: 'list'; width: settings.pageWidth; height: settings.pageHeight }
    Item { id: gridDelegate;  Package.name: 'grid'; width: settings.pageWidth; height: settings.pageHeight }

    Item {
        id: wrapper
        width: settings.pageWidth; height: settings.pageHeight

        Image {
            id: thumbnail; smooth: true; source: thumbnail_url

        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (wrapper.state == 'inGrid') {
                    listDelegate.fullscreen.view.currentIndex = index;
                    wrapper.state = 'fullscreen'
                } else {
                    gridDelegate.grid.view.currentIndex = index;
                    wrapper.state = 'inGrid'
                }
            }

        }

        state: 'inGrid'
        states: [
            State {
                name: 'fullscreen'
                ParentChange { target: wrapper; parent: listDelegate; x: 0; y: 0;
                width: listDelegate.width; height: listDelegate.height
                }
            },
            State {
                name: 'inGrid'
                ParentChange {
                    target: wrapper; parent: gridDelegate
                    x: 0; y: 0; width: gridDelegate.width; height: gridDelegate.height
                }
            }
        ]

        transitions: [
            Transition {
                from: 'inGrid'; to: 'fullscreen'
                SequentialAnimation {
                    PauseAnimation { duration: gridItem.GridView.isCurrentItem ? 0 : 600 }
                    ParentAnimation {
                        target: wrapper; via: foreground
                        NumberAnimation {
                            targets: [ wrapper]
                            properties: 'x,y,width,height,opacity'
                            duration: gridItem.GridView.isCurrentItem ? 600 : 1; easing.type: 'OutQuart'
                        }
                    }
                }
            },
            Transition {
                from: 'fullscreen'; to: 'inGrid'
                ParentAnimation {
                    target: wrapper; via: foreground
                    NumberAnimation {
                        targets: [ wrapper ]
                        properties: 'x,y,width,height,opacity'
                        duration: gridItem.GridView.isCurrentItem ? 600 : 1; easing.type: 'OutQuart'
                    }
                }
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:1)

要显示所选项目而不滚动,直到所选项目的索引要同步视图元素当前索引:

onCurrentIndexChanged:{             your_component.positionViewAtIndex(currentIndex,YourViewElement.Contain)         }