From 0779dc2ad0a7e087de71eadf64423e9610d3cc0a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:08:19 +0100 Subject: Contact List: cosmetic cleanup Change-Id: I5782451235269cac8c4e6aaf319a30151cb599ef Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/ContactDelegate.ui.qml | 14 +++++--------- examples/quickcontrols2/contactlist/ContactDialog.qml | 7 ++++--- .../quickcontrols2/contactlist/ContactDialogForm.ui.qml | 4 +--- examples/quickcontrols2/contactlist/addressmodel.cpp | 3 +-- examples/quickcontrols2/contactlist/addressmodel.h | 1 - examples/quickcontrols2/contactlist/contactlist.pro | 14 +++++--------- examples/quickcontrols2/contactlist/main.cpp | 5 ++--- 7 files changed, 18 insertions(+), 30 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/ContactDelegate.ui.qml b/examples/quickcontrols2/contactlist/ContactDelegate.ui.qml index 8c8c4722..cab26e63 100644 --- a/examples/quickcontrols2/contactlist/ContactDelegate.ui.qml +++ b/examples/quickcontrols2/contactlist/ContactDelegate.ui.qml @@ -52,16 +52,16 @@ import QtQuick 2.7 import QtQuick.Controls 2.1 AbstractButton { - clip: true - checkable: true - autoExclusive: true - id: delegate - height: 50 property alias remove: remove property alias edit: edit + width: 300 + height: 50 + clip: true + checkable: true + autoExclusive: true Column { id: column1 @@ -157,10 +157,6 @@ AbstractButton { } states: [ - State { - name: "collapsed" - when: !delegate.checked - }, State { name: "expanded" when: delegate.checked diff --git a/examples/quickcontrols2/contactlist/ContactDialog.qml b/examples/quickcontrols2/contactlist/ContactDialog.qml index ed781282..d8fa4f74 100644 --- a/examples/quickcontrols2/contactlist/ContactDialog.qml +++ b/examples/quickcontrols2/contactlist/ContactDialog.qml @@ -53,11 +53,12 @@ import QtQuick.Window 2.2 import QtQuick.Controls 2.1 Dialog { - standardButtons: Dialog.Ok | Dialog.Cancel - modal: true + id: dialog x: parent.width / 2 - width / 2 y: parent.height / 2 - height / 2 + standardButtons: Dialog.Ok | Dialog.Cancel + modal: true property int lastIndex property QtObject lastModel @@ -86,7 +87,7 @@ Dialog { dialog.open(); } - contentItem: ContactDialogForm { + contentItem: ContactDialogForm { id: form } diff --git a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml b/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml index e1f31943..3dd57875 100644 --- a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml +++ b/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml @@ -1,4 +1,3 @@ - /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. @@ -66,7 +65,7 @@ GridLayout { columns: 2 Label { - text: qsTr("Full Name:") + text: qsTr("Full Name") Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline } @@ -117,4 +116,3 @@ GridLayout { placeholderText: grid.placeholderText } } - diff --git a/examples/quickcontrols2/contactlist/addressmodel.cpp b/examples/quickcontrols2/contactlist/addressmodel.cpp index fb83186e..f5234b7a 100644 --- a/examples/quickcontrols2/contactlist/addressmodel.cpp +++ b/examples/quickcontrols2/contactlist/addressmodel.cpp @@ -79,8 +79,7 @@ QVariant AddressModel::data(const QModelIndex &index, int role) const case CityRole: return m_data.at(index.row()).city; case NumberRole: return m_data.at(index.row()).number; default: return QVariant(); - - } + } return QVariant(); } diff --git a/examples/quickcontrols2/contactlist/addressmodel.h b/examples/quickcontrols2/contactlist/addressmodel.h index 04417b95..f1c92ea2 100644 --- a/examples/quickcontrols2/contactlist/addressmodel.h +++ b/examples/quickcontrols2/contactlist/addressmodel.h @@ -64,7 +64,6 @@ public: CityRole, NumberRole }; - Q_ENUM(AdressModelRoles) AddressModel(QObject *parent = nullptr); diff --git a/examples/quickcontrols2/contactlist/contactlist.pro b/examples/quickcontrols2/contactlist/contactlist.pro index 8125d020..418aacab 100644 --- a/examples/quickcontrols2/contactlist/contactlist.pro +++ b/examples/quickcontrols2/contactlist/contactlist.pro @@ -1,23 +1,19 @@ TEMPLATE = app TARGET = contactlist - -QT += quick quickcontrols2 -CONFIG += c++11 +QT += quick HEADERS += \ addressmodel.h -SOURCES += main.cpp \ +SOURCES += \ + main.cpp \ addressmodel.cpp RESOURCES += \ - $$files(*.qml) \ - -# Additional import path used to resolve QML modules in Qt Creator's code model -QML_IMPORT_PATH=$$PWD/imports + $$files(*.qml) # Additional import path used to resolve QML modules just for Qt Quick Designer -QML_DESIGNER_IMPORT_PATH=$$PWD/designer +QML_DESIGNER_IMPORT_PATH = $$PWD/designer OTHER_FILES += \ designer/Backend/*.qml diff --git a/examples/quickcontrols2/contactlist/main.cpp b/examples/quickcontrols2/contactlist/main.cpp index 9c75d7b3..268a750a 100644 --- a/examples/quickcontrols2/contactlist/main.cpp +++ b/examples/quickcontrols2/contactlist/main.cpp @@ -48,11 +48,11 @@ ** ****************************************************************************/ -#include "addressmodel.h" - #include #include +#include "addressmodel.h" + int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); @@ -60,7 +60,6 @@ int main(int argc, char *argv[]) qmlRegisterType("Backend", 1, 0, "AddressModel"); QQmlApplicationEngine engine; - engine.addImportPath(":/imports"); engine.load(QUrl(QStringLiteral("qrc:/contactlist.qml"))); return app.exec(); -- cgit v1.2.3 From efc119e2d5c85e2bc98f9d0904ddad223cf7f74e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:15:53 +0100 Subject: Contact List: rename AddressModel to ContactModel Change-Id: I2da41a5d108b5da23dc969d30c50138ef7e42cc3 Reviewed-by: J-P Nurmi --- .../quickcontrols2/contactlist/MainForm.ui.qml | 2 +- .../quickcontrols2/contactlist/addressmodel.cpp | 149 --------------------- examples/quickcontrols2/contactlist/addressmodel.h | 94 ------------- .../quickcontrols2/contactlist/contactlist.pro | 4 +- .../quickcontrols2/contactlist/contactmodel.cpp | 149 +++++++++++++++++++++ examples/quickcontrols2/contactlist/contactmodel.h | 94 +++++++++++++ .../contactlist/designer/Backend/AddressModel.qml | 88 ------------ .../contactlist/designer/Backend/ContactModel.qml | 88 ++++++++++++ .../contactlist/designer/Backend/qmldir | 2 +- .../doc/src/qtquickcontrols2-contactlist.qdoc | 8 +- examples/quickcontrols2/contactlist/main.cpp | 4 +- 11 files changed, 341 insertions(+), 341 deletions(-) delete mode 100644 examples/quickcontrols2/contactlist/addressmodel.cpp delete mode 100644 examples/quickcontrols2/contactlist/addressmodel.h create mode 100644 examples/quickcontrols2/contactlist/contactmodel.cpp create mode 100644 examples/quickcontrols2/contactlist/contactmodel.h delete mode 100644 examples/quickcontrols2/contactlist/designer/Backend/AddressModel.qml create mode 100644 examples/quickcontrols2/contactlist/designer/Backend/ContactModel.qml (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/MainForm.ui.qml b/examples/quickcontrols2/contactlist/MainForm.ui.qml index 634398bd..3be33c1f 100644 --- a/examples/quickcontrols2/contactlist/MainForm.ui.qml +++ b/examples/quickcontrols2/contactlist/MainForm.ui.qml @@ -96,7 +96,7 @@ Page { } } - model: AddressModel { + model: ContactModel { } ScrollBar.vertical: ScrollBar { diff --git a/examples/quickcontrols2/contactlist/addressmodel.cpp b/examples/quickcontrols2/contactlist/addressmodel.cpp deleted file mode 100644 index f5234b7a..00000000 --- a/examples/quickcontrols2/contactlist/addressmodel.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 "addressmodel.h" - -AddressModel::AddressModel(QObject *parent ) : QAbstractListModel(parent) -{ - m_data.append({ "Angel Hogan", "Chapel St. 368 ", "Clearwater" , "0311 1823993" }); - m_data.append({ "Felicia Patton", "Annadale Lane 2", "Knoxville" , "0368 1244494" }); - m_data.append({ "Grant Crawford", "Windsor Drive 34", "Riverdale" , "0351 7826892" }); - m_data.append({ "Gretchen Little", "Sunset Drive 348", "Virginia Beach" , "0343 1234991" }); - m_data.append({ "Geoffrey Richards", "University Lane 54", "Trussville" , "0423 2144944" }); - m_data.append({ "Henrietta Chavez", "Via Volto San Luca 3", "Piobesi Torinese" , "0399 2826994" }); - m_data.append({ "Harvey Chandler", "North Squaw Creek 11", "Madisonville" , "0343 1244492" }); - m_data.append({ "Miguel Gomez", "Wild Rose Street 13", "Trussville" , "0343 9826996" }); - m_data.append({ "Norma Rodriguez", " Glen Eagles Street 53", "Buffalo" , "0241 5826596" }); - m_data.append({ "Shelia Ramirez", "East Miller Ave 68", "Pickerington" , "0346 4844556" }); - m_data.append({ "Stephanie Moss", "Piazza Trieste e Trento 77", "Roata Chiusani" , "0363 0510490" }); -} - -int AddressModel::rowCount(const QModelIndex &) const -{ - return m_data.count(); -} - -QVariant AddressModel::data(const QModelIndex &index, int role) const -{ - if (index.row() < rowCount()) - switch (role) { - case FullNameRole: return m_data.at(index.row()).fullName; - case AddressRole: return m_data.at(index.row()).address; - case CityRole: return m_data.at(index.row()).city; - case NumberRole: return m_data.at(index.row()).number; - default: return QVariant(); - } - return QVariant(); -} - -QHash AddressModel::roleNames() const -{ - static QHash roleNames { - { FullNameRole, "fullName" }, - { AddressRole, "address" }, - { CityRole, "city" }, - { NumberRole, "number" } - }; - return roleNames; -} - -void AddressModel::updateContact(int row, - const QString &fullName, - const QString &address, - const QString &city, - const QString &number) -{ - if (row >= 0 && row < rowCount()) { - m_data.replace(row, { fullName, address, city, number }); - dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); - } else if (row < 0) { - beginInsertRows(QModelIndex(), rowCount() - 1, rowCount() - 1); - m_data.append({fullName, address, city, number}); - endInsertRows(); - dataChanged(index(rowCount() - 1, 0), index(rowCount() - 1, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); - } -} - -void AddressModel::removeContact(int row) -{ - if (row >= 0 && row < rowCount()) { - beginRemoveRows(QModelIndex(), row, row); - m_data.removeAt(row); - endRemoveRows(); - } -} - -QString AddressModel::getFullName(int row) const -{ - if (row >= 0 && row < rowCount()) - return m_data.at(row).fullName; - return QString(); -} - -QString AddressModel::getAddress(int row) const -{ - if (row >= 0 && row < rowCount()) - return m_data.at(row).address; - return QString(); -} - -QString AddressModel::getCity(int row) const -{ - if (row >= 0 && row < rowCount()) - return m_data.at(row).city; - return QString(); -} - -QString AddressModel::getNumber(int row) const -{ - if (row >= 0 && row < rowCount()) - return m_data.at(row).number; - return QString(); -} diff --git a/examples/quickcontrols2/contactlist/addressmodel.h b/examples/quickcontrols2/contactlist/addressmodel.h deleted file mode 100644 index f1c92ea2..00000000 --- a/examples/quickcontrols2/contactlist/addressmodel.h +++ /dev/null @@ -1,94 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 ADDRESSMODEL_H -#define ADDRESSMODEL_H - -#include - -class AddressModel : public QAbstractListModel -{ - Q_OBJECT - -public: - enum AdressModelRoles { - FullNameRole = Qt::DisplayRole, - AddressRole = Qt::UserRole, - CityRole, - NumberRole - }; - Q_ENUM(AdressModelRoles) - - AddressModel(QObject *parent = nullptr); - - int rowCount(const QModelIndex & = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - QHash roleNames() const; - - Q_INVOKABLE QString getFullName(int row) const; - Q_INVOKABLE QString getAddress(int row) const; - Q_INVOKABLE QString getCity(int row) const; - Q_INVOKABLE QString getNumber(int row) const; - - Q_INVOKABLE void updateContact(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); - Q_INVOKABLE void removeContact(int row); - -private: - struct Data { - QString fullName; - QString address; - QString city; - QString number; - }; - - QList m_data; -}; - -#endif // ADDRESSMODEL_H diff --git a/examples/quickcontrols2/contactlist/contactlist.pro b/examples/quickcontrols2/contactlist/contactlist.pro index 418aacab..9025dbde 100644 --- a/examples/quickcontrols2/contactlist/contactlist.pro +++ b/examples/quickcontrols2/contactlist/contactlist.pro @@ -3,11 +3,11 @@ TARGET = contactlist QT += quick HEADERS += \ - addressmodel.h + contactmodel.h SOURCES += \ main.cpp \ - addressmodel.cpp + contactmodel.cpp RESOURCES += \ $$files(*.qml) diff --git a/examples/quickcontrols2/contactlist/contactmodel.cpp b/examples/quickcontrols2/contactlist/contactmodel.cpp new file mode 100644 index 00000000..3ffaed8c --- /dev/null +++ b/examples/quickcontrols2/contactlist/contactmodel.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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 "contactmodel.h" + +ContactModel::ContactModel(QObject *parent ) : QAbstractListModel(parent) +{ + m_contacts.append({ "Angel Hogan", "Chapel St. 368 ", "Clearwater" , "0311 1823993" }); + m_contacts.append({ "Felicia Patton", "Annadale Lane 2", "Knoxville" , "0368 1244494" }); + m_contacts.append({ "Grant Crawford", "Windsor Drive 34", "Riverdale" , "0351 7826892" }); + m_contacts.append({ "Gretchen Little", "Sunset Drive 348", "Virginia Beach" , "0343 1234991" }); + m_contacts.append({ "Geoffrey Richards", "University Lane 54", "Trussville" , "0423 2144944" }); + m_contacts.append({ "Henrietta Chavez", "Via Volto San Luca 3", "Piobesi Torinese" , "0399 2826994" }); + m_contacts.append({ "Harvey Chandler", "North Squaw Creek 11", "Madisonville" , "0343 1244492" }); + m_contacts.append({ "Miguel Gomez", "Wild Rose Street 13", "Trussville" , "0343 9826996" }); + m_contacts.append({ "Norma Rodriguez", " Glen Eagles Street 53", "Buffalo" , "0241 5826596" }); + m_contacts.append({ "Shelia Ramirez", "East Miller Ave 68", "Pickerington" , "0346 4844556" }); + m_contacts.append({ "Stephanie Moss", "Piazza Trieste e Trento 77", "Roata Chiusani" , "0363 0510490" }); +} + +int ContactModel::rowCount(const QModelIndex &) const +{ + return m_contacts.count(); +} + +QVariant ContactModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < rowCount()) + switch (role) { + case FullNameRole: return m_contacts.at(index.row()).fullName; + case AddressRole: return m_contacts.at(index.row()).address; + case CityRole: return m_contacts.at(index.row()).city; + case NumberRole: return m_contacts.at(index.row()).number; + default: return QVariant(); + } + return QVariant(); +} + +QHash ContactModel::roleNames() const +{ + static QHash roleNames { + { FullNameRole, "fullName" }, + { AddressRole, "address" }, + { CityRole, "city" }, + { NumberRole, "number" } + }; + return roleNames; +} + +void ContactModel::updateContact(int row, + const QString &fullName, + const QString &address, + const QString &city, + const QString &number) +{ + if (row >= 0 && row < rowCount()) { + m_contacts.replace(row, { fullName, address, city, number }); + dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); + } else if (row < 0) { + beginInsertRows(QModelIndex(), rowCount() - 1, rowCount() - 1); + m_contacts.append({fullName, address, city, number}); + endInsertRows(); + dataChanged(index(rowCount() - 1, 0), index(rowCount() - 1, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); + } +} + +void ContactModel::removeContact(int row) +{ + if (row >= 0 && row < rowCount()) { + beginRemoveRows(QModelIndex(), row, row); + m_contacts.removeAt(row); + endRemoveRows(); + } +} + +QString ContactModel::getFullName(int row) const +{ + if (row >= 0 && row < rowCount()) + return m_contacts.at(row).fullName; + return QString(); +} + +QString ContactModel::getAddress(int row) const +{ + if (row >= 0 && row < rowCount()) + return m_contacts.at(row).address; + return QString(); +} + +QString ContactModel::getCity(int row) const +{ + if (row >= 0 && row < rowCount()) + return m_contacts.at(row).city; + return QString(); +} + +QString ContactModel::getNumber(int row) const +{ + if (row >= 0 && row < rowCount()) + return m_contacts.at(row).number; + return QString(); +} diff --git a/examples/quickcontrols2/contactlist/contactmodel.h b/examples/quickcontrols2/contactlist/contactmodel.h new file mode 100644 index 00000000..2776905f --- /dev/null +++ b/examples/quickcontrols2/contactlist/contactmodel.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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 CONTACTMODEL_H +#define CONTACTMODEL_H + +#include + +class ContactModel : public QAbstractListModel +{ + Q_OBJECT + +public: + enum ContactRole { + FullNameRole = Qt::DisplayRole, + AddressRole = Qt::UserRole, + CityRole, + NumberRole + }; + Q_ENUM(ContactRole) + + ContactModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex & = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QHash roleNames() const; + + Q_INVOKABLE QString getFullName(int row) const; + Q_INVOKABLE QString getAddress(int row) const; + Q_INVOKABLE QString getCity(int row) const; + Q_INVOKABLE QString getNumber(int row) const; + + Q_INVOKABLE void updateContact(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); + Q_INVOKABLE void removeContact(int row); + +private: + struct Contact { + QString fullName; + QString address; + QString city; + QString number; + }; + + QList m_contacts; +}; + +#endif // CONTACTMODEL_H diff --git a/examples/quickcontrols2/contactlist/designer/Backend/AddressModel.qml b/examples/quickcontrols2/contactlist/designer/Backend/AddressModel.qml deleted file mode 100644 index b9570934..00000000 --- a/examples/quickcontrols2/contactlist/designer/Backend/AddressModel.qml +++ /dev/null @@ -1,88 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 2.6 - -ListModel { - ListElement { - address: "Chapel St. 368" - city: "Knoxville" - number: "0311 1823993" - fullName: "Angel Hogan" - } - - ListElement { - address: "Annadale Lane 2" - city: "Clearwater" - number: "0368 1244494" - fullName: "Felicia Patton" - } - - ListElement { - address: "Windsor Drive 34" - city: "Riverdale" - number: "0368 1244494" - fullName: "Grant Crawford" - } - - ListElement { - address: "Sunset Drive 348" - city: "Virginia Beach" - number: "0351 7826892" - fullName: "Gretchen Little" - } - - ListElement { - address: "University Lane 54" - city: "Trussville" - number: "0399 2826994" - fullName: "Geoffrey Richards" - } -} diff --git a/examples/quickcontrols2/contactlist/designer/Backend/ContactModel.qml b/examples/quickcontrols2/contactlist/designer/Backend/ContactModel.qml new file mode 100644 index 00000000..b9570934 --- /dev/null +++ b/examples/quickcontrols2/contactlist/designer/Backend/ContactModel.qml @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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 2.6 + +ListModel { + ListElement { + address: "Chapel St. 368" + city: "Knoxville" + number: "0311 1823993" + fullName: "Angel Hogan" + } + + ListElement { + address: "Annadale Lane 2" + city: "Clearwater" + number: "0368 1244494" + fullName: "Felicia Patton" + } + + ListElement { + address: "Windsor Drive 34" + city: "Riverdale" + number: "0368 1244494" + fullName: "Grant Crawford" + } + + ListElement { + address: "Sunset Drive 348" + city: "Virginia Beach" + number: "0351 7826892" + fullName: "Gretchen Little" + } + + ListElement { + address: "University Lane 54" + city: "Trussville" + number: "0399 2826994" + fullName: "Geoffrey Richards" + } +} diff --git a/examples/quickcontrols2/contactlist/designer/Backend/qmldir b/examples/quickcontrols2/contactlist/designer/Backend/qmldir index 45552843..8e2037d4 100644 --- a/examples/quickcontrols2/contactlist/designer/Backend/qmldir +++ b/examples/quickcontrols2/contactlist/designer/Backend/qmldir @@ -1,2 +1,2 @@ module Backend -AddressModel 1.0 AddressModel.qml +ContactModel 1.0 ContactModel.qml diff --git a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc index 93ef569d..a6a25b49 100644 --- a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc +++ b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc @@ -43,27 +43,27 @@ The contact list application allows the user to add, edit, and remove contacts. The actual implementation is done in C++ and exposed as a QAbstractListModel. - The AdressModel C++ class is registered under a namespace and later + The ContactModel C++ class is registered under a namespace and later imported and instantiated by \e MainForm.ui.qml. For more information about registering C++ classes as QML types, see \l {Defining QML Types from C++}. \code #include ... - qmlRegisterType("Backend", 1, 0, "AdressModel"); + qmlRegisterType("Backend", 1, 0, "ContactModel"); ... \endcode \section1 Designer Support - In the designer subdirectory, we create a plugin that replaces the AdressModel + In the designer subdirectory, we create a plugin that replaces the ContactModel in Qt Quick Designer. For this to work we add the following line to \e contactlist.pro. \code QML_DESIGNER_IMPORT_PATH=$$PWD/designer \endcode - Because Qt Quick Designer cannot instantiate the AdressModel C++ class, we define + Because Qt Quick Designer cannot instantiate the ContactModel C++ class, we define a mockup using a ListModel. This ensures that the ListView using the model shows something in Qt Quick Designer. diff --git a/examples/quickcontrols2/contactlist/main.cpp b/examples/quickcontrols2/contactlist/main.cpp index 268a750a..ccad6ff3 100644 --- a/examples/quickcontrols2/contactlist/main.cpp +++ b/examples/quickcontrols2/contactlist/main.cpp @@ -51,13 +51,13 @@ #include #include -#include "addressmodel.h" +#include "contactmodel.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - qmlRegisterType("Backend", 1, 0, "AddressModel"); + qmlRegisterType("Backend", 1, 0, "ContactModel"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/contactlist.qml"))); -- cgit v1.2.3 From bd3cb530795a6233024166a81518c5cde1e62273 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:43:28 +0100 Subject: Contact List: simplify the ContactModel getters QList::value() does the boundary checking for us. Change-Id: I6965e6be861da86d35e6f30f74c94ba39e4ad2ea Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/contactmodel.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/contactmodel.cpp b/examples/quickcontrols2/contactlist/contactmodel.cpp index 3ffaed8c..3252c80a 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.cpp +++ b/examples/quickcontrols2/contactlist/contactmodel.cpp @@ -122,28 +122,20 @@ void ContactModel::removeContact(int row) QString ContactModel::getFullName(int row) const { - if (row >= 0 && row < rowCount()) - return m_contacts.at(row).fullName; - return QString(); + return m_contacts.value(row).fullName; } QString ContactModel::getAddress(int row) const { - if (row >= 0 && row < rowCount()) - return m_contacts.at(row).address; - return QString(); + return m_contacts.value(row).address; } QString ContactModel::getCity(int row) const { - if (row >= 0 && row < rowCount()) - return m_contacts.at(row).city; - return QString(); + return m_contacts.value(row).city; } QString ContactModel::getNumber(int row) const { - if (row >= 0 && row < rowCount()) - return m_contacts.at(row).number; - return QString(); + return m_contacts.value(row).number; } -- cgit v1.2.3 From 24135738e48464d5ca633fd5dc304f006fbfe5c1 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:50:17 +0100 Subject: Contact List: fix model insertion New contacts must be inserted in alphabetical order so the sections stay sane. Change-Id: I39279bbeb429198b38a2b4da1d76eb50bfab723c Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/contactmodel.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/contactmodel.cpp b/examples/quickcontrols2/contactlist/contactmodel.cpp index 3252c80a..1f435a56 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.cpp +++ b/examples/quickcontrols2/contactlist/contactmodel.cpp @@ -104,10 +104,12 @@ void ContactModel::updateContact(int row, m_contacts.replace(row, { fullName, address, city, number }); dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); } else if (row < 0) { - beginInsertRows(QModelIndex(), rowCount() - 1, rowCount() - 1); - m_contacts.append({fullName, address, city, number}); + row = 0; + while (row < m_contacts.count() && fullName > m_contacts.at(row).fullName) + ++row; + beginInsertRows(QModelIndex(), row, row); + m_contacts.insert(row, {fullName, address, city, number}); endInsertRows(); - dataChanged(index(rowCount() - 1, 0), index(rowCount() - 1, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); } } -- cgit v1.2.3 From bc45c43ab7cbd9ea53b2636e8751c55e6d3e22fd Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:32:53 +0100 Subject: Contact List: cleanup the list view No clipping, current index, or x-coordinates needed. Change-Id: Ibfb4a8e1159072a84ee229607b28e7f4010eb925 Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/MainForm.ui.qml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/MainForm.ui.qml b/examples/quickcontrols2/contactlist/MainForm.ui.qml index 3be33c1f..fdc6dd91 100644 --- a/examples/quickcontrols2/contactlist/MainForm.ui.qml +++ b/examples/quickcontrols2/contactlist/MainForm.ui.qml @@ -67,14 +67,12 @@ Page { ListView { id: listView - currentIndex: -1 - boundsBehavior: Flickable.StopAtBounds - clip: true focus: true anchors.fill: parent section.criteria: ViewSection.FirstCharacter section.property: "fullName" snapMode: ListView.SnapToItem + boundsBehavior: Flickable.StopAtBounds section.delegate: SectionDelegate { width: listView.width @@ -83,7 +81,6 @@ Page { delegate: ContactDelegate { id: delegate width: listView.width - x: 5 Connections { target: delegate.edit -- cgit v1.2.3 From 4c2625b199e265d1d30ef2ad662e77319df80c74 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:38:44 +0100 Subject: Contact List: add dialog titles Change-Id: Ia842791e28380fd672d3edfe0e6acad0613a64ae Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/ContactDialog.qml | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/ContactDialog.qml b/examples/quickcontrols2/contactlist/ContactDialog.qml index d8fa4f74..c3b3f731 100644 --- a/examples/quickcontrols2/contactlist/ContactDialog.qml +++ b/examples/quickcontrols2/contactlist/ContactDialog.qml @@ -73,6 +73,7 @@ Dialog { lastIndex = -1; lastModel = model; + dialog.title = qsTr("Add Contact") dialog.open(); } @@ -84,6 +85,7 @@ Dialog { lastIndex = index; lastModel = model; + dialog.title = qsTr("Edit Contact") dialog.open(); } -- cgit v1.2.3 From 82a7941020b04ffb9b78a9c94ac8d75bbdb099e9 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:51:53 +0100 Subject: Contact List: give the contact dialog focus Change-Id: Id8d48a42e283e262c44a9f2c64268459a4a11050 Reviewed-by: J-P Nurmi --- examples/quickcontrols2/contactlist/ContactDialog.qml | 1 + examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml | 1 + 2 files changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/ContactDialog.qml b/examples/quickcontrols2/contactlist/ContactDialog.qml index c3b3f731..fd0ddbfd 100644 --- a/examples/quickcontrols2/contactlist/ContactDialog.qml +++ b/examples/quickcontrols2/contactlist/ContactDialog.qml @@ -59,6 +59,7 @@ Dialog { y: parent.height / 2 - height / 2 standardButtons: Dialog.Ok | Dialog.Cancel modal: true + focus: true property int lastIndex property QtObject lastModel diff --git a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml b/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml index 3dd57875..f3397bb0 100644 --- a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml +++ b/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml @@ -71,6 +71,7 @@ GridLayout { TextField { id: fullName + focus: true Layout.fillWidth: true Layout.minimumWidth: grid.minimumInputSize Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline -- cgit v1.2.3 From 0cfadeb6de542548c45ef485a4452e820cefb401 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 17 Nov 2016 22:23:30 +0100 Subject: Contact List: tweak the section header Replace the old-fashioned gradient with a ToolBar. It stands out more than well enough, and is fully style-agnostic. Also, give descriptive IDs for better designer integration/preview. Change-Id: Ib2d4404e3c0fa769ac661645046a25e578f62d37 Reviewed-by: Mitch Curtis --- .../quickcontrols2/contactlist/MainForm.ui.qml | 4 ++-- .../contactlist/SectionDelegate.ui.qml | 25 ++++++---------------- 2 files changed, 8 insertions(+), 21 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/MainForm.ui.qml b/examples/quickcontrols2/contactlist/MainForm.ui.qml index fdc6dd91..1639ecc8 100644 --- a/examples/quickcontrols2/contactlist/MainForm.ui.qml +++ b/examples/quickcontrols2/contactlist/MainForm.ui.qml @@ -69,11 +69,11 @@ Page { focus: true anchors.fill: parent - section.criteria: ViewSection.FirstCharacter - section.property: "fullName" snapMode: ListView.SnapToItem boundsBehavior: Flickable.StopAtBounds + section.property: "fullName" + section.criteria: ViewSection.FirstCharacter section.delegate: SectionDelegate { width: listView.width } diff --git a/examples/quickcontrols2/contactlist/SectionDelegate.ui.qml b/examples/quickcontrols2/contactlist/SectionDelegate.ui.qml index 3b9f7dc5..fa64ffbf 100644 --- a/examples/quickcontrols2/contactlist/SectionDelegate.ui.qml +++ b/examples/quickcontrols2/contactlist/SectionDelegate.ui.qml @@ -51,27 +51,14 @@ import QtQuick 2.7 import QtQuick.Controls 2.1 -Rectangle { - height: 20 - width: 120 - gradient: Gradient { - GradientStop { - position: 0 - color: "#dddddd" - } - - GradientStop { - position: 1 - color: "blue" - } - } +ToolBar { + id: background Label { - x: 12 - color: "white" + id: label text: section - style: Text.Raised - font.pixelSize: 18 - font.bold: true + anchors.fill: parent + horizontalAlignment: Qt.AlignHCenter + verticalAlignment: Qt.AlignVCenter } } -- cgit v1.2.3 From 1eeeca9d202ceaf11b2935ebe590a85145d33ef5 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 18 Nov 2016 14:06:19 +0100 Subject: Overhaul the Contact List example Some renames, API tweaks, and structural changes. Glue the pieces (view/model/dialog) together in contactlist.qml. Change-Id: Id3a31248b391838c6d39f2f0f355e1c35456ccf5 Reviewed-by: Mitch Curtis --- .../quickcontrols2/contactlist/ContactDialog.qml | 49 ++++----- .../contactlist/ContactDialogForm.ui.qml | 119 --------------------- .../quickcontrols2/contactlist/ContactForm.ui.qml | 119 +++++++++++++++++++++ .../quickcontrols2/contactlist/ContactView.ui.qml | 116 ++++++++++++++++++++ .../quickcontrols2/contactlist/MainForm.ui.qml | 110 ------------------- .../quickcontrols2/contactlist/contactlist.qml | 22 +++- .../quickcontrols2/contactlist/contactmodel.cpp | 59 ++++------ examples/quickcontrols2/contactlist/contactmodel.h | 11 +- 8 files changed, 303 insertions(+), 302 deletions(-) delete mode 100644 examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml create mode 100644 examples/quickcontrols2/contactlist/ContactForm.ui.qml create mode 100644 examples/quickcontrols2/contactlist/ContactView.ui.qml delete mode 100644 examples/quickcontrols2/contactlist/MainForm.ui.qml (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/ContactDialog.qml b/examples/quickcontrols2/contactlist/ContactDialog.qml index fd0ddbfd..1836ea28 100644 --- a/examples/quickcontrols2/contactlist/ContactDialog.qml +++ b/examples/quickcontrols2/contactlist/ContactDialog.qml @@ -49,52 +49,47 @@ ****************************************************************************/ import QtQuick 2.7 -import QtQuick.Window 2.2 import QtQuick.Controls 2.1 Dialog { id: dialog - x: parent.width / 2 - width / 2 - y: parent.height / 2 - height / 2 - standardButtons: Dialog.Ok | Dialog.Cancel - modal: true - focus: true - - property int lastIndex - property QtObject lastModel - - property alias form: form + property int index: -1 + signal finished(string fullName, string address, string city, string number) - function addContact(model) { + function createContact() { form.fullName.clear(); form.address.clear(); form.city.clear(); form.number.clear(); - lastIndex = -1; - lastModel = model; - dialog.title = qsTr("Add Contact") + dialog.title = qsTr("Add Contact"); + dialog.index = -1; dialog.open(); } - function editContact(model, index) { - form.fullName.text = model.getFullName(index); - form.address.text = model.getAddress(index); - form.city.text = model.getCity(index); - form.number.text = model.getNumber(index); - lastIndex = index; - lastModel = model; + function editContact(index, contact) { + form.fullName.text = contact.fullName; + form.address.text = contact.address; + form.city.text = contact.city; + form.number.text = contact.number; - dialog.title = qsTr("Edit Contact") + dialog.title = qsTr("Edit Contact"); + dialog.index = index; dialog.open(); } - contentItem: ContactDialogForm { + x: parent.width / 2 - width / 2 + y: parent.height / 2 - height / 2 + + focus: true + modal: true + title: qsTr("Add Contact") + standardButtons: Dialog.Ok | Dialog.Cancel + + contentItem: ContactForm { id: form } - onAccepted: lastModel.updateContact(lastIndex, form.fullName.text, - form.address.text, form.city.text, - form.number.text) + onAccepted: finished(form.fullName.text, form.address.text, form.city.text, form.number.text) } diff --git a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml b/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml deleted file mode 100644 index f3397bb0..00000000 --- a/examples/quickcontrols2/contactlist/ContactDialogForm.ui.qml +++ /dev/null @@ -1,119 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 2.7 -import QtQuick.Layouts 1.0 -import QtQuick.Controls 2.1 - -GridLayout { - id: grid - property alias fullName: fullName - property alias address: address - property alias city: city - property alias number: number - property int minimumInputSize: 120 - property string placeholderText: qsTr("") - - rows: 4 - columns: 2 - - Label { - text: qsTr("Full Name") - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - } - - TextField { - id: fullName - focus: true - Layout.fillWidth: true - Layout.minimumWidth: grid.minimumInputSize - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - placeholderText: grid.placeholderText - } - - Label { - text: qsTr("Address") - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - } - - TextField { - id: address - Layout.fillWidth: true - Layout.minimumWidth: grid.minimumInputSize - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - placeholderText: grid.placeholderText - } - - Label { - text: qsTr("City") - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - } - - TextField { - id: city - Layout.fillWidth: true - Layout.minimumWidth: grid.minimumInputSize - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - placeholderText: grid.placeholderText - } - - Label { - text: qsTr("Number") - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - } - - TextField { - id: number - Layout.fillWidth: true - Layout.minimumWidth: grid.minimumInputSize - Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline - placeholderText: grid.placeholderText - } -} diff --git a/examples/quickcontrols2/contactlist/ContactForm.ui.qml b/examples/quickcontrols2/contactlist/ContactForm.ui.qml new file mode 100644 index 00000000..f3397bb0 --- /dev/null +++ b/examples/quickcontrols2/contactlist/ContactForm.ui.qml @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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 2.7 +import QtQuick.Layouts 1.0 +import QtQuick.Controls 2.1 + +GridLayout { + id: grid + property alias fullName: fullName + property alias address: address + property alias city: city + property alias number: number + property int minimumInputSize: 120 + property string placeholderText: qsTr("") + + rows: 4 + columns: 2 + + Label { + text: qsTr("Full Name") + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + } + + TextField { + id: fullName + focus: true + Layout.fillWidth: true + Layout.minimumWidth: grid.minimumInputSize + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + placeholderText: grid.placeholderText + } + + Label { + text: qsTr("Address") + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + } + + TextField { + id: address + Layout.fillWidth: true + Layout.minimumWidth: grid.minimumInputSize + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + placeholderText: grid.placeholderText + } + + Label { + text: qsTr("City") + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + } + + TextField { + id: city + Layout.fillWidth: true + Layout.minimumWidth: grid.minimumInputSize + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + placeholderText: grid.placeholderText + } + + Label { + text: qsTr("Number") + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + } + + TextField { + id: number + Layout.fillWidth: true + Layout.minimumWidth: grid.minimumInputSize + Layout.alignment: Qt.AlignLeft | Qt.AlignBaseline + placeholderText: grid.placeholderText + } +} diff --git a/examples/quickcontrols2/contactlist/ContactView.ui.qml b/examples/quickcontrols2/contactlist/ContactView.ui.qml new file mode 100644 index 00000000..30a72b65 --- /dev/null +++ b/examples/quickcontrols2/contactlist/ContactView.ui.qml @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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 2.7 +import QtQuick.Controls 2.1 +import Backend 1.0 + +Page { + id: page + + signal addContact() + signal editContact(int index) + signal removeContact(int index) + + property alias model: contactModel + + width: 320 + height: 480 + + ListView { + id: listView + anchors.fill: parent + + focus: true + boundsBehavior: Flickable.StopAtBounds + + section.property: "fullName" + section.criteria: ViewSection.FirstCharacter + section.delegate: SectionDelegate { + width: listView.width + } + + delegate: ContactDelegate { + id: delegate + width: listView.width + + Connections { + target: delegate.edit + onClicked: page.editContact(index) + } + + Connections { + target: delegate.remove + onClicked: page.removeContact(index) + } + } + + model: ContactModel { + id: contactModel + } + + ScrollBar.vertical: ScrollBar { } + } + + footer: ToolBar { + id: footer + + ToolButton { + id: addButton + text: qsTr("Add Contact") + anchors.right: parent.right + + Connections { + target: addButton + onClicked: page.addContact() + } + } + } +} diff --git a/examples/quickcontrols2/contactlist/MainForm.ui.qml b/examples/quickcontrols2/contactlist/MainForm.ui.qml deleted file mode 100644 index 1639ecc8..00000000 --- a/examples/quickcontrols2/contactlist/MainForm.ui.qml +++ /dev/null @@ -1,110 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 2.7 -import Backend 1.0 -import QtQuick.Controls 2.1 - -Page { - id: form - - width: 320 - height: 480 - - property alias button: button - property alias listView: listView - - property ContactDialog dialog: ContactDialog { - } - - ListView { - id: listView - - focus: true - anchors.fill: parent - snapMode: ListView.SnapToItem - boundsBehavior: Flickable.StopAtBounds - - section.property: "fullName" - section.criteria: ViewSection.FirstCharacter - section.delegate: SectionDelegate { - width: listView.width - } - - delegate: ContactDelegate { - id: delegate - width: listView.width - - Connections { - target: delegate.edit - onClicked: dialog.editContact(listView.model, index) - } - - Connections { - target: delegate.remove - onClicked: listView.model.removeContact(index) - } - } - - model: ContactModel { - } - - ScrollBar.vertical: ScrollBar { - } - } - - footer: ToolBar { - ToolButton { - id: button - text: "Add Contact" - anchors.right: parent.right - } - } -} diff --git a/examples/quickcontrols2/contactlist/contactlist.qml b/examples/quickcontrols2/contactlist/contactlist.qml index 7ace67fa..04382407 100644 --- a/examples/quickcontrols2/contactlist/contactlist.qml +++ b/examples/quickcontrols2/contactlist/contactlist.qml @@ -52,13 +52,29 @@ import QtQuick 2.7 import QtQuick.Controls 2.1 ApplicationWindow { - visible: true + id: window + width: 320 height: 480 + visible: true title: qsTr("Contact List") - MainForm { - button.onClicked: dialog.addContact(listView.model) + ContactDialog { + id: contactDialog + onFinished: { + if (index === -1) + contactView.model.append(fullName, address, city, number) + else + contactView.model.set(index, fullName, address, city, number) + } + } + + ContactView { + id: contactView anchors.fill: parent + + onAddContact: contactDialog.createContact() + onEditContact: contactDialog.editContact(index, model.get(index)) + onRemoveContact: model.remove(index) } } diff --git a/examples/quickcontrols2/contactlist/contactmodel.cpp b/examples/quickcontrols2/contactlist/contactmodel.cpp index 1f435a56..308da2da 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.cpp +++ b/examples/quickcontrols2/contactlist/contactmodel.cpp @@ -85,59 +85,46 @@ QVariant ContactModel::data(const QModelIndex &index, int role) const QHash ContactModel::roleNames() const { - static QHash roleNames { + static const QHash roles { { FullNameRole, "fullName" }, { AddressRole, "address" }, { CityRole, "city" }, { NumberRole, "number" } }; - return roleNames; + return roles; } -void ContactModel::updateContact(int row, - const QString &fullName, - const QString &address, - const QString &city, - const QString &number) +QVariantMap ContactModel::get(int row) const { - if (row >= 0 && row < rowCount()) { - m_contacts.replace(row, { fullName, address, city, number }); - dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); - } else if (row < 0) { - row = 0; - while (row < m_contacts.count() && fullName > m_contacts.at(row).fullName) - ++row; - beginInsertRows(QModelIndex(), row, row); - m_contacts.insert(row, {fullName, address, city, number}); - endInsertRows(); - } + const Contact contact = m_contacts.value(row); + return { {"fullName", contact.fullName}, {"address", contact.address}, {"city", contact.city}, {"number", contact.number} }; } -void ContactModel::removeContact(int row) +void ContactModel::append(const QString &fullName, const QString &address, const QString &city, const QString &number) { - if (row >= 0 && row < rowCount()) { - beginRemoveRows(QModelIndex(), row, row); - m_contacts.removeAt(row); - endRemoveRows(); - } + int row = 0; + while (row < m_contacts.count() && fullName > m_contacts.at(row).fullName) + ++row; + beginInsertRows(QModelIndex(), row, row); + m_contacts.insert(row, {fullName, address, city, number}); + endInsertRows(); } -QString ContactModel::getFullName(int row) const +void ContactModel::set(int row, const QString &fullName, const QString &address, const QString &city, const QString &number) { - return m_contacts.value(row).fullName; -} + if (row < 0 || row >= m_contacts.count()) + return; -QString ContactModel::getAddress(int row) const -{ - return m_contacts.value(row).address; + m_contacts.replace(row, { fullName, address, city, number }); + dataChanged(index(row, 0), index(row, 0), { FullNameRole, AddressRole, CityRole, NumberRole }); } -QString ContactModel::getCity(int row) const +void ContactModel::remove(int row) { - return m_contacts.value(row).city; -} + if (row < 0 || row >= m_contacts.count()) + return; -QString ContactModel::getNumber(int row) const -{ - return m_contacts.value(row).number; + beginRemoveRows(QModelIndex(), row, row); + m_contacts.removeAt(row); + endRemoveRows(); } diff --git a/examples/quickcontrols2/contactlist/contactmodel.h b/examples/quickcontrols2/contactlist/contactmodel.h index 2776905f..d09602c4 100644 --- a/examples/quickcontrols2/contactlist/contactmodel.h +++ b/examples/quickcontrols2/contactlist/contactmodel.h @@ -72,13 +72,10 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash roleNames() const; - Q_INVOKABLE QString getFullName(int row) const; - Q_INVOKABLE QString getAddress(int row) const; - Q_INVOKABLE QString getCity(int row) const; - Q_INVOKABLE QString getNumber(int row) const; - - Q_INVOKABLE void updateContact(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); - Q_INVOKABLE void removeContact(int row); + Q_INVOKABLE QVariantMap get(int row) const; + Q_INVOKABLE void append(const QString &fullName, const QString &address, const QString &city, const QString &number); + Q_INVOKABLE void set(int row, const QString &fullName, const QString &address, const QString &city, const QString &number); + Q_INVOKABLE void remove(int row); private: struct Contact { -- cgit v1.2.3 From f915d0529e801021f049fef9bcd2e4eb7f0f39f2 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sun, 20 Nov 2016 07:06:23 +0100 Subject: qtquickcontrols2-contactlist.qdoc: remove stray image-tag Change-Id: I7c88bfbe8e7d3738302d859e4dafc2d0b5fdfee3 Reviewed-by: J-P Nurmi --- .../contactlist/doc/src/qtquickcontrols2-contactlist.qdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc index a6a25b49..c563b159 100644 --- a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc +++ b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc @@ -38,7 +38,7 @@ \section1 C++ Backend - \borderedimage image qtquickcontrols2-contactlist.png + \borderedimage qtquickcontrols2-contactlist.png The contact list application allows the user to add, edit, and remove contacts. The actual implementation is done in C++ and exposed as a QAbstractListModel. -- cgit v1.2.3 From de4ff95cd166b13fb4818ada5493d913345ef0ef Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sun, 20 Nov 2016 07:43:13 +0100 Subject: Whitespace fixes Change-Id: I4eab43944ec03b79d713db1c6756e76cbfa38b30 Reviewed-by: J-P Nurmi --- .../contactlist/doc/src/qtquickcontrols2-contactlist.qdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc index c563b159..2b4de68b 100644 --- a/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc +++ b/examples/quickcontrols2/contactlist/doc/src/qtquickcontrols2-contactlist.qdoc @@ -60,7 +60,7 @@ in Qt Quick Designer. For this to work we add the following line to \e contactlist.pro. \code - QML_DESIGNER_IMPORT_PATH=$$PWD/designer + QML_DESIGNER_IMPORT_PATH = $$PWD/designer \endcode Because Qt Quick Designer cannot instantiate the ContactModel C++ class, we define -- cgit v1.2.3