diff options
Diffstat (limited to 'sources/pyside6/doc/tutorials/qmlsqlintegration')
4 files changed, 66 insertions, 167 deletions
diff --git a/sources/pyside6/doc/tutorials/qmlsqlintegration/chat.qml b/sources/pyside6/doc/tutorials/qmlsqlintegration/chat.qml index 453be1252..da58ae9b2 100644 --- a/sources/pyside6/doc/tutorials/qmlsqlintegration/chat.qml +++ b/sources/pyside6/doc/tutorials/qmlsqlintegration/chat.qml @@ -1,46 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of Qt for Python. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -import QtQuick 2.12 -import QtQuick.Layouts 1.12 -import QtQuick.Controls 2.12 -import ChatModel 1.0 +import QtQuick +import QtQuick.Layouts +import QtQuick.Controls +import ChatModel ApplicationWindow { id: window @@ -54,7 +18,7 @@ ApplicationWindow { } ColumnLayout { - anchors.fill: parent + anchors.fill: window ListView { id: listView diff --git a/sources/pyside6/doc/tutorials/qmlsqlintegration/main.py b/sources/pyside6/doc/tutorials/qmlsqlintegration/main.py index 3cd19e96e..314fd5aa5 100644 --- a/sources/pyside6/doc/tutorials/qmlsqlintegration/main.py +++ b/sources/pyside6/doc/tutorials/qmlsqlintegration/main.py @@ -1,41 +1,5 @@ -############################################################################# -## -## Copyright (C) 2021 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the Qt for Python project. -## -## $QT_BEGIN_LICENSE:LGPL$ -## 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. -## -## GNU Lesser General Public License Usage -## Alternatively, this file may be used under the terms of the GNU Lesser -## General Public License version 3 as published by the Free Software -## Foundation and appearing in the file LICENSE.LGPL3 included in the -## packaging of this file. Please review the following information to -## ensure the GNU Lesser General Public License version 3 requirements -## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 2.0 or (at your option) the GNU General -## Public license version 3 or any later version approved by the KDE Free -## Qt Foundation. The licenses are as published by the Free Software -## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-2.0.html and -## https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only import sys import logging diff --git a/sources/pyside6/doc/tutorials/qmlsqlintegration/qmlsqlintegration.rst b/sources/pyside6/doc/tutorials/qmlsqlintegration/qmlsqlintegration.rst index b7d627282..eee3f807e 100644 --- a/sources/pyside6/doc/tutorials/qmlsqlintegration/qmlsqlintegration.rst +++ b/sources/pyside6/doc/tutorials/qmlsqlintegration/qmlsqlintegration.rst @@ -4,7 +4,7 @@ QML, SQL and PySide Integration Tutorial This tutorial is very similar to the `Qt Chat Tutorial`_ one but it focuses on explaining how to integrate a SQL database into a PySide6 application using QML for its UI. -.. _`Qt Chat Tutorial`: https://doc.qt.io/qt-5/qtquickcontrols-chattutorial-example.html +.. _`Qt Chat Tutorial`: https://doc.qt.io/qt-6/qtquickcontrols-chattutorial-example.html sqlDialog.py ------------ @@ -14,9 +14,9 @@ name of our table, and define the global function ``createTable()`` that creates doesn't already exist. The database contains a single line to mock the beginning of a conversation. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 40-77 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 4-43 The ``SqlConversationModel`` class offers the read-only data model required for the non-editable contacts list. It derives from the :ref:`QSqlQueryModel` class, which is the logical choice for @@ -26,25 +26,25 @@ Then, we proceed to create the table, set its name to the one defined previously We add the necessary attributes to the table, to have a program that reflects the idea of a chat application. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 80-91 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 47-59 In ``setRecipient()``, you set a filter over the returned results from the database, and emit a signal every time the recipient of the message changes. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 93-103 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 61-70 The ``data()`` function falls back to ``QSqlTableModel``'s implementation if the role is not a custom user role. If you get a user role, we can subtract :meth:`~.QtCore.Qt.UserRole` from it to get the index of that field, and then use that index to find the value to be returned. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 105-112 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 72-79 In ``roleNames()``, we return a Python dictionary with our custom role and role names as key-values @@ -53,27 +53,27 @@ Alternatively, it can be useful to declare an Enum to hold all of the role value Note that ``names`` has to be a hash to be used as a dictionary key, and that's why we're using the ``hash`` function. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 114-128 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 81-95 The ``send_message()`` function uses the given recipient and message to insert a new record into the database. Using :meth:`~.QSqlTableModel.OnManualSubmit` requires you to also call ``submitAll()``, since all the changes will be cached in the model until you do so. - .. literalinclude:: sqlDialog.py - :linenos: - :lines: 130-145 +.. literalinclude:: sqlDialog.py + :linenos: + :lines: 97-116 chat.qml -------- Let's look at the ``chat.qml`` file. - .. literalinclude:: chat.qml - :linenos: - :lines: 40-42 +.. literalinclude:: chat.qml + :linenos: + :lines: 4-6 First, import the Qt Quick module. This gives us access to graphical primitives such as Item, Rectangle, Text, and so on. @@ -86,9 +86,9 @@ root type, Window: Let's step through the ``chat.qml`` file. - .. literalinclude:: chat.qml - :linenos: - :lines: 44-49 +.. literalinclude:: chat.qml + :linenos: + :lines: 9-14 ``ApplicationWindow`` is a Window with some added convenience for creating a header and a footer. It also provides the foundation for popups and supports some basic styling, such as the background @@ -98,6 +98,13 @@ There are three properties that are almost always set when using ApplicationWind ``height``, and ``visible``. Once we've set these, we have a properly sized, empty window ready to be filled with content. +Because we are exposing the :code:`SqlConversationModel` class to QML, we will +declare a component to access it: + +.. literalinclude:: chat.qml + :linenos: + :lines: 16-18 + There are two ways of laying out items in QML: `Item Positioners`_ and `Qt Quick Layouts`_. - Item positioners (`Row`_, `Column`_, and so on) are useful for situations where the size of items @@ -106,9 +113,13 @@ There are two ways of laying out items in QML: `Item Positioners`_ and `Qt Quick resizable user interfaces. Below, we use `ColumnLayout`_ to vertically lay out a `ListView`_ and a `Pane`_. - .. literalinclude:: chat.qml - :linenos: - :lines: 50-53 + .. literalinclude:: chat.qml + :linenos: + :lines: 20-23 + + .. literalinclude:: chat.qml + :linenos: + :lines: 72-74 Pane is basically a rectangle whose color comes from the application's style. It's similar to `Frame`_, but it has no stroke around its border. @@ -133,18 +144,16 @@ remaining space that is left after accommodating the Pane. .. _attached properties: https://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html .. _Layout.fillWidth: https://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html#fillWidth-attached-prop .. _Layout.fillHeight: https://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html#fillHeight-attached-prop -.. _ListView: https://doc.qt.io/qt-5/qml-qtquick-listview.html .. _Qt Quick QML Types: https://doc.qt.io/qt-5/qtquick-qmlmodule.html Let's look at the ``Listview`` in detail: - .. literalinclude:: chat.qml - :linenos: - :lines: 53-99 +.. literalinclude:: chat.qml + :linenos: + :lines: 23-70 After filling the ``width`` and ``height`` of its parent, we also set some margins on the view. - Next, we set `displayMarginBeginning`_ and `displayMarginEnd`_. These properties ensure that the delegates outside the view don't disappear when you scroll at the edges of the view. @@ -168,9 +177,9 @@ At the bottom of the screen, we place a `TextArea`_ item to allow multi-line tex button to send the message. We use Pane to cover the area under these two items: - .. literalinclude:: chat.qml - :linenos: - :lines: 101-125 +.. literalinclude:: chat.qml + :linenos: + :lines: 72-96 The `TextArea`_ should fill the available width of the screen. We assign some placeholder text to provide a visual cue to the contact as to where they should begin @@ -192,18 +201,16 @@ main.py We use ``logging`` instead of Python's ``print()``, because it provides a better way to control the messages levels that our application will generate (errors, warnings, and information messages). - .. literalinclude:: main.py - :linenos: - :lines: 40-50 +.. literalinclude:: main.py + :linenos: + :lines: 4-16 ``connectToDatabase()`` creates a connection with the SQLite database, creating the actual file if it doesn't already exist. - .. literalinclude:: main.py - :linenos: - :lines: 53-72 - - +.. literalinclude:: main.py + :linenos: + :lines: 19-39 A few interesting things happen in the ``main`` function: @@ -212,14 +219,14 @@ A few interesting things happen in the ``main`` function: using the **QtWidgets** module. - Connecting to the database, - Declaring a :ref:`QQmlApplicationEngine`. - This allows you to access the QML context property to connect Python + This allows you to access the QML Elements to connect Python and QML from the conversation model we built on ``sqlDialog.py``. - Loading the ``.qml`` file that defines the UI. Finally, the Qt application runs, and your program starts. - .. literalinclude:: main.py - :linenos: - :lines: 75-85 +.. literalinclude:: main.py + :linenos: + :lines: 42-52 .. image:: example_list_view.png diff --git a/sources/pyside6/doc/tutorials/qmlsqlintegration/sqlDialog.py b/sources/pyside6/doc/tutorials/qmlsqlintegration/sqlDialog.py index 4ebb19ce2..d728aee59 100644 --- a/sources/pyside6/doc/tutorials/qmlsqlintegration/sqlDialog.py +++ b/sources/pyside6/doc/tutorials/qmlsqlintegration/sqlDialog.py @@ -1,41 +1,5 @@ -############################################################################# -## -## Copyright (C) 2021 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the Qt for Python project. -## -## $QT_BEGIN_LICENSE:LGPL$ -## 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. -## -## GNU Lesser General Public License Usage -## Alternatively, this file may be used under the terms of the GNU Lesser -## General Public License version 3 as published by the Free Software -## Foundation and appearing in the file LICENSE.LGPL3 included in the -## packaging of this file. Please review the following information to -## ensure the GNU Lesser General Public License version 3 requirements -## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 2.0 or (at your option) the GNU General -## Public license version 3 or any later version approved by the KDE Free -## Qt Foundation. The licenses are as published by the Free Software -## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-2.0.html and -## https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only import datetime import logging |