I have a QML application that is basically just a ListView which displays a number of "chapters", which in turn each include one or more "pages".
Since I don't know how many chapters and pages a QML might have in production, I'm using a Loader
to load the pages on demand, which should save some memory.
So the problem is that I want to "jump" to a certain story and page at the push of a button. I added a few buttons to the example which jump to different chapters already.
You can vertically flick between chapters and horizontally between pages of each chapter.
The problem I have is that I can't figure out how to get the ListView
containing the pages to jump to a specific page after switching/loading a specific chapter. Basically I'm missing something like pagesView.currentPage = 5
or something similar.
What would be a good way to get this working?
The corresponding QML. You can run this with qmlscene.
import QtQuick 2.4
import QtQuick.Controls 1.2
ApplicationWindow {
width: 1024
height: 768
Component {
id: pageViewComponent
ListView {
id: pagesView
property int storyIndex: chapterView.modelData
orientation: ListView.Horizontal; clip: true
model: 20; snapMode: ListView.SnapToItem
delegate: Rectangle {
width: pagesView.width; height: pagesView.height
color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
border.color: "black"
Text { text: "Page " + modelData; anchors.centerIn: parent; color: "white" }
}
}
}
Rectangle {
color: "black"
anchors.fill: parent
// Chapters
ListView {
id: chapterView
model: 8
anchors.fill: parent
snapMode: ListView.SnapToItem
delegate: Rectangle {
color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
width: chapterView.width; height: chapterView.height
Rectangle {
width: parent.width * 0.6; height: parent.height * 0.6
anchors.centerIn: parent
Loader {
anchors.fill: parent
sourceComponent: pageViewComponent
}
}
Text {
x: 50; y: 50
color: "white"; font.pointSize: 30
text: "Chapter " + modelData
}
Flow {
Button {
text: "Go to Chapter 2, Page 7"
onClicked: {
chapterView.positionViewAtIndex(2, ListView.Beginning)
//
//
// After jumping to the correct chapter, we obviously have to jump
// to the correct page after the Loader for that specific chapter has
// completed loading the pages of the chapter.
//
//
}
}
Button {
text: "Go to Chapter 1, Page 1"
onClicked: {
chapterView.positionViewAtIndex(1, ListView.Beginning)
// dito
}
}
Button {
text: "Go to Chapter 5, Page 2"
onClicked: {
chapterView.positionViewAtIndex(5, ListView.Beginning)
// dito
}
}
}
}
}
}
}