1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | #include <QtSql> |
53 | |
54 | #include "window.h" |
55 | |
56 | //! [Set up widgets] |
57 | Window::Window(QWidget *parent) |
58 | : QWidget(parent) |
59 | { |
60 | setupModel(); |
61 | |
62 | nameLabel = new QLabel(tr(s: "Na&me:" )); |
63 | nameEdit = new QLineEdit(); |
64 | addressLabel = new QLabel(tr(s: "&Address:" )); |
65 | addressEdit = new QTextEdit(); |
66 | typeLabel = new QLabel(tr(s: "&Type:" )); |
67 | typeComboBox = new QComboBox(); |
68 | nextButton = new QPushButton(tr(s: "&Next" )); |
69 | previousButton = new QPushButton(tr(s: "&Previous" )); |
70 | |
71 | nameLabel->setBuddy(nameEdit); |
72 | addressLabel->setBuddy(addressEdit); |
73 | typeLabel->setBuddy(typeComboBox); |
74 | //! [Set up widgets] |
75 | |
76 | //! [Set up the mapper] |
77 | QSqlTableModel *relModel = model->relationModel(column: typeIndex); |
78 | typeComboBox->setModel(relModel); |
79 | typeComboBox->setModelColumn(relModel->fieldIndex(fieldName: "description" )); |
80 | |
81 | mapper = new QDataWidgetMapper(this); |
82 | mapper->setModel(model); |
83 | mapper->setItemDelegate(new QSqlRelationalDelegate(this)); |
84 | mapper->addMapping(widget: nameEdit, section: model->fieldIndex(fieldName: "name" )); |
85 | mapper->addMapping(widget: addressEdit, section: model->fieldIndex(fieldName: "address" )); |
86 | mapper->addMapping(widget: typeComboBox, section: typeIndex); |
87 | //! [Set up the mapper] |
88 | |
89 | //! [Set up connections and layouts] |
90 | connect(sender: previousButton, signal: &QPushButton::clicked, |
91 | receiver: mapper, slot: &QDataWidgetMapper::toPrevious); |
92 | connect(sender: nextButton, signal: &QPushButton::clicked, |
93 | receiver: mapper, slot: &QDataWidgetMapper::toNext); |
94 | connect(sender: mapper, signal: &QDataWidgetMapper::currentIndexChanged, |
95 | receiver: this, slot: &Window::updateButtons); |
96 | |
97 | QGridLayout *layout = new QGridLayout(); |
98 | layout->addWidget(nameLabel, row: 0, column: 0, rowSpan: 1, columnSpan: 1); |
99 | layout->addWidget(nameEdit, row: 0, column: 1, rowSpan: 1, columnSpan: 1); |
100 | layout->addWidget(previousButton, row: 0, column: 2, rowSpan: 1, columnSpan: 1); |
101 | layout->addWidget(addressLabel, row: 1, column: 0, rowSpan: 1, columnSpan: 1); |
102 | layout->addWidget(addressEdit, row: 1, column: 1, rowSpan: 2, columnSpan: 1); |
103 | layout->addWidget(nextButton, row: 1, column: 2, rowSpan: 1, columnSpan: 1); |
104 | layout->addWidget(typeLabel, row: 3, column: 0, rowSpan: 1, columnSpan: 1); |
105 | layout->addWidget(typeComboBox, row: 3, column: 1, rowSpan: 1, columnSpan: 1); |
106 | setLayout(layout); |
107 | |
108 | setWindowTitle(tr(s: "SQL Widget Mapper" )); |
109 | mapper->toFirst(); |
110 | } |
111 | //! [Set up connections and layouts] |
112 | |
113 | //! [Set up the main table] |
114 | void Window::setupModel() |
115 | { |
116 | QSqlDatabase db = QSqlDatabase::addDatabase(type: "QSQLITE" ); |
117 | db.setDatabaseName(":memory:" ); |
118 | if (!db.open()) { |
119 | QMessageBox::critical(parent: 0, title: tr(s: "Cannot open database" ), |
120 | text: tr(s: "Unable to establish a database connection.\n" |
121 | "This example needs SQLite support. Please read " |
122 | "the Qt SQL driver documentation for information how " |
123 | "to build it." ), buttons: QMessageBox::Cancel); |
124 | return; |
125 | } |
126 | |
127 | QSqlQuery query; |
128 | query.exec(query: "create table person (id int primary key, " |
129 | "name varchar(20), address varchar(200), typeid int)" ); |
130 | query.exec(query: "insert into person values(1, 'Alice', " |
131 | "'<qt>123 Main Street<br/>Market Town</qt>', 101)" ); |
132 | query.exec(query: "insert into person values(2, 'Bob', " |
133 | "'<qt>PO Box 32<br/>Mail Handling Service" |
134 | "<br/>Service City</qt>', 102)" ); |
135 | query.exec(query: "insert into person values(3, 'Carol', " |
136 | "'<qt>The Lighthouse<br/>Remote Island</qt>', 103)" ); |
137 | query.exec(query: "insert into person values(4, 'Donald', " |
138 | "'<qt>47338 Park Avenue<br/>Big City</qt>', 101)" ); |
139 | query.exec(query: "insert into person values(5, 'Emma', " |
140 | "'<qt>Research Station<br/>Base Camp<br/>" |
141 | "Big Mountain</qt>', 103)" ); |
142 | //! [Set up the main table] |
143 | |
144 | //! [Set up the address type table] |
145 | query.exec(query: "create table addresstype (id int, description varchar(20))" ); |
146 | query.exec(query: "insert into addresstype values(101, 'Home')" ); |
147 | query.exec(query: "insert into addresstype values(102, 'Work')" ); |
148 | query.exec(query: "insert into addresstype values(103, 'Other')" ); |
149 | |
150 | model = new QSqlRelationalTableModel(this); |
151 | model->setTable("person" ); |
152 | model->setEditStrategy(QSqlTableModel::OnManualSubmit); |
153 | |
154 | typeIndex = model->fieldIndex(fieldName: "typeid" ); |
155 | |
156 | model->setRelation(column: typeIndex, |
157 | relation: QSqlRelation("addresstype" , "id" , "description" )); |
158 | model->select(); |
159 | } |
160 | //! [Set up the address type table] |
161 | |
162 | //! [Slot for updating the buttons] |
163 | void Window::updateButtons(int row) |
164 | { |
165 | previousButton->setEnabled(row > 0); |
166 | nextButton->setEnabled(row < model->rowCount() - 1); |
167 | } |
168 | //! [Slot for updating the buttons] |
169 | |