From 0ca32eb4c57ba43b64cfc74c0fbd1c63706952c2 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Tue, 12 Feb 2019 15:48:17 +0100 Subject: add "json" manual TableModel test This covers the use case of fetching and showing data from a web API. Change-Id: I9e9cd6a17f409383ba71bc32f0f9d63a45213784 Reviewed-by: Richard Moe Gustavsen --- tests/manual/tableview/tablemodel/json/JsonData.js | 186 +++++++++++++++++++++ tests/manual/tableview/tablemodel/json/json.pro | 12 ++ tests/manual/tableview/tablemodel/json/main.cpp | 52 ++++++ tests/manual/tableview/tablemodel/json/main.qml | 116 +++++++++++++ 4 files changed, 366 insertions(+) create mode 100644 tests/manual/tableview/tablemodel/json/JsonData.js create mode 100644 tests/manual/tableview/tablemodel/json/json.pro create mode 100644 tests/manual/tableview/tablemodel/json/main.cpp create mode 100644 tests/manual/tableview/tablemodel/json/main.qml diff --git a/tests/manual/tableview/tablemodel/json/JsonData.js b/tests/manual/tableview/tablemodel/json/JsonData.js new file mode 100644 index 0000000000..a37233c539 --- /dev/null +++ b/tests/manual/tableview/tablemodel/json/JsonData.js @@ -0,0 +1,186 @@ +let drivers = [ + { + "driverId":"fisichella", + "code":"FIS", + "url":"http://en.wikipedia.org/wiki/Giancarlo_Fisichella", + "givenName":"Giancarlo", + "familyName":"Fisichella", + "dateOfBirth":"1973-01-14", + "nationality":"Italian" + }, + { + "driverId":"barrichello", + "code":"BAR", + "url":"http://en.wikipedia.org/wiki/Rubens_Barrichello", + "givenName":"Rubens", + "familyName":"Barrichello", + "dateOfBirth":"1972-05-23", + "nationality":"Brazilian" + }, + { + "driverId":"alonso", + "permanentNumber":"14", + "code":"ALO", + "url":"http://en.wikipedia.org/wiki/Fernando_Alonso", + "givenName":"Fernando", + "familyName":"Alonso", + "dateOfBirth":"1981-07-29", + "nationality":"Spanish" + }, + { + "driverId":"coulthard", + "code":"COU", + "url":"http://en.wikipedia.org/wiki/David_Coulthard", + "givenName":"David", + "familyName":"Coulthard", + "dateOfBirth":"1971-03-27", + "nationality":"British" + }, + { + "driverId":"webber", + "code":"WEB", + "url":"http://en.wikipedia.org/wiki/Mark_Webber", + "givenName":"Mark", + "familyName":"Webber", + "dateOfBirth":"1976-08-27", + "nationality":"Australian" + }, + { + "driverId":"montoya", + "code":"MON", + "url":"http://en.wikipedia.org/wiki/Juan_Pablo_Montoya", + "givenName":"Juan", + "familyName":"Pablo Montoya", + "dateOfBirth":"1975-09-20", + "nationality":"Colombian" + }, + { + "driverId":"klien", + "code":"KLI", + "url":"http://en.wikipedia.org/wiki/Christian_Klien", + "givenName":"Christian", + "familyName":"Klien", + "dateOfBirth":"1983-02-07", + "nationality":"Austrian" + }, + { + "driverId":"raikkonen", + "permanentNumber":"7", + "code":"RAI", + "url":"http://en.wikipedia.org/wiki/Kimi_R%C3%A4ikk%C3%B6nen", + "givenName":"Kimi", + "familyName":"Räikkönen", + "dateOfBirth":"1979-10-17", + "nationality":"Finnish" + }, + { + "driverId":"trulli", + "code":"TRU", + "url":"http://en.wikipedia.org/wiki/Jarno_Trulli", + "givenName":"Jarno", + "familyName":"Trulli", + "dateOfBirth":"1974-07-13", + "nationality":"Italian" + }, + { + "driverId":"massa", + "permanentNumber":"19", + "code":"MAS", + "url":"http://en.wikipedia.org/wiki/Felipe_Massa", + "givenName":"Felipe", + "familyName":"Massa", + "dateOfBirth":"1981-04-25", + "nationality":"Brazilian" + }, + { + "driverId":"button", + "permanentNumber":"22", + "code":"BUT", + "url":"http://en.wikipedia.org/wiki/Jenson_Button", + "givenName":"Jenson", + "familyName":"Button", + "dateOfBirth":"1980-01-19", + "nationality":"British" + }, + { + "driverId":"ralf_schumacher", + "code":"SCH", + "url":"http://en.wikipedia.org/wiki/Ralf_Schumacher", + "givenName":"Ralf", + "familyName":"Schumacher", + "dateOfBirth":"1975-06-30", + "nationality":"German" + }, + { + "driverId":"villeneuve", + "code":"VIL", + "url":"http://en.wikipedia.org/wiki/Jacques_Villeneuve", + "givenName":"Jacques", + "familyName":"Villeneuve", + "dateOfBirth":"1971-04-09", + "nationality":"Canadian" + }, + { + "driverId":"sato", + "code":"SAT", + "url":"http://en.wikipedia.org/wiki/Takuma_Sato", + "givenName":"Takuma", + "familyName":"Sato", + "dateOfBirth":"1977-01-28", + "nationality":"Japanese" + }, + { + "driverId":"karthikeyan", + "code":"KAR", + "url":"http://en.wikipedia.org/wiki/Narain_Karthikeyan", + "givenName":"Narain", + "familyName":"Karthikeyan", + "dateOfBirth":"1977-01-14", + "nationality":"Indian" + }, + { + "driverId":"monteiro", + "code":"TMO", + "url":"http://en.wikipedia.org/wiki/Tiago_Monteiro", + "givenName":"Tiago", + "familyName":"Monteiro", + "dateOfBirth":"1976-07-24", + "nationality":"Portuguese" + }, + { + "driverId":"friesacher", + "code":"FRI", + "url":"http://en.wikipedia.org/wiki/Patrick_Friesacher", + "givenName":"Patrick", + "familyName":"Friesacher", + "dateOfBirth":"1980-09-26", + "nationality":"Austrian" + }, + { + "driverId":"michael_schumacher", + "code":"MSC", + "url":"http://en.wikipedia.org/wiki/Michael_Schumacher", + "givenName":"Michael", + "familyName":"Schumacher", + "dateOfBirth":"1969-01-03", + "nationality":"German" + }, + { + "driverId":"heidfeld", + "code":"HEI", + "url":"http://en.wikipedia.org/wiki/Nick_Heidfeld", + "givenName":"Nick", + "familyName":"Heidfeld", + "dateOfBirth":"1977-05-10", + "nationality":"German" + }, + { + "driverId":"albers", + "code":"ALB", + "url":"http://en.wikipedia.org/wiki/Christijan_Albers", + "givenName":"Christijan", + "familyName":"Albers", + "dateOfBirth":"1979-04-16", + "nationality":"Dutch" + } +] diff --git a/tests/manual/tableview/tablemodel/json/json.pro b/tests/manual/tableview/tablemodel/json/json.pro new file mode 100644 index 0000000000..2339e488aa --- /dev/null +++ b/tests/manual/tableview/tablemodel/json/json.pro @@ -0,0 +1,12 @@ +TEMPLATE = app +TARGET = json +QT += qml quick +SOURCES += main.cpp +RESOURCES += \ + main.qml \ + JsonData.js + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/tests/manual/tableview/tablemodel/json/main.cpp b/tests/manual/tableview/tablemodel/json/main.cpp new file mode 100644 index 0000000000..176b532d49 --- /dev/null +++ b/tests/manual/tableview/tablemodel/json/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $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$ +** +****************************************************************************/ + +#include +#include + +int main(int argc, char *argv[]) +{ + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + return app.exec(); +} diff --git a/tests/manual/tableview/tablemodel/json/main.qml b/tests/manual/tableview/tablemodel/json/main.qml new file mode 100644 index 0000000000..2a835459c0 --- /dev/null +++ b/tests/manual/tableview/tablemodel/json/main.qml @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQuick module of the Qt Toolkit. +** +** $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$ +** +****************************************************************************/ + +import QtQuick 2.12 +import QtQuick.Controls 2.5 +import QtQuick.Layouts 1.12 +import QtQuick.Window 2.12 +import Qt.labs.qmlmodels 1.0 + +import "JsonData.js" as CachedJsonData + +ApplicationWindow { + id: window + width: 800 + height: 300 + visible: true + + function requestJson() { + let doc = new XMLHttpRequest() + doc.onreadystatechange = function() { + if (doc.readyState === XMLHttpRequest.DONE) { + var root = JSON.parse(doc.responseText) + var race = root.MRData.RaceTable.Races[0] + var raceResults = race.Results + var drivers = [] + for (let i = 0; i < raceResults.length; ++i) { + drivers.push(raceResults[i].Driver) + } + tableView.model.rows = drivers + print(JSON.stringify(drivers)) + } + } + + doc.open("GET", "http://ergast.com/api/f1/2005/1/results.json") + doc.send() + } + + Component.onCompleted: requestJson() + // Same as the data we get from ergast. Use it while developing + // to avoid flooding the server with requests. +// Component.onCompleted: tableView.model.rows = CachedJsonData.drivers + + ColumnLayout { + anchors.fill: parent + + TableView { + id: tableView + boundsBehavior: Flickable.StopAtBounds + + ScrollBar.horizontal: ScrollBar { + policy: ScrollBar.AlwaysOn + } + ScrollBar.vertical: ScrollBar { + policy: ScrollBar.AlwaysOn + } + + Layout.minimumHeight: window.height / 2 + Layout.fillWidth: true + Layout.fillHeight: true + + model: TableModel { + TableModelColumn { display: "driverId" } + TableModelColumn { display: "code" } + TableModelColumn { display: "url" } + TableModelColumn { display: "givenName" } + TableModelColumn { display: "familyName" } + TableModelColumn { display: "dateOfBirth" } + TableModelColumn { display: "nationality" } + } + + delegate: TextField { + objectName: "tableViewTextFieldDelegate" + text: model.display + selectByMouse: true + implicitWidth: 140 + onAccepted: model.display = text + } + } + } +} -- cgit v1.2.3