diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-06-26 15:18:30 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-06-30 08:51:06 +0200 |
commit | 72a153a7f573008f071986030e73c7be6765bc88 (patch) | |
tree | 9b924c9d41dd5d520762ff3ca8967ad1e5156a22 /examples/widgets | |
parent | 8937169c190246ebc85df242f85b3bda911bc5c6 (diff) |
Move simple widget mapper example to manual test
Pick-to: 6.5 6.6
Change-Id: I703843b5ee935794c2e2fd0407f9a1508b088ab6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'examples/widgets')
9 files changed, 2 insertions, 314 deletions
diff --git a/examples/widgets/doc/src/combowidgetmapper.qdoc b/examples/widgets/doc/src/combowidgetmapper.qdoc index 2f53bfd11b..87a1879232 100644 --- a/examples/widgets/doc/src/combowidgetmapper.qdoc +++ b/examples/widgets/doc/src/combowidgetmapper.qdoc @@ -10,14 +10,7 @@ \image combowidgetmapper-example.png - In the \l{Simple Widget Mapper Example}, we showed the basic use of a - widget mapper to relate data exposed by a model to simple input widgets - in a user interface. However, sometimes we want to use input widgets that - expose data as choices to the user, such as QComboBox, and we need a way - to relate their input to the values stored in the model. - - This example is very similar to the \l{Simple Widget Mapper Example}. - Again, we create a \c Window class with an almost identical user interface, + We create a \c Window class with an almost identical user interface, except that, instead of providing a spin box so that each person's age can be entered, we provide a combo box to allow their addresses to be classified as "Home", "Work" or "Other". @@ -65,8 +58,7 @@ interfering with the other input widgets. The implementation is shown later. \endomit - The rest of the constructor is very similar to that of the - \l{Simple Widget Mapper Example}: + The rest of the constructor sets up connections and layouts: \snippet itemviews/combowidgetmapper/window.cpp Set up connections and layouts diff --git a/examples/widgets/doc/src/simplewidgetmapper.qdoc b/examples/widgets/doc/src/simplewidgetmapper.qdoc deleted file mode 100644 index 508f9d201f..0000000000 --- a/examples/widgets/doc/src/simplewidgetmapper.qdoc +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example itemviews/simplewidgetmapper - \title Simple Widget Mapper Example - \ingroup examples-itemviews - \brief The Simple Widget Mapper example shows how to use a widget mapper to display - data from a model in a collection of widgets. - - \image simplewidgetmapper-example.png - - The QDataWidgetMapper class allows information obtained from a - \l{Model Classes}{model} to be viewed and edited in a collection of - widgets instead of in an \l{View Classes}{item view}. - Any model derived from QAbstractItemModel can be used as the source of - data and almost any input widget can be used to display it. - - The example itself is very simple: we create \c Window, a QWidget subclass - that we use to hold the widgets used to present the data, and show it. The - \c Window class will provide buttons that the user can click to show - different records from the model. - - \section1 Window Class Definition - - The class provides a constructor, a slot to keep the buttons up to date, - and a private function to set up the model: - - \snippet itemviews/simplewidgetmapper/window.h Window definition - - In addition to the QDataWidgetMapper object and the controls used to make - up the user interface, we use a QStandardItemModel to hold our data. - We could use a custom model, but this standard implementation is sufficient - for our purposes. - - \section1 Window Class Implementation - - The constructor of the \c Window class can be explained in three parts. - In the first part, we set up the widgets used for the user interface: - - \snippet itemviews/simplewidgetmapper/window.cpp Set up widgets - - We also set up the buddy relationships between various labels and the - corresponding input widgets. - - Next, we set up the widget mapper, relating each input widget to a column - in the model specified by the call to \l{QDataWidgetMapper::}{setModel()}: - - \snippet itemviews/simplewidgetmapper/window.cpp Set up the mapper - - We also connect the mapper to the \uicontrol{Next} and \uicontrol{Previous} buttons - via its \l{QDataWidgetMapper::}{toNext()} and - \l{QDataWidgetMapper::}{toPrevious()} slots. The mapper's - \l{QDataWidgetMapper::}{currentIndexChanged()} signal is connected to the - \c{updateButtons()} slot in the window which we'll show later. - - In the final part of the constructor, we set up the layout, placing each - of the widgets in a grid (we could also use a QFormLayout for this): - - \snippet itemviews/simplewidgetmapper/window.cpp Set up the layout - - Lastly, we set the window title and initialize the mapper by setting it to - refer to the first row in the model. - - The model is initialized in the window's \c{setupModel()} function. Here, - we create a standard model with 5 rows and 3 columns, and we insert some - sample names, addresses and ages into each row: - - \snippet itemviews/simplewidgetmapper/window.cpp Set up the model - - As a result, each row can be treated like a record in a database, and the - widget mapper will read the data from each row, using the column numbers - specified earlier to access the correct data for each widget. This is - shown in the following diagram: - - \image widgetmapper-simple-mapping.png - - Since the user can navigate using the buttons in the user interface, the - example is fully-functional at this point, but to make it a bit more - user-friendly, we implement the \c{updateButtons()} slot to show when the - user is viewing the first or last records: - - \snippet itemviews/simplewidgetmapper/window.cpp Slot for updating the buttons - - If the mapper is referring to the first row in the model, the \uicontrol{Previous} - button is disabled. Similarly, the \uicontrol{Next} button is disabled if the - mapper reaches the last row in the model. - - \section1 More Complex Mappings - - The QDataWidgetMapper class makes it easy to relate information from a - model to widgets in a user interface. However, it is sometimes necessary - to use input widgets which offer choices to the user, such as QComboBox, - in conjunction with a widget mapper. - - In these situations, although the mapping to input widgets remains simple, - more work needs to be done to expose additional data to the widget mapper. - This is covered by the \l{Combo Widget Mapper Example}{Combo Widget Mapper} - and \l{SQL Widget Mapper Example}{SQL Widget Mapper} - examples. -*/ diff --git a/examples/widgets/itemviews/CMakeLists.txt b/examples/widgets/itemviews/CMakeLists.txt index 30c93de51b..9659dafa01 100644 --- a/examples/widgets/itemviews/CMakeLists.txt +++ b/examples/widgets/itemviews/CMakeLists.txt @@ -9,7 +9,6 @@ qt_internal_add_example(customsortfiltermodel) qt_internal_add_example(editabletreemodel) qt_internal_add_example(fetchmore) qt_internal_add_example(frozencolumn) -qt_internal_add_example(simplewidgetmapper) qt_internal_add_example(spinboxdelegate) qt_internal_add_example(spreadsheet) qt_internal_add_example(stardelegate) diff --git a/examples/widgets/itemviews/itemviews.pro b/examples/widgets/itemviews/itemviews.pro index eee0f8eb18..15ad262554 100644 --- a/examples/widgets/itemviews/itemviews.pro +++ b/examples/widgets/itemviews/itemviews.pro @@ -8,7 +8,6 @@ SUBDIRS = addressbook \ fetchmore \ frozencolumn \ simpletreemodel \ - simplewidgetmapper \ spinboxdelegate \ spreadsheet \ stardelegate diff --git a/examples/widgets/itemviews/simplewidgetmapper/CMakeLists.txt b/examples/widgets/itemviews/simplewidgetmapper/CMakeLists.txt deleted file mode 100644 index ced0741e44..0000000000 --- a/examples/widgets/itemviews/simplewidgetmapper/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(simplewidgetmapper LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/simplewidgetmapper") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) - -qt_standard_project_setup() - -qt_add_executable(simplewidgetmapper - main.cpp - window.cpp window.h -) - -set_target_properties(simplewidgetmapper PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(simplewidgetmapper PRIVATE - Qt6::Core - Qt6::Gui - Qt6::Widgets -) - -install(TARGETS simplewidgetmapper - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/itemviews/simplewidgetmapper/main.cpp b/examples/widgets/itemviews/simplewidgetmapper/main.cpp deleted file mode 100644 index 2709c948f9..0000000000 --- a/examples/widgets/itemviews/simplewidgetmapper/main.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "window.h" - -#include <QApplication> - -int main(int argc, char **argv) -{ - QApplication app(argc, argv); - Window window; - window.show(); - return app.exec(); -} diff --git a/examples/widgets/itemviews/simplewidgetmapper/simplewidgetmapper.pro b/examples/widgets/itemviews/simplewidgetmapper/simplewidgetmapper.pro deleted file mode 100644 index f86a16bd3f..0000000000 --- a/examples/widgets/itemviews/simplewidgetmapper/simplewidgetmapper.pro +++ /dev/null @@ -1,10 +0,0 @@ -QT += widgets -requires(qtConfig(datawidgetmapper)) - -HEADERS = window.h -SOURCES = main.cpp \ - window.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/simplewidgetmapper -INSTALLS += target diff --git a/examples/widgets/itemviews/simplewidgetmapper/window.cpp b/examples/widgets/itemviews/simplewidgetmapper/window.cpp deleted file mode 100644 index f7ef05dbd5..0000000000 --- a/examples/widgets/itemviews/simplewidgetmapper/window.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "window.h" - -#include <QtWidgets> - -//! [Set up widgets] -Window::Window(QWidget *parent) - : QWidget(parent) -{ - setupModel(); - - nameLabel = new QLabel(tr("Na&me:")); - nameEdit = new QLineEdit(); - addressLabel = new QLabel(tr("&Address:")); - addressEdit = new QTextEdit(); - ageLabel = new QLabel(tr("A&ge (in years):")); - ageSpinBox = new QSpinBox(); - nextButton = new QPushButton(tr("&Next")); - previousButton = new QPushButton(tr("&Previous")); - - nameLabel->setBuddy(nameEdit); - addressLabel->setBuddy(addressEdit); - ageLabel->setBuddy(ageSpinBox); -//! [Set up widgets] - -//! [Set up the mapper] - mapper = new QDataWidgetMapper(this); - mapper->setModel(model); - mapper->addMapping(nameEdit, 0); - mapper->addMapping(addressEdit, 1); - mapper->addMapping(ageSpinBox, 2); - - connect(previousButton, &QAbstractButton::clicked, mapper, &QDataWidgetMapper::toPrevious); - connect(nextButton, &QAbstractButton::clicked, mapper, &QDataWidgetMapper::toNext); - connect(mapper, &QDataWidgetMapper::currentIndexChanged, this, &Window::updateButtons); -//! [Set up the mapper] - -//! [Set up the layout] - QGridLayout *layout = new QGridLayout(); - layout->addWidget(nameLabel, 0, 0, 1, 1); - layout->addWidget(nameEdit, 0, 1, 1, 1); - layout->addWidget(previousButton, 0, 2, 1, 1); - layout->addWidget(addressLabel, 1, 0, 1, 1); - layout->addWidget(addressEdit, 1, 1, 2, 1); - layout->addWidget(nextButton, 1, 2, 1, 1); - layout->addWidget(ageLabel, 3, 0, 1, 1); - layout->addWidget(ageSpinBox, 3, 1, 1, 1); - setLayout(layout); - - setWindowTitle(tr("Simple Widget Mapper")); - mapper->toFirst(); -} -//! [Set up the layout] - -//! [Set up the model] -void Window::setupModel() -{ - model = new QStandardItemModel(5, 3, this); - - QStringList names; - names << "Alice" << "Bob" << "Carol" << "Donald" << "Emma"; - - QStringList addresses; - addresses << "<qt>123 Main Street<br/>Market Town</qt>" - << "<qt>PO Box 32<br/>Mail Handling Service" - "<br/>Service City</qt>" - << "<qt>The Lighthouse<br/>Remote Island</qt>" - << "<qt>47338 Park Avenue<br/>Big City</qt>" - << "<qt>Research Station<br/>Base Camp<br/>Big Mountain</qt>"; - - QStringList ages; - ages << "20" << "31" << "32" << "19" << "26"; - - for (int row = 0; row < 5; ++row) { - QStandardItem *item = new QStandardItem(names[row]); - model->setItem(row, 0, item); - item = new QStandardItem(addresses[row]); - model->setItem(row, 1, item); - item = new QStandardItem(ages[row]); - model->setItem(row, 2, item); - } -} -//! [Set up the model] - -//! [Slot for updating the buttons] -void Window::updateButtons(int row) -{ - previousButton->setEnabled(row > 0); - nextButton->setEnabled(row < model->rowCount() - 1); -} -//! [Slot for updating the buttons] diff --git a/examples/widgets/itemviews/simplewidgetmapper/window.h b/examples/widgets/itemviews/simplewidgetmapper/window.h deleted file mode 100644 index 1502c00df1..0000000000 --- a/examples/widgets/itemviews/simplewidgetmapper/window.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef WINDOW_H -#define WINDOW_H - -#include <QWidget> - -QT_BEGIN_NAMESPACE -class QDataWidgetMapper; -class QLabel; -class QLineEdit; -class QPushButton; -class QSpinBox; -class QStandardItemModel; -class QTextEdit; -QT_END_NAMESPACE - -//! [Window definition] -class Window : public QWidget -{ - Q_OBJECT - -public: - Window(QWidget *parent = nullptr); - -private slots: - void updateButtons(int row); - -private: - void setupModel(); - - QLabel *nameLabel; - QLabel *addressLabel; - QLabel *ageLabel; - QLineEdit *nameEdit; - QTextEdit *addressEdit; - QSpinBox *ageSpinBox; - QPushButton *nextButton; - QPushButton *previousButton; - - QStandardItemModel *model; - QDataWidgetMapper *mapper; -}; -//! [Window definition] - -#endif // WINDOW_H |