[SailfishDevel] Sharing model between pages ("by reference")

Andrey Kozhevnikov coderusinbox at gmail.com
Sun Feb 1 10:19:02 UTC 2015


Hello,

Why didn't you tried before asking? This is working in exactly same way 
as you described in your question.

Below is template pages. MainPage declares testModel, FirstPage and 
SecondPage using same model as reference, assigned to listModel 
property. Everything works flawlessly.

MainPage.qml

import QtQuick 2.0
import Sailfish.Silica 1.0

Page {
     id: page

     ListModel {
         id: testModel
         ListElement {
             name: "One"
         }
         ListElement {
             name: "Two"
         }
         ListElement {
             name: "Three"
         }
     }

     SilicaFlickable {
         anchors.fill: parent

         PullDownMenu {
             MenuItem {
                 text: qsTr("Show Page 1")
                 onClicked: 
pageStack.push(Qt.resolvedUrl("FirstPage.qml"), {"listModel": testModel})
             }
             MenuItem {
                 text: qsTr("Show Page 2")
                 onClicked: 
pageStack.push(Qt.resolvedUrl("SecondPage.qml"), {"listModel": testModel})
             }
         }

         contentHeight: column.height

         Column {
             id: column

             width: page.width
             spacing: Theme.paddingLarge
             PageHeader {
                 title: qsTr("UI Template")
             }
             Label {
                 x: Theme.paddingLarge
                 text: qsTr("Hello Sailors")
                 color: Theme.secondaryHighlightColor
                 font.pixelSize: Theme.fontSizeExtraLarge
             }
         }
     }
}

FirstPage.qml

import QtQuick 2.0
import Sailfish.Silica 1.0

Page {
     id: page
     property ListModel listModel
     SilicaListView {
         id: listView
         model: listModel
         anchors.fill: parent
         header: PageHeader {
             title: qsTr("First Page")
         }
         delegate: BackgroundItem {
             id: delegate

             Label {
                 x: Theme.paddingLarge
                 text: model.name
                 anchors.verticalCenter: parent.verticalCenter
                 color: delegate.highlighted ? Theme.highlightColor : 
Theme.primaryColor
             }
             onClicked: listModel.append({"name": "New" + listView.count})
         }
         VerticalScrollDecorator {}
     }
}

SecondPage.qml

import QtQuick 2.0
import Sailfish.Silica 1.0

Page {
     id: page
     property ListModel listModel
     SilicaListView {
         id: listView
         model: listModel
         anchors.fill: parent
         header: PageHeader {
             title: qsTr("Second Page")
         }
         delegate: BackgroundItem {
             id: delegate

             Label {
                 x: Theme.paddingLarge
                 text: model.name
                 anchors.verticalCenter: parent.verticalCenter
                 color: delegate.highlighted ? Theme.highlightColor : 
Theme.primaryColor
             }
             onClicked: listModel.remove(index)
         }
         VerticalScrollDecorator {}
     }
}

01.02.2015 14:57, Dirk Zimmermann пишет:
> Hi,
>
> I have a page with an attached page. Currently, they get pushed on the
> stack by a parent page like this:
>
> pageStack.push(Qt.resolvedUrl("Page1.qml"), lightModel)
> pageStack.pushAttached(Qt.resolvedUrl("Page2.qml"), lightModel)
>
> Page1.qml will then refresh its copy with live data. I would rather
> Page2.qml not have to refresh its copy the same way. Instead, they would
> ideally share the same object, and changes to it would be instantly
> visible when switching between the two pages.
>
> Is that possible?
>
> --
> Dirk
> _______________________________________________
> SailfishOS.org Devel mailing list
> To unsubscribe, please send a mail to devel-unsubscribe at lists.sailfishos.org



More information about the Devel mailing list