[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include <QtGui>
|
---|
| 43 | #include <QtSql>
|
---|
| 44 |
|
---|
| 45 | #include "window.h"
|
---|
| 46 |
|
---|
| 47 | //! [Set up widgets]
|
---|
| 48 | Window::Window(QWidget *parent)
|
---|
| 49 | : QWidget(parent)
|
---|
| 50 | {
|
---|
| 51 | setupModel();
|
---|
| 52 |
|
---|
| 53 | nameLabel = new QLabel(tr("Na&me:"));
|
---|
| 54 | nameEdit = new QLineEdit();
|
---|
| 55 | addressLabel = new QLabel(tr("&Address:"));
|
---|
| 56 | addressEdit = new QTextEdit();
|
---|
| 57 | typeLabel = new QLabel(tr("&Type:"));
|
---|
| 58 | typeComboBox = new QComboBox();
|
---|
| 59 | nextButton = new QPushButton(tr("&Next"));
|
---|
| 60 | previousButton = new QPushButton(tr("&Previous"));
|
---|
| 61 |
|
---|
| 62 | nameLabel->setBuddy(nameEdit);
|
---|
| 63 | addressLabel->setBuddy(addressEdit);
|
---|
| 64 | typeLabel->setBuddy(typeComboBox);
|
---|
| 65 | //! [Set up widgets]
|
---|
| 66 |
|
---|
| 67 | //! [Set up the mapper]
|
---|
| 68 | QSqlTableModel *relModel = model->relationModel(typeIndex);
|
---|
| 69 | typeComboBox->setModel(relModel);
|
---|
| 70 | typeComboBox->setModelColumn(relModel->fieldIndex("description"));
|
---|
| 71 |
|
---|
| 72 | mapper = new QDataWidgetMapper(this);
|
---|
| 73 | mapper->setModel(model);
|
---|
| 74 | mapper->setItemDelegate(new QSqlRelationalDelegate(this));
|
---|
| 75 | mapper->addMapping(nameEdit, model->fieldIndex("name"));
|
---|
| 76 | mapper->addMapping(addressEdit, model->fieldIndex("address"));
|
---|
| 77 | mapper->addMapping(typeComboBox, typeIndex);
|
---|
| 78 | //! [Set up the mapper]
|
---|
| 79 |
|
---|
| 80 | //! [Set up connections and layouts]
|
---|
| 81 | connect(previousButton, SIGNAL(clicked()),
|
---|
| 82 | mapper, SLOT(toPrevious()));
|
---|
| 83 | connect(nextButton, SIGNAL(clicked()),
|
---|
| 84 | mapper, SLOT(toNext()));
|
---|
| 85 | connect(mapper, SIGNAL(currentIndexChanged(int)),
|
---|
| 86 | this, SLOT(updateButtons(int)));
|
---|
| 87 |
|
---|
| 88 | QGridLayout *layout = new QGridLayout();
|
---|
| 89 | layout->addWidget(nameLabel, 0, 0, 1, 1);
|
---|
| 90 | layout->addWidget(nameEdit, 0, 1, 1, 1);
|
---|
| 91 | layout->addWidget(previousButton, 0, 2, 1, 1);
|
---|
| 92 | layout->addWidget(addressLabel, 1, 0, 1, 1);
|
---|
| 93 | layout->addWidget(addressEdit, 1, 1, 2, 1);
|
---|
| 94 | layout->addWidget(nextButton, 1, 2, 1, 1);
|
---|
| 95 | layout->addWidget(typeLabel, 3, 0, 1, 1);
|
---|
| 96 | layout->addWidget(typeComboBox, 3, 1, 1, 1);
|
---|
| 97 | setLayout(layout);
|
---|
| 98 |
|
---|
| 99 | setWindowTitle(tr("SQL Widget Mapper"));
|
---|
| 100 | mapper->toFirst();
|
---|
| 101 | }
|
---|
| 102 | //! [Set up connections and layouts]
|
---|
| 103 |
|
---|
| 104 | //! [Set up the main table]
|
---|
| 105 | void Window::setupModel()
|
---|
| 106 | {
|
---|
| 107 | QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
---|
| 108 | db.setDatabaseName(":memory:");
|
---|
| 109 | if (!db.open()) {
|
---|
| 110 | QMessageBox::critical(0, tr("Cannot open database"),
|
---|
| 111 | tr("Unable to establish a database connection.\n"
|
---|
| 112 | "This example needs SQLite support. Please read "
|
---|
| 113 | "the Qt SQL driver documentation for information how "
|
---|
| 114 | "to build it."), QMessageBox::Cancel);
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | QSqlQuery query;
|
---|
| 119 | query.exec("create table person (id int primary key, "
|
---|
| 120 | "name varchar(20), address varchar(200), typeid int)");
|
---|
| 121 | query.exec("insert into person values(1, 'Alice', "
|
---|
| 122 | "'<qt>123 Main Street<br/>Market Town</qt>', 101)");
|
---|
| 123 | query.exec("insert into person values(2, 'Bob', "
|
---|
| 124 | "'<qt>PO Box 32<br/>Mail Handling Service"
|
---|
| 125 | "<br/>Service City</qt>', 102)");
|
---|
| 126 | query.exec("insert into person values(3, 'Carol', "
|
---|
| 127 | "'<qt>The Lighthouse<br/>Remote Island</qt>', 103)");
|
---|
| 128 | query.exec("insert into person values(4, 'Donald', "
|
---|
| 129 | "'<qt>47338 Park Avenue<br/>Big City</qt>', 101)");
|
---|
| 130 | query.exec("insert into person values(5, 'Emma', "
|
---|
| 131 | "'<qt>Research Station<br/>Base Camp<br/>"
|
---|
| 132 | "Big Mountain</qt>', 103)");
|
---|
| 133 | //! [Set up the main table]
|
---|
| 134 |
|
---|
| 135 | //! [Set up the address type table]
|
---|
| 136 | query.exec("create table addresstype (id int, description varchar(20))");
|
---|
| 137 | query.exec("insert into addresstype values(101, 'Home')");
|
---|
| 138 | query.exec("insert into addresstype values(102, 'Work')");
|
---|
| 139 | query.exec("insert into addresstype values(103, 'Other')");
|
---|
| 140 |
|
---|
| 141 | model = new QSqlRelationalTableModel(this);
|
---|
| 142 | model->setTable("person");
|
---|
| 143 | model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
---|
| 144 |
|
---|
| 145 | typeIndex = model->fieldIndex("typeid");
|
---|
| 146 |
|
---|
| 147 | model->setRelation(typeIndex,
|
---|
| 148 | QSqlRelation("addresstype", "id", "description"));
|
---|
| 149 | model->select();
|
---|
| 150 | }
|
---|
| 151 | //! [Set up the address type table]
|
---|
| 152 |
|
---|
| 153 | //! [Slot for updating the buttons]
|
---|
| 154 | void Window::updateButtons(int row)
|
---|
| 155 | {
|
---|
| 156 | previousButton->setEnabled(row > 0);
|
---|
| 157 | nextButton->setEnabled(row < model->rowCount() - 1);
|
---|
| 158 | }
|
---|
| 159 | //! [Slot for updating the buttons]
|
---|