A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQSqlRelationalTableModel</id>
		<title>C++/Qt/QSqlRelationalTableModel - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FQt%2FQSqlRelationalTableModel"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QSqlRelationalTableModel&amp;action=history"/>
		<updated>2026-04-18T18:29:30Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QSqlRelationalTableModel&amp;diff=1122&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QSqlRelationalTableModel&amp;diff=1122&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QSqlRelationalTableModel&amp;diff=1123&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/Qt/QSqlRelationalTableModel&amp;diff=1123&amp;oldid=prev"/>
				<updated>2010-05-25T10:24:51Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Data mapper==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#ifndef WINDOW_H&lt;br /&gt;
#define WINDOW_H&lt;br /&gt;
#include &amp;lt;QWidget&amp;gt;&lt;br /&gt;
QT_BEGIN_NAMESPACE&lt;br /&gt;
class QComboBox;&lt;br /&gt;
class QDataWidgetMapper;&lt;br /&gt;
class QItemSelectionModel;&lt;br /&gt;
class QLabel;&lt;br /&gt;
class QLineEdit;&lt;br /&gt;
class QPushButton;&lt;br /&gt;
class QSqlRelationalTableModel;&lt;br /&gt;
class QStandardItemModel;&lt;br /&gt;
class QStringListModel;&lt;br /&gt;
class QTextEdit;&lt;br /&gt;
QT_END_NAMESPACE&lt;br /&gt;
//! [Window definition]&lt;br /&gt;
class Window : public QWidget&lt;br /&gt;
{&lt;br /&gt;
    Q_OBJECT&lt;br /&gt;
public:&lt;br /&gt;
    Window(QWidget *parent = 0);&lt;br /&gt;
private slots:&lt;br /&gt;
    void updateButtons(int row);&lt;br /&gt;
private:&lt;br /&gt;
    void setupModel();&lt;br /&gt;
    QLabel *nameLabel;&lt;br /&gt;
    QLabel *addressLabel;&lt;br /&gt;
    QLabel *typeLabel;&lt;br /&gt;
    QLineEdit *nameEdit;&lt;br /&gt;
    QTextEdit *addressEdit;&lt;br /&gt;
    QComboBox *typeComboBox;&lt;br /&gt;
    QPushButton *nextButton;&lt;br /&gt;
    QPushButton *previousButton;&lt;br /&gt;
    QSqlRelationalTableModel *model;&lt;br /&gt;
    QItemSelectionModel *selectionModel;&lt;br /&gt;
    QDataWidgetMapper *mapper;&lt;br /&gt;
    int typeIndex;&lt;br /&gt;
};&lt;br /&gt;
//! [Window definition]&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QtGui&amp;gt;&lt;br /&gt;
#include &amp;lt;QtSql&amp;gt;&lt;br /&gt;
#include &amp;quot;window.h&amp;quot;&lt;br /&gt;
//! [Set up widgets]&lt;br /&gt;
Window::Window(QWidget *parent)&lt;br /&gt;
    : QWidget(parent)&lt;br /&gt;
{&lt;br /&gt;
    setupModel();&lt;br /&gt;
    nameLabel = new QLabel(tr(&amp;quot;Na&amp;amp;me:&amp;quot;));&lt;br /&gt;
    nameEdit = new QLineEdit();&lt;br /&gt;
    addressLabel = new QLabel(tr(&amp;quot;&amp;amp;Address:&amp;quot;));&lt;br /&gt;
    addressEdit = new QTextEdit();&lt;br /&gt;
    typeLabel = new QLabel(tr(&amp;quot;&amp;amp;Type:&amp;quot;));&lt;br /&gt;
    typeComboBox = new QComboBox();&lt;br /&gt;
    nextButton = new QPushButton(tr(&amp;quot;&amp;amp;Next&amp;quot;));&lt;br /&gt;
    previousButton = new QPushButton(tr(&amp;quot;&amp;amp;Previous&amp;quot;));&lt;br /&gt;
    nameLabel-&amp;gt;setBuddy(nameEdit);&lt;br /&gt;
    addressLabel-&amp;gt;setBuddy(addressEdit);&lt;br /&gt;
    typeLabel-&amp;gt;setBuddy(typeComboBox);&lt;br /&gt;
//! [Set up widgets]&lt;br /&gt;
//! [Set up the mapper]&lt;br /&gt;
    QSqlTableModel *relModel = model-&amp;gt;relationModel(typeIndex);&lt;br /&gt;
    typeComboBox-&amp;gt;setModel(relModel);&lt;br /&gt;
    typeComboBox-&amp;gt;setModelColumn(relModel-&amp;gt;fieldIndex(&amp;quot;description&amp;quot;));&lt;br /&gt;
    mapper = new QDataWidgetMapper(this);&lt;br /&gt;
    mapper-&amp;gt;setModel(model);&lt;br /&gt;
    mapper-&amp;gt;setItemDelegate(new QSqlRelationalDelegate(this));&lt;br /&gt;
    mapper-&amp;gt;addMapping(nameEdit, model-&amp;gt;fieldIndex(&amp;quot;name&amp;quot;));&lt;br /&gt;
    mapper-&amp;gt;addMapping(addressEdit, model-&amp;gt;fieldIndex(&amp;quot;address&amp;quot;));&lt;br /&gt;
    mapper-&amp;gt;addMapping(typeComboBox, typeIndex);&lt;br /&gt;
//! [Set up the mapper]&lt;br /&gt;
//! [Set up connections and layouts]&lt;br /&gt;
    connect(previousButton, SIGNAL(clicked()),&lt;br /&gt;
            mapper, SLOT(toPrevious()));&lt;br /&gt;
    connect(nextButton, SIGNAL(clicked()),&lt;br /&gt;
            mapper, SLOT(toNext()));&lt;br /&gt;
    connect(mapper, SIGNAL(currentIndexChanged(int)),&lt;br /&gt;
            this, SLOT(updateButtons(int)));&lt;br /&gt;
    QGridLayout *layout = new QGridLayout();&lt;br /&gt;
    layout-&amp;gt;addWidget(nameLabel, 0, 0, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(nameEdit, 0, 1, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(previousButton, 0, 2, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(addressLabel, 1, 0, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(addressEdit, 1, 1, 2, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(nextButton, 1, 2, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(typeLabel, 3, 0, 1, 1);&lt;br /&gt;
    layout-&amp;gt;addWidget(typeComboBox, 3, 1, 1, 1);&lt;br /&gt;
    setLayout(layout);&lt;br /&gt;
    setWindowTitle(tr(&amp;quot;SQL Widget Mapper&amp;quot;));&lt;br /&gt;
    mapper-&amp;gt;toFirst();&lt;br /&gt;
}&lt;br /&gt;
//! [Set up connections and layouts]&lt;br /&gt;
//! [Set up the main table]&lt;br /&gt;
void Window::setupModel()&lt;br /&gt;
{&lt;br /&gt;
    QSqlDatabase db = QSqlDatabase::addDatabase(&amp;quot;QSQLITE&amp;quot;);&lt;br /&gt;
    db.setDatabaseName(&amp;quot;:memory:&amp;quot;);&lt;br /&gt;
    if (!db.open()) {&lt;br /&gt;
        QMessageBox::critical(0, tr(&amp;quot;Cannot open database&amp;quot;),&lt;br /&gt;
            tr(&amp;quot;Unable to establish a database connection.\n&amp;quot;&lt;br /&gt;
               &amp;quot;This example needs SQLite support. Please read &amp;quot;&lt;br /&gt;
               &amp;quot;the Qt SQL driver documentation for information how &amp;quot;&lt;br /&gt;
               &amp;quot;to build it.&amp;quot;), QMessageBox::Cancel);&lt;br /&gt;
        return;&lt;br /&gt;
    }&lt;br /&gt;
    QSqlQuery query;&lt;br /&gt;
    query.exec(&amp;quot;create table person (id int primary key, &amp;quot;&lt;br /&gt;
               &amp;quot;name varchar(20), address varchar(200), typeid int)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(1, &amp;quot;Alice&amp;quot;, &amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;&amp;lt;qt&amp;gt;123 Main Street&amp;lt;br/&amp;gt;Market Town&amp;lt;/qt&amp;gt;&amp;quot;, 101)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(2, &amp;quot;Bob&amp;quot;, &amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;&amp;lt;qt&amp;gt;PO Box 32&amp;lt;br/&amp;gt;Mail Handling Service&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;lt;br/&amp;gt;Service City&amp;lt;/qt&amp;gt;&amp;quot;, 102)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(3, &amp;quot;Carol&amp;quot;, &amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;&amp;lt;qt&amp;gt;The Lighthouse&amp;lt;br/&amp;gt;Remote Island&amp;lt;/qt&amp;gt;&amp;quot;, 103)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(4, &amp;quot;Donald&amp;quot;, &amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;&amp;lt;qt&amp;gt;47338 Park Avenue&amp;lt;br/&amp;gt;Big City&amp;lt;/qt&amp;gt;&amp;quot;, 101)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(5, &amp;quot;Emma&amp;quot;, &amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;&amp;lt;qt&amp;gt;Research Station&amp;lt;br/&amp;gt;Base Camp&amp;lt;br/&amp;gt;&amp;quot;&lt;br /&gt;
               &amp;quot;Big Mountain&amp;lt;/qt&amp;gt;&amp;quot;, 103)&amp;quot;);&lt;br /&gt;
//! [Set up the main table]&lt;br /&gt;
//! [Set up the address type table]&lt;br /&gt;
    query.exec(&amp;quot;create table addresstype (id int, description varchar(20))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into addresstype values(101, &amp;quot;Home&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into addresstype values(102, &amp;quot;Work&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into addresstype values(103, &amp;quot;Other&amp;quot;)&amp;quot;);&lt;br /&gt;
    model = new QSqlRelationalTableModel(this);&lt;br /&gt;
    model-&amp;gt;setTable(&amp;quot;person&amp;quot;);&lt;br /&gt;
    model-&amp;gt;setEditStrategy(QSqlTableModel::OnManualSubmit);&lt;br /&gt;
    typeIndex = model-&amp;gt;fieldIndex(&amp;quot;typeid&amp;quot;);&lt;br /&gt;
    model-&amp;gt;setRelation(typeIndex,&lt;br /&gt;
           QSqlRelation(&amp;quot;addresstype&amp;quot;, &amp;quot;id&amp;quot;, &amp;quot;description&amp;quot;));&lt;br /&gt;
    model-&amp;gt;select();&lt;br /&gt;
}&lt;br /&gt;
//! [Set up the address type table]&lt;br /&gt;
//! [Slot for updating the buttons]&lt;br /&gt;
void Window::updateButtons(int row)&lt;br /&gt;
{&lt;br /&gt;
    previousButton-&amp;gt;setEnabled(row &amp;gt; 0);&lt;br /&gt;
    nextButton-&amp;gt;setEnabled(row &amp;lt; model-&amp;gt;rowCount() - 1);&lt;br /&gt;
}&lt;br /&gt;
//! [Slot for updating the buttons]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QApplication&amp;gt;&lt;br /&gt;
#include &amp;quot;window.h&amp;quot;&lt;br /&gt;
int main(int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
    QApplication app(argc, argv);&lt;br /&gt;
    Window window;&lt;br /&gt;
    window.show();&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Using QSqlRelationalTableModel==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
/****************************************************************************&lt;br /&gt;
**&lt;br /&gt;
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).&lt;br /&gt;
** All rights reserved.&lt;br /&gt;
** Contact: Nokia Corporation (qt-info@nokia.com)&lt;br /&gt;
**&lt;br /&gt;
** This file is part of the examples of the Qt Toolkit.&lt;br /&gt;
**&lt;br /&gt;
** $QT_BEGIN_LICENSE:LGPL$&lt;br /&gt;
** Commercial Usage&lt;br /&gt;
** Licensees holding valid Qt Commercial licenses may use this file in&lt;br /&gt;
** accordance with the Qt Commercial License Agreement provided with the&lt;br /&gt;
** Software or, alternatively, in accordance with the terms contained in&lt;br /&gt;
** a written agreement between you and Nokia.&lt;br /&gt;
**&lt;br /&gt;
** GNU Lesser General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU Lesser&lt;br /&gt;
** General Public License version 2.1 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.LGPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU Lesser General Public License version 2.1 requirements&lt;br /&gt;
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.&lt;br /&gt;
**&lt;br /&gt;
** In addition, as a special exception, Nokia gives you certain additional&lt;br /&gt;
** rights.  These rights are described in the Nokia Qt LGPL Exception&lt;br /&gt;
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.&lt;br /&gt;
**&lt;br /&gt;
** GNU General Public License Usage&lt;br /&gt;
** Alternatively, this file may be used under the terms of the GNU&lt;br /&gt;
** General Public License version 3.0 as published by the Free Software&lt;br /&gt;
** Foundation and appearing in the file LICENSE.GPL included in the&lt;br /&gt;
** packaging of this file.  Please review the following information to&lt;br /&gt;
** ensure the GNU General Public License version 3.0 requirements will be&lt;br /&gt;
** met: http://www.gnu.org/copyleft/gpl.html.&lt;br /&gt;
**&lt;br /&gt;
** If you have questions regarding the use of this file, please contact&lt;br /&gt;
** Nokia at qt-info@nokia.com.&lt;br /&gt;
** $QT_END_LICENSE$&lt;br /&gt;
**&lt;br /&gt;
****************************************************************************/&lt;br /&gt;
#ifndef CONNECTION_H&lt;br /&gt;
#define CONNECTION_H&lt;br /&gt;
#include &amp;lt;QMessageBox&amp;gt;&lt;br /&gt;
#include &amp;lt;QSqlDatabase&amp;gt;&lt;br /&gt;
#include &amp;lt;QSqlError&amp;gt;&lt;br /&gt;
#include &amp;lt;QSqlQuery&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
    This file defines a helper function to open a connection to an&lt;br /&gt;
    in-memory SQLITE database and to create a test table.&lt;br /&gt;
    If you want to use another database, simply modify the code&lt;br /&gt;
    below. All the examples in this directory use this function to&lt;br /&gt;
    connect to a database.&lt;br /&gt;
*/&lt;br /&gt;
static bool createConnection()&lt;br /&gt;
{&lt;br /&gt;
    QSqlDatabase db = QSqlDatabase::addDatabase(&amp;quot;QSQLITE&amp;quot;);&lt;br /&gt;
    db.setDatabaseName(&amp;quot;:memory:&amp;quot;);&lt;br /&gt;
    if (!db.open()) {&lt;br /&gt;
        QMessageBox::critical(0, qApp-&amp;gt;tr(&amp;quot;Cannot open database&amp;quot;),&lt;br /&gt;
            qApp-&amp;gt;tr(&amp;quot;Unable to establish a database connection.\n&amp;quot;&lt;br /&gt;
                     &amp;quot;This example needs SQLite support. Please read &amp;quot;&lt;br /&gt;
                     &amp;quot;the Qt SQL driver documentation for information how &amp;quot;&lt;br /&gt;
                     &amp;quot;to build it.\n\n&amp;quot;&lt;br /&gt;
                     &amp;quot;Click Cancel to exit.&amp;quot;), QMessageBox::Cancel);&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
    QSqlQuery query;&lt;br /&gt;
    query.exec(&amp;quot;create table person (id int primary key, &amp;quot;&lt;br /&gt;
               &amp;quot;firstname varchar(20), lastname varchar(20))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(101, &amp;quot;Danny&amp;quot;, &amp;quot;Young&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(102, &amp;quot;Christine&amp;quot;, &amp;quot;Holand&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(103, &amp;quot;Lars&amp;quot;, &amp;quot;Gordon&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(104, &amp;quot;Roberto&amp;quot;, &amp;quot;Robitaille&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into person values(105, &amp;quot;Maria&amp;quot;, &amp;quot;Papadopoulos&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;create table offices (id int primary key,&amp;quot;&lt;br /&gt;
                                             &amp;quot;imagefile int,&amp;quot;&lt;br /&gt;
                                             &amp;quot;location varchar(20),&amp;quot;&lt;br /&gt;
                                             &amp;quot;country varchar(20),&amp;quot;&lt;br /&gt;
                                             &amp;quot;description varchar(100))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(0, 0, &amp;quot;Oslo&amp;quot;, &amp;quot;Norway&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;Oslo is home to more than 500 000 citizens and has a &amp;quot;&lt;br /&gt;
               &amp;quot;lot to offer.It has been called \&amp;quot;The city with the big &amp;quot;&lt;br /&gt;
               &amp;quot;heart\&amp;quot; and this is a nickname we are happy to live up to.&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(1, 1, &amp;quot;Brisbane&amp;quot;, &amp;quot;Australia&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;Brisbane is the capital of Queensland, the Sunshine State, &amp;quot;&lt;br /&gt;
               &amp;quot;where it is beautiful one day, perfect the next.  &amp;quot;&lt;br /&gt;
               &amp;quot;Brisbane is Australia&amp;quot;&amp;quot;s 3rd largest city, being home &amp;quot;&lt;br /&gt;
               &amp;quot;to almost 2 million people.&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(2, 2, &amp;quot;Redwood City&amp;quot;, &amp;quot;US&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;You find Redwood City in the heart of the Bay Area &amp;quot;&lt;br /&gt;
               &amp;quot;just north of Silicon Valley. The largest nearby city is &amp;quot;&lt;br /&gt;
               &amp;quot;San Jose which is the third largest city in California &amp;quot;&lt;br /&gt;
               &amp;quot;and the 10th largest in the US.&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(3, 3, &amp;quot;Berlin&amp;quot;, &amp;quot;Germany&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;Berlin, the capital of Germany is dynamic, cosmopolitan &amp;quot;&lt;br /&gt;
               &amp;quot;and creative, allowing for every kind of lifestyle. &amp;quot;&lt;br /&gt;
               &amp;quot;East meets West in the metropolis at the heart of a &amp;quot;&lt;br /&gt;
               &amp;quot;changing Europe.&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(4, 4, &amp;quot;Munich&amp;quot;, &amp;quot;Germany&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;Several technology companies are represented in Munich, &amp;quot;&lt;br /&gt;
               &amp;quot;and the city is often called the \&amp;quot;Bavarian Silicon Valley\&amp;quot;. &amp;quot;&lt;br /&gt;
               &amp;quot;The exciting city is also filled with culture, &amp;quot;&lt;br /&gt;
               &amp;quot;art and music. &amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into offices &amp;quot;&lt;br /&gt;
               &amp;quot;values(5, 5, &amp;quot;Beijing&amp;quot;, &amp;quot;China&amp;quot;,&amp;quot;&lt;br /&gt;
               &amp;quot;&amp;quot;Beijing as a capital city has more than 3000 years of &amp;quot;&lt;br /&gt;
               &amp;quot;history. Today the city counts 12 million citizens, and &amp;quot;&lt;br /&gt;
               &amp;quot;is the political, economic and cultural centre of China.&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;create table images (locationid int, file varchar(20))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(0, &amp;quot;images/oslo.png&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(1, &amp;quot;images/brisbane.png&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(2, &amp;quot;images/redwood.png&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(3, &amp;quot;images/berlin.png&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(4, &amp;quot;images/munich.png&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into images values(5, &amp;quot;images/beijing.png&amp;quot;)&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;QtGui&amp;gt;&lt;br /&gt;
#include &amp;lt;QtSql&amp;gt;&lt;br /&gt;
#include &amp;quot;../connection.h&amp;quot;&lt;br /&gt;
void initializeModel(QSqlRelationalTableModel *model)&lt;br /&gt;
{&lt;br /&gt;
    model-&amp;gt;setTable(&amp;quot;employee&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    model-&amp;gt;setEditStrategy(QSqlTableModel::OnManualSubmit);&lt;br /&gt;
    model-&amp;gt;setRelation(2, QSqlRelation(&amp;quot;city&amp;quot;, &amp;quot;id&amp;quot;, &amp;quot;name&amp;quot;));&lt;br /&gt;
 &lt;br /&gt;
    model-&amp;gt;setRelation(3, QSqlRelation(&amp;quot;country&amp;quot;, &amp;quot;id&amp;quot;, &amp;quot;name&amp;quot;));&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
    model-&amp;gt;setHeaderData(0, Qt::Horizontal, QObject::tr(&amp;quot;ID&amp;quot;));&lt;br /&gt;
    model-&amp;gt;setHeaderData(1, Qt::Horizontal, QObject::tr(&amp;quot;Name&amp;quot;));&lt;br /&gt;
    model-&amp;gt;setHeaderData(2, Qt::Horizontal, QObject::tr(&amp;quot;City&amp;quot;));&lt;br /&gt;
    model-&amp;gt;setHeaderData(3, Qt::Horizontal, QObject::tr(&amp;quot;Country&amp;quot;));&lt;br /&gt;
&lt;br /&gt;
    model-&amp;gt;select();&lt;br /&gt;
}&lt;br /&gt;
QTableView *createView(const QString &amp;amp;title, QSqlTableModel *model)&lt;br /&gt;
{&lt;br /&gt;
    QTableView *view = new QTableView;&lt;br /&gt;
    view-&amp;gt;setModel(model);&lt;br /&gt;
    view-&amp;gt;setItemDelegate(new QSqlRelationalDelegate(view));&lt;br /&gt;
    view-&amp;gt;setWindowTitle(title);&lt;br /&gt;
    return view;&lt;br /&gt;
}&lt;br /&gt;
void createRelationalTables()&lt;br /&gt;
{&lt;br /&gt;
    QSqlQuery query;&lt;br /&gt;
    query.exec(&amp;quot;create table employee(id int primary key, name varchar(20), city int, country int)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into employee values(1, &amp;quot;Espen&amp;quot;, 5000, 47)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into employee values(2, &amp;quot;Harald&amp;quot;, 80000, 49)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into employee values(3, &amp;quot;Sam&amp;quot;, 100, 1)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;create table city(id int, name varchar(20))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into city values(100, &amp;quot;San Jose&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into city values(5000, &amp;quot;Oslo&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into city values(80000, &amp;quot;Munich&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;create table country(id int, name varchar(20))&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into country values(1, &amp;quot;USA&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into country values(47, &amp;quot;Norway&amp;quot;)&amp;quot;);&lt;br /&gt;
    query.exec(&amp;quot;insert into country values(49, &amp;quot;Germany&amp;quot;)&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    QApplication app(argc, argv);&lt;br /&gt;
    if (!createConnection())&lt;br /&gt;
        return 1;&lt;br /&gt;
    createRelationalTables();&lt;br /&gt;
    QSqlRelationalTableModel model;&lt;br /&gt;
    initializeModel(&amp;amp;model);&lt;br /&gt;
    QTableView *view = createView(QObject::tr(&amp;quot;Relational Table Model&amp;quot;), &amp;amp;model);&lt;br /&gt;
    view-&amp;gt;show();&lt;br /&gt;
    return app.exec();&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>