aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols2/chattutorial/chapter5
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quickcontrols2/chattutorial/chapter5')
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/+Material/ChatToolBar.qml56
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/CMakeLists.txt65
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/ChatToolBar.qml54
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/ContactPage.qml90
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/ConversationPage.qml164
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/chapter5.pro36
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein.pngbin0 -> 1669 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@2x.pngbin0 -> 5657 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@3x.pngbin0 -> 11804 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@4x.pngbin0 -> 19989 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway.pngbin0 -> 2255 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@2x.pngbin0 -> 6375 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@3x.pngbin0 -> 13723 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@4x.pngbin0 -> 24109 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude.pngbin0 -> 2937 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@2x.pngbin0 -> 9239 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@3x.pngbin0 -> 18163 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@4x.pngbin0 -> 28614 bytes
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/images/images.qrc16
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/main.cpp99
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/main.qml66
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/qtquickcontrols2.conf10
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.cpp90
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.h62
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.cpp154
-rw-r--r--examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.h79
26 files changed, 1041 insertions, 0 deletions
diff --git a/examples/quickcontrols2/chattutorial/chapter5/+Material/ChatToolBar.qml b/examples/quickcontrols2/chattutorial/chapter5/+Material/ChatToolBar.qml
new file mode 100644
index 0000000000..d4a66764bd
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/+Material/ChatToolBar.qml
@@ -0,0 +1,56 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick.Controls
+import QtQuick.Controls.Material
+
+ToolBar {
+ Material.theme: Material.Dark
+}
diff --git a/examples/quickcontrols2/chattutorial/chapter5/CMakeLists.txt b/examples/quickcontrols2/chattutorial/chapter5/CMakeLists.txt
new file mode 100644
index 0000000000..1d39757f1d
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/CMakeLists.txt
@@ -0,0 +1,65 @@
+cmake_minimum_required(VERSION 3.16)
+project(chapter5 LANGUAGES CXX)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(CMAKE_AUTOMOC ON)
+
+if(NOT DEFINED INSTALL_EXAMPLESDIR)
+ set(INSTALL_EXAMPLESDIR "examples")
+endif()
+
+set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quickcontrols2/chattutorial/chapter5")
+
+find_package(Qt6 COMPONENTS Core Gui Qml Quick Sql)
+
+qt_add_executable(chattutorial-chapter5 WIN32 MACOSX_BUNDLE
+ main.cpp
+ sqlcontactmodel.cpp sqlcontactmodel.h
+ sqlconversationmodel.cpp sqlconversationmodel.h
+)
+target_link_libraries(chattutorial-chapter5 PRIVATE
+ Qt::Core
+ Qt::Gui
+ Qt::Qml
+ Qt::Quick
+ Qt::Sql
+)
+
+# Resources:
+qt_add_qml_module(chattutorial-chapter5
+ URI chapter5
+ VERSION 1.0
+ QML_FILES
+ "+Material/ChatToolBar.qml"
+ "ChatToolBar.qml"
+ "ContactPage.qml"
+ "ConversationPage.qml"
+ "main.qml"
+ RESOURCES
+ "images/Albert_Einstein.png"
+ "images/Albert_Einstein@2x.png"
+ "images/Albert_Einstein@3x.png"
+ "images/Albert_Einstein@4x.png"
+ "images/Ernest_Hemingway.png"
+ "images/Ernest_Hemingway@2x.png"
+ "images/Ernest_Hemingway@3x.png"
+ "images/Ernest_Hemingway@4x.png"
+ "images/Hans_Gude.png"
+ "images/Hans_Gude@2x.png"
+ "images/Hans_Gude@3x.png"
+ "images/Hans_Gude@4x.png"
+)
+
+qt6_add_resources(chattutorial-chapter5 "conf"
+ PREFIX
+ "/"
+ FILES
+ "qtquickcontrols2.conf"
+)
+
+install(TARGETS chattutorial-chapter5
+ RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
+ LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+)
diff --git a/examples/quickcontrols2/chattutorial/chapter5/ChatToolBar.qml b/examples/quickcontrols2/chattutorial/chapter5/ChatToolBar.qml
new file mode 100644
index 0000000000..c192aeb51a
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/ChatToolBar.qml
@@ -0,0 +1,54 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick.Controls
+
+ToolBar {
+}
diff --git a/examples/quickcontrols2/chattutorial/chapter5/ContactPage.qml b/examples/quickcontrols2/chattutorial/chapter5/ContactPage.qml
new file mode 100644
index 0000000000..34a138edca
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/ContactPage.qml
@@ -0,0 +1,90 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick
+import QtQuick.Controls
+
+import io.qt.examples.chattutorial
+
+Page {
+ id: root
+
+ header: ChatToolBar {
+ Label {
+ text: qsTr("Contacts")
+ font.pixelSize: 20
+ anchors.centerIn: parent
+ }
+ }
+
+ ListView {
+ id: listView
+ anchors.fill: parent
+ topMargin: 48
+ leftMargin: 48
+ bottomMargin: 48
+ rightMargin: 48
+ spacing: 20
+ model: SqlContactModel {}
+ delegate: ItemDelegate {
+ text: model.display
+ width: listView.width - listView.leftMargin - listView.rightMargin
+ height: avatar.implicitHeight
+ leftPadding: avatar.implicitWidth + 32
+ onClicked: root.StackView.view.push("ConversationPage.qml", { inConversationWith: model.display })
+
+ Image {
+ id: avatar
+ source: "images/" + model.display.replace(" ", "_") + ".png"
+ }
+ }
+ }
+}
+
diff --git a/examples/quickcontrols2/chattutorial/chapter5/ConversationPage.qml b/examples/quickcontrols2/chattutorial/chapter5/ConversationPage.qml
new file mode 100644
index 0000000000..60dbc4a2ee
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/ConversationPage.qml
@@ -0,0 +1,164 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick
+import QtQuick.Layouts
+import QtQuick.Controls
+
+import io.qt.examples.chattutorial
+
+Page {
+ id: root
+
+ property string inConversationWith
+
+ header: ChatToolBar {
+ ToolButton {
+ text: qsTr("Back")
+ anchors.left: parent.left
+ anchors.leftMargin: 10
+ anchors.verticalCenter: parent.verticalCenter
+ onClicked: root.StackView.view.pop()
+ }
+
+ Label {
+ id: pageTitle
+ text: inConversationWith
+ font.pixelSize: 20
+ anchors.centerIn: parent
+ }
+ }
+
+ ColumnLayout {
+ anchors.fill: parent
+
+ ListView {
+ id: listView
+ Layout.fillWidth: true
+ Layout.fillHeight: true
+ Layout.margins: pane.leftPadding + messageField.leftPadding
+ displayMarginBeginning: 40
+ displayMarginEnd: 40
+ verticalLayoutDirection: ListView.BottomToTop
+ spacing: 12
+ model: SqlConversationModel {
+ recipient: inConversationWith
+ }
+ delegate: Column {
+ anchors.right: sentByMe ? listView.contentItem.right : undefined
+ spacing: 6
+
+ readonly property bool sentByMe: model.recipient !== "Me"
+
+ Row {
+ id: messageRow
+ spacing: 6
+ anchors.right: sentByMe ? parent.right : undefined
+
+ Image {
+ id: avatar
+ source: !sentByMe ? "images/" + model.author.replace(" ", "_") + ".png" : ""
+ }
+
+ Rectangle {
+ width: Math.min(messageText.implicitWidth + 24, listView.width - avatar.width - messageRow.spacing)
+ height: messageText.implicitHeight + 24
+ color: sentByMe ? "lightgrey" : "steelblue"
+
+ Label {
+ id: messageText
+ text: model.message
+ color: sentByMe ? "black" : "white"
+ anchors.fill: parent
+ anchors.margins: 12
+ wrapMode: Label.Wrap
+ }
+ }
+ }
+
+ Label {
+ id: timestampText
+ text: Qt.formatDateTime(model.timestamp, "d MMM hh:mm")
+ color: "lightgrey"
+ anchors.right: sentByMe ? parent.right : undefined
+ }
+ }
+
+ ScrollBar.vertical: ScrollBar {}
+ }
+
+ Pane {
+ id: pane
+ Layout.fillWidth: true
+
+ RowLayout {
+ width: parent.width
+
+ TextArea {
+ id: messageField
+ Layout.fillWidth: true
+ placeholderText: qsTr("Compose message")
+ wrapMode: TextArea.Wrap
+ }
+
+ Button {
+ id: sendButton
+ text: qsTr("Send")
+ enabled: messageField.length > 0
+ onClicked: {
+ listView.model.sendMessage(inConversationWith, messageField.text);
+ messageField.text = "";
+ }
+ }
+ }
+ }
+ }
+}
+
diff --git a/examples/quickcontrols2/chattutorial/chapter5/chapter5.pro b/examples/quickcontrols2/chattutorial/chapter5/chapter5.pro
new file mode 100644
index 0000000000..625642d7b5
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/chapter5.pro
@@ -0,0 +1,36 @@
+TEMPLATE = app
+
+QT += qml quick sql
+CONFIG += c++11
+
+HEADERS += sqlcontactmodel.h \
+ sqlconversationmodel.h
+
+SOURCES += main.cpp \
+ sqlcontactmodel.cpp \
+ sqlconversationmodel.cpp
+
+resources.files = \
+ +Material/ChatToolBar.qml \
+ ChatToolBar.qml \
+ ContactPage.qml \
+ ConversationPage.qml \
+ images/Albert_Einstein.png \
+ images/Albert_Einstein@2x.png \
+ images/Albert_Einstein@3x.png \
+ images/Albert_Einstein@4x.png \
+ images/Ernest_Hemingway.png \
+ images/Ernest_Hemingway@2x.png \
+ images/Ernest_Hemingway@3x.png \
+ images/Ernest_Hemingway@4x.png \
+ images/Hans_Gude.png \
+ images/Hans_Gude@2x.png \
+ images/Hans_Gude@3x.png \
+ images/Hans_Gude@4x.png \
+ main.qml
+resources.prefix = chapter5/
+RESOURCES += resources \
+ qtquickcontrols2.conf
+
+target.path = $$[QT_INSTALL_EXAMPLES]/quickcontrols2/chattutorial/chapter5
+INSTALLS += target
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein.png b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein.png
new file mode 100644
index 0000000000..7c44b90b8d
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@2x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@2x.png
new file mode 100644
index 0000000000..6ce9c39b6c
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@2x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@3x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@3x.png
new file mode 100644
index 0000000000..aab6a6a162
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@3x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@4x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@4x.png
new file mode 100644
index 0000000000..3611284df4
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Albert_Einstein@4x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway.png b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway.png
new file mode 100644
index 0000000000..3ac8992fd9
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@2x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@2x.png
new file mode 100644
index 0000000000..122d9f1e53
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@2x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@3x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@3x.png
new file mode 100644
index 0000000000..2fe9c2cc1c
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@3x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@4x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@4x.png
new file mode 100644
index 0000000000..96fb6788b5
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Ernest_Hemingway@4x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude.png b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude.png
new file mode 100644
index 0000000000..907e38bc61
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@2x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@2x.png
new file mode 100644
index 0000000000..6837796a83
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@2x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@3x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@3x.png
new file mode 100644
index 0000000000..29af422ad3
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@3x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@4x.png b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@4x.png
new file mode 100644
index 0000000000..bc35eeaa79
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/Hans_Gude@4x.png
Binary files differ
diff --git a/examples/quickcontrols2/chattutorial/chapter5/images/images.qrc b/examples/quickcontrols2/chattutorial/chapter5/images/images.qrc
new file mode 100644
index 0000000000..9eda6aa29c
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/images/images.qrc
@@ -0,0 +1,16 @@
+<RCC>
+ <qresource prefix="/">
+ <file>Albert_Einstein.png</file>
+ <file>Albert_Einstein@2x.png</file>
+ <file>Albert_Einstein@3x.png</file>
+ <file>Albert_Einstein@4x.png</file>
+ <file>Ernest_Hemingway.png</file>
+ <file>Ernest_Hemingway@2x.png</file>
+ <file>Ernest_Hemingway@3x.png</file>
+ <file>Ernest_Hemingway@4x.png</file>
+ <file>Hans_Gude.png</file>
+ <file>Hans_Gude@2x.png</file>
+ <file>Hans_Gude@3x.png</file>
+ <file>Hans_Gude@4x.png</file>
+ </qresource>
+</RCC>
diff --git a/examples/quickcontrols2/chattutorial/chapter5/main.cpp b/examples/quickcontrols2/chattutorial/chapter5/main.cpp
new file mode 100644
index 0000000000..74e66106d4
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/main.cpp
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore>
+#include <QGuiApplication>
+#include <QSqlDatabase>
+#include <QSqlError>
+#include <QtQml>
+
+#include "sqlcontactmodel.h"
+#include "sqlconversationmodel.h"
+
+static void connectToDatabase()
+{
+ QSqlDatabase database = QSqlDatabase::database();
+ if (!database.isValid()) {
+ database = QSqlDatabase::addDatabase("QSQLITE");
+ if (!database.isValid())
+ qFatal("Cannot add database: %s", qPrintable(database.lastError().text()));
+ }
+
+ const QDir writeDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
+ if (!writeDir.mkpath("."))
+ qFatal("Failed to create writable directory at %s", qPrintable(writeDir.absolutePath()));
+
+ // Ensure that we have a writable location on all devices.
+ const QString fileName = writeDir.absolutePath() + "/chat-database.sqlite3";
+ // When using the SQLite driver, open() will create the SQLite database if it doesn't exist.
+ database.setDatabaseName(fileName);
+ if (!database.open()) {
+ qFatal("Cannot open database: %s", qPrintable(database.lastError().text()));
+ QFile::remove(fileName);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ QGuiApplication app(argc, argv);
+
+ qmlRegisterType<SqlContactModel>("io.qt.examples.chattutorial", 1, 0, "SqlContactModel");
+ qmlRegisterType<SqlConversationModel>("io.qt.examples.chattutorial", 1, 0, "SqlConversationModel");
+
+ connectToDatabase();
+
+ QQmlApplicationEngine engine;
+ engine.load(QUrl(QStringLiteral("qrc:/chapter5/main.qml")));
+ if (engine.rootObjects().isEmpty())
+ return -1;
+
+ return app.exec();
+}
+
diff --git a/examples/quickcontrols2/chattutorial/chapter5/main.qml b/examples/quickcontrols2/chattutorial/chapter5/main.qml
new file mode 100644
index 0000000000..029f45e3c8
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/main.qml
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick
+import QtQuick.Controls
+
+ApplicationWindow {
+ id: window
+ width: 540
+ height: 960
+ visible: true
+
+ StackView {
+ id: stackView
+ anchors.fill: parent
+ initialItem: ContactPage {}
+ }
+}
+
diff --git a/examples/quickcontrols2/chattutorial/chapter5/qtquickcontrols2.conf b/examples/quickcontrols2/chattutorial/chapter5/qtquickcontrols2.conf
new file mode 100644
index 0000000000..c8a8eeeb05
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/qtquickcontrols2.conf
@@ -0,0 +1,10 @@
+[Controls]
+Style=Basic
+
+[Material]
+Primary=Indigo
+Accent=Indigo
+Theme=Dark
+
+[Universal]
+Theme=Dark
diff --git a/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.cpp b/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.cpp
new file mode 100644
index 0000000000..6203ebec1e
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.cpp
@@ -0,0 +1,90 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "sqlcontactmodel.h"
+
+#include <QDebug>
+#include <QSqlError>
+#include <QSqlQuery>
+
+static void createTable()
+{
+ if (QSqlDatabase::database().tables().contains(QStringLiteral("Contacts"))) {
+ // The table already exists; we don't need to do anything.
+ return;
+ }
+
+ QSqlQuery query;
+ if (!query.exec(
+ "CREATE TABLE IF NOT EXISTS 'Contacts' ("
+ " 'name' TEXT NOT NULL,"
+ " PRIMARY KEY(name)"
+ ")")) {
+ qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
+ }
+
+ query.exec("INSERT INTO Contacts VALUES('Albert Einstein')");
+ query.exec("INSERT INTO Contacts VALUES('Ernest Hemingway')");
+ query.exec("INSERT INTO Contacts VALUES('Hans Gude')");
+}
+
+SqlContactModel::SqlContactModel(QObject *parent) :
+ QSqlQueryModel(parent)
+{
+ createTable();
+
+ QSqlQuery query;
+ if (!query.exec("SELECT * FROM Contacts"))
+ qFatal("Contacts SELECT query failed: %s", qPrintable(query.lastError().text()));
+
+ setQuery(query);
+ if (lastError().isValid())
+ qFatal("Cannot set query on SqlContactModel: %s", qPrintable(lastError().text()));
+}
diff --git a/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.h b/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.h
new file mode 100644
index 0000000000..5953c76258
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/sqlcontactmodel.h
@@ -0,0 +1,62 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef SQLCONTACTMODEL_H
+#define SQLCONTACTMODEL_H
+
+#include <QSqlQueryModel>
+
+class SqlContactModel : public QSqlQueryModel
+{
+public:
+ SqlContactModel(QObject *parent = nullptr);
+};
+
+#endif // SQLCONTACTMODEL_H
diff --git a/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.cpp b/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.cpp
new file mode 100644
index 0000000000..99526e81d3
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.cpp
@@ -0,0 +1,154 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "sqlconversationmodel.h"
+
+#include <QDateTime>
+#include <QDebug>
+#include <QSqlError>
+#include <QSqlRecord>
+#include <QSqlQuery>
+
+static const char *conversationsTableName = "Conversations";
+
+static void createTable()
+{
+ if (QSqlDatabase::database().tables().contains(conversationsTableName)) {
+ // The table already exists; we don't need to do anything.
+ return;
+ }
+
+ QSqlQuery query;
+ if (!query.exec(
+ "CREATE TABLE IF NOT EXISTS 'Conversations' ("
+ "'author' TEXT NOT NULL,"
+ "'recipient' TEXT NOT NULL,"
+ "'timestamp' TEXT NOT NULL,"
+ "'message' TEXT NOT NULL,"
+ "FOREIGN KEY('author') REFERENCES Contacts ( name ),"
+ "FOREIGN KEY('recipient') REFERENCES Contacts ( name )"
+ ")")) {
+ qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
+ }
+
+ query.exec("INSERT INTO Conversations VALUES('Me', 'Ernest Hemingway', '2016-01-07T14:36:06', 'Hello!')");
+ query.exec("INSERT INTO Conversations VALUES('Ernest Hemingway', 'Me', '2016-01-07T14:36:16', 'Good afternoon.')");
+ query.exec("INSERT INTO Conversations VALUES('Me', 'Albert Einstein', '2016-01-01T11:24:53', 'Hi!')");
+ query.exec("INSERT INTO Conversations VALUES('Albert Einstein', 'Me', '2016-01-07T14:36:16', 'Good morning.')");
+ query.exec("INSERT INTO Conversations VALUES('Hans Gude', 'Me', '2015-11-20T06:30:02', 'God morgen. Har du fått mitt maleri?')");
+ query.exec("INSERT INTO Conversations VALUES('Me', 'Hans Gude', '2015-11-20T08:21:03', 'God morgen, Hans. Ja, det er veldig fint. Tusen takk! "
+ "Hvor mange timer har du brukt på den?')");
+}
+
+SqlConversationModel::SqlConversationModel(QObject *parent) :
+ QSqlTableModel(parent)
+{
+ createTable();
+ setTable(conversationsTableName);
+ setSort(2, Qt::DescendingOrder);
+ // Ensures that the model is sorted correctly after submitting a new row.
+ setEditStrategy(QSqlTableModel::OnManualSubmit);
+}
+
+QString SqlConversationModel::recipient() const
+{
+ return m_recipient;
+}
+
+void SqlConversationModel::setRecipient(const QString &recipient)
+{
+ if (recipient == m_recipient)
+ return;
+
+ m_recipient = recipient;
+
+ const QString filterString = QString::fromLatin1(
+ "(recipient = '%1' AND author = 'Me') OR (recipient = 'Me' AND author='%1')").arg(m_recipient);
+ setFilter(filterString);
+ select();
+
+ emit recipientChanged();
+}
+
+QVariant SqlConversationModel::data(const QModelIndex &index, int role) const
+{
+ if (role < Qt::UserRole)
+ return QSqlTableModel::data(index, role);
+
+ const QSqlRecord sqlRecord = record(index.row());
+ return sqlRecord.value(role - Qt::UserRole);
+}
+
+QHash<int, QByteArray> SqlConversationModel::roleNames() const
+{
+ QHash<int, QByteArray> names;
+ names[Qt::UserRole] = "author";
+ names[Qt::UserRole + 1] = "recipient";
+ names[Qt::UserRole + 2] = "timestamp";
+ names[Qt::UserRole + 3] = "message";
+ return names;
+}
+
+void SqlConversationModel::sendMessage(const QString &recipient, const QString &message)
+{
+ const QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
+
+ QSqlRecord newRecord = record();
+ newRecord.setValue("author", "Me");
+ newRecord.setValue("recipient", recipient);
+ newRecord.setValue("timestamp", timestamp);
+ newRecord.setValue("message", message);
+ if (!insertRecord(rowCount(), newRecord)) {
+ qWarning() << "Failed to send message:" << lastError().text();
+ return;
+ }
+
+ submitAll();
+}
diff --git a/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.h b/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.h
new file mode 100644
index 0000000000..9c65d64a84
--- /dev/null
+++ b/examples/quickcontrols2/chattutorial/chapter5/sqlconversationmodel.h
@@ -0,0 +1,79 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef SQLCONVERSATIONMODEL_H
+#define SQLCONVERSATIONMODEL_H
+
+#include <QSqlTableModel>
+
+class SqlConversationModel : public QSqlTableModel
+{
+ Q_OBJECT
+ Q_PROPERTY(QString recipient READ recipient WRITE setRecipient NOTIFY recipientChanged)
+
+public:
+ SqlConversationModel(QObject *parent = nullptr);
+
+ QString recipient() const;
+ void setRecipient(const QString &recipient);
+
+ QVariant data(const QModelIndex &index, int role) const override;
+ QHash<int, QByteArray> roleNames() const override;
+
+ Q_INVOKABLE void sendMessage(const QString &recipient, const QString &message);
+
+signals:
+ void recipientChanged();
+
+private:
+ QString m_recipient;
+};
+
+#endif // SQLCONVERSATIONMODEL_H