localstorage.qml Example File
localstorage/localstorage/localstorage.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.LocalStorage 2.0
import "Database.js" as JS
Window {
visible: true
width: Screen.width / 2
height: Screen.height / 1.8
color: "#161616"
property bool creatingNewEntry: false
property bool editingEntry: false
Rectangle {
anchors.fill: parent
ColumnLayout {
anchors.fill: parent
Header {
id: input
Layout.fillWidth: true
}
RowLayout {
MyButton {
text: "New"
onClicked: {
input.initrec_new()
creatingNewEntry = true
listView.model.setProperty(listView.currentIndex, "id", 0)
}
}
MyButton {
id: saveButton
enabled: (creatingNewEntry || editingEntry) && listView.currentIndex != -1
text: "Save"
onClicked: {
var insertedRow = false;
if (listView.model.get(listView.currentIndex).id < 1) {
if (input.insertrec()) {
input.setlistview()
insertedRow = true
} else {
statustext.text = "Failed to insert row"
}
} else {
input.setlistview()
JS.dbUpdate(listView.model.get(listView.currentIndex).date,
listView.model.get(listView.currentIndex).trip_desc,
listView.model.get(listView.currentIndex).distance,
listView.model.get(listView.currentIndex).id)
}
if (insertedRow) {
input.initrec()
creatingNewEntry = false
editingEntry = false
listView.forceLayout()
}
}
}
MyButton {
id: editButton
text: "Edit"
enabled: !creatingNewEntry && !editingEntry && listView.currentIndex != -1
onClicked: {
input.editrec(listView.model.get(listView.currentIndex).date,
listView.model.get(listView.currentIndex).trip_desc,
listView.model.get(listView.currentIndex).distance,
listView.model.get(listView.currentIndex).id)
editingEntry = true
}
}
MyButton {
id: deleteButton
text: "Delete"
enabled: !creatingNewEntry && listView.currentIndex != -1
onClicked: {
JS.dbDeleteRow(listView.model.get(listView.currentIndex).id)
listView.model.remove(listView.currentIndex, 1)
if (listView.count == 0) {
listView.currentIndex = -1
}
}
}
MyButton {
id: cancelButton
text: "Cancel"
enabled: (creatingNewEntry || editingEntry) && listView.currentIndex != -1
onClicked: {
if (listView.model.get(listView.currentIndex).id === 0) {
listView.model.remove(listView.currentIndex, 1)
}
listView.forceLayout()
creatingNewEntry = false
editingEntry = false
input.initrec()
}
}
MyButton {
text: "Exit"
onClicked: Qt.quit()
}
}
Component {
id: highlightBar
Rectangle {
width: listView.currentItem.width
height: listView.currentItem.height
color: "lightgreen"
}
}
ListView {
id: listView
Layout.fillWidth: true
Layout.fillHeight: true
model: MyModel {}
delegate: MyDelegate {}
enabled: !creatingNewEntry && !editingEntry
highlight: highlightBar
highlightFollowsCurrentItem: true
focus: true
header: Component {
Text {
text: "Saved activities"
}
}
}
Text {
id: statustext
color: "red"
Layout.fillWidth: true
font.bold: true
font.pointSize: 20
}
}
}
Component.onCompleted: {
JS.dbInit()
}
}