From 67ad0519fd165acee4a4d2a94fa502e9e4847bd0 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 23 Mar 2009 10:34:13 +0100 Subject: Long live Qt! --- examples/sql/README | 40 ++ examples/sql/cachedtable/cachedtable.pro | 11 + examples/sql/cachedtable/main.cpp | 58 +++ examples/sql/cachedtable/tableeditor.cpp | 106 +++++ examples/sql/cachedtable/tableeditor.h | 73 ++++ examples/sql/connection.h | 136 +++++++ examples/sql/drilldown/drilldown.pro | 16 + examples/sql/drilldown/drilldown.qrc | 11 + examples/sql/drilldown/imageitem.cpp | 124 ++++++ examples/sql/drilldown/imageitem.h | 75 ++++ examples/sql/drilldown/images/beijing.png | Bin 0 -> 99093 bytes examples/sql/drilldown/images/berlin.png | Bin 0 -> 81944 bytes examples/sql/drilldown/images/brisbane.png | Bin 0 -> 57785 bytes examples/sql/drilldown/images/munich.png | Bin 0 -> 59769 bytes examples/sql/drilldown/images/oslo.png | Bin 0 -> 41781 bytes examples/sql/drilldown/images/redwood.png | Bin 0 -> 39050 bytes examples/sql/drilldown/informationwindow.cpp | 170 ++++++++ examples/sql/drilldown/informationwindow.h | 91 +++++ examples/sql/drilldown/logo.png | Bin 0 -> 13378 bytes examples/sql/drilldown/main.cpp | 59 +++ examples/sql/drilldown/view.cpp | 179 +++++++++ examples/sql/drilldown/view.h | 81 ++++ examples/sql/masterdetail/albumdetails.xml | 98 +++++ examples/sql/masterdetail/database.h | 97 +++++ examples/sql/masterdetail/dialog.cpp | 283 ++++++++++++++ examples/sql/masterdetail/dialog.h | 83 ++++ examples/sql/masterdetail/images/icon.png | Bin 0 -> 30095 bytes examples/sql/masterdetail/images/image.png | Bin 0 -> 166692 bytes examples/sql/masterdetail/main.cpp | 60 +++ examples/sql/masterdetail/mainwindow.cpp | 430 +++++++++++++++++++++ examples/sql/masterdetail/mainwindow.h | 104 +++++ examples/sql/masterdetail/masterdetail.pro | 16 + examples/sql/masterdetail/masterdetail.qrc | 6 + examples/sql/querymodel/customsqlmodel.cpp | 65 ++++ examples/sql/querymodel/customsqlmodel.h | 59 +++ examples/sql/querymodel/editablesqlmodel.cpp | 110 ++++++ examples/sql/querymodel/editablesqlmodel.h | 63 +++ examples/sql/querymodel/main.cpp | 87 +++++ examples/sql/querymodel/querymodel.pro | 13 + .../relationaltablemodel/relationaltablemodel.cpp | 115 ++++++ .../relationaltablemodel/relationaltablemodel.pro | 9 + examples/sql/sql.pro | 12 + examples/sql/sqlwidgetmapper/main.cpp | 52 +++ examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro | 10 + examples/sql/sqlwidgetmapper/window.cpp | 159 ++++++++ examples/sql/sqlwidgetmapper/window.h | 90 +++++ examples/sql/tablemodel/tablemodel.cpp | 84 ++++ examples/sql/tablemodel/tablemodel.pro | 9 + 48 files changed, 3344 insertions(+) create mode 100644 examples/sql/README create mode 100644 examples/sql/cachedtable/cachedtable.pro create mode 100644 examples/sql/cachedtable/main.cpp create mode 100644 examples/sql/cachedtable/tableeditor.cpp create mode 100644 examples/sql/cachedtable/tableeditor.h create mode 100644 examples/sql/connection.h create mode 100644 examples/sql/drilldown/drilldown.pro create mode 100644 examples/sql/drilldown/drilldown.qrc create mode 100644 examples/sql/drilldown/imageitem.cpp create mode 100644 examples/sql/drilldown/imageitem.h create mode 100644 examples/sql/drilldown/images/beijing.png create mode 100644 examples/sql/drilldown/images/berlin.png create mode 100644 examples/sql/drilldown/images/brisbane.png create mode 100644 examples/sql/drilldown/images/munich.png create mode 100644 examples/sql/drilldown/images/oslo.png create mode 100644 examples/sql/drilldown/images/redwood.png create mode 100644 examples/sql/drilldown/informationwindow.cpp create mode 100644 examples/sql/drilldown/informationwindow.h create mode 100644 examples/sql/drilldown/logo.png create mode 100644 examples/sql/drilldown/main.cpp create mode 100644 examples/sql/drilldown/view.cpp create mode 100644 examples/sql/drilldown/view.h create mode 100644 examples/sql/masterdetail/albumdetails.xml create mode 100644 examples/sql/masterdetail/database.h create mode 100644 examples/sql/masterdetail/dialog.cpp create mode 100644 examples/sql/masterdetail/dialog.h create mode 100644 examples/sql/masterdetail/images/icon.png create mode 100644 examples/sql/masterdetail/images/image.png create mode 100644 examples/sql/masterdetail/main.cpp create mode 100644 examples/sql/masterdetail/mainwindow.cpp create mode 100644 examples/sql/masterdetail/mainwindow.h create mode 100644 examples/sql/masterdetail/masterdetail.pro create mode 100644 examples/sql/masterdetail/masterdetail.qrc create mode 100644 examples/sql/querymodel/customsqlmodel.cpp create mode 100644 examples/sql/querymodel/customsqlmodel.h create mode 100644 examples/sql/querymodel/editablesqlmodel.cpp create mode 100644 examples/sql/querymodel/editablesqlmodel.h create mode 100644 examples/sql/querymodel/main.cpp create mode 100644 examples/sql/querymodel/querymodel.pro create mode 100644 examples/sql/relationaltablemodel/relationaltablemodel.cpp create mode 100644 examples/sql/relationaltablemodel/relationaltablemodel.pro create mode 100644 examples/sql/sql.pro create mode 100644 examples/sql/sqlwidgetmapper/main.cpp create mode 100644 examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro create mode 100644 examples/sql/sqlwidgetmapper/window.cpp create mode 100644 examples/sql/sqlwidgetmapper/window.h create mode 100644 examples/sql/tablemodel/tablemodel.cpp create mode 100644 examples/sql/tablemodel/tablemodel.pro (limited to 'examples/sql') diff --git a/examples/sql/README b/examples/sql/README new file mode 100644 index 0000000000..56b4b3e11f --- /dev/null +++ b/examples/sql/README @@ -0,0 +1,40 @@ +Qt provides extensive database interoperability, with support for products +from both open source and proprietary vendors. + +SQL support is integrated with Qt's model/view architecture, making it easier +to provide GUI integration for your database applications. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/sql/cachedtable/cachedtable.pro b/examples/sql/cachedtable/cachedtable.pro new file mode 100644 index 0000000000..a14aa42d8f --- /dev/null +++ b/examples/sql/cachedtable/cachedtable.pro @@ -0,0 +1,11 @@ +HEADERS = ../connection.h \ + tableeditor.h +SOURCES = main.cpp \ + tableeditor.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/cachedtable +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS cachedtable.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/cachedtable +INSTALLS += target sources diff --git a/examples/sql/cachedtable/main.cpp b/examples/sql/cachedtable/main.cpp new file mode 100644 index 0000000000..47e3b9fdf4 --- /dev/null +++ b/examples/sql/cachedtable/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "../connection.h" +#include "tableeditor.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + TableEditor editor("person"); + editor.show(); + return editor.exec(); +} +//! [0] diff --git a/examples/sql/cachedtable/tableeditor.cpp b/examples/sql/cachedtable/tableeditor.cpp new file mode 100644 index 0000000000..33ddd54c2a --- /dev/null +++ b/examples/sql/cachedtable/tableeditor.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "tableeditor.h" + +//! [0] +TableEditor::TableEditor(const QString &tableName, QWidget *parent) + : QDialog(parent) +{ + model = new QSqlTableModel(this); + model->setTable(tableName); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + model->select(); + + model->setHeaderData(0, Qt::Horizontal, tr("ID")); + model->setHeaderData(1, Qt::Horizontal, tr("First name")); + model->setHeaderData(2, Qt::Horizontal, tr("Last name")); + +//! [0] //! [1] + QTableView *view = new QTableView; + view->setModel(model); +//! [1] + +//! [2] + submitButton = new QPushButton(tr("Submit")); + submitButton->setDefault(true); + revertButton = new QPushButton(tr("&Revert")); + quitButton = new QPushButton(tr("Quit")); + + buttonBox = new QDialogButtonBox(Qt::Vertical); + buttonBox->addButton(submitButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); +//! [2] + +//! [3] + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); + connect(revertButton, SIGNAL(clicked()), model, SLOT(revertAll())); + connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); +//! [3] + +//! [4] + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(view); + mainLayout->addWidget(buttonBox); + setLayout(mainLayout); + + setWindowTitle(tr("Cached Table")); +} +//! [4] + +//! [5] +void TableEditor::submit() +{ + model->database().transaction(); + if (model->submitAll()) { + model->database().commit(); + } else { + model->database().rollback(); + QMessageBox::warning(this, tr("Cached Table"), + tr("The database reported an error: %1") + .arg(model->lastError().text())); + } +} +//! [5] diff --git a/examples/sql/cachedtable/tableeditor.h b/examples/sql/cachedtable/tableeditor.h new file mode 100644 index 0000000000..3485fd3b8b --- /dev/null +++ b/examples/sql/cachedtable/tableeditor.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TABLEEDITOR_H +#define TABLEEDITOR_H + +#include + +QT_BEGIN_NAMESPACE +class QDialogButtonBox; +class QPushButton; +class QSqlTableModel; +QT_END_NAMESPACE + +//! [0] +class TableEditor : public QDialog +{ + Q_OBJECT + +public: + TableEditor(const QString &tableName, QWidget *parent = 0); + +private slots: + void submit(); + +private: + QPushButton *submitButton; + QPushButton *revertButton; + QPushButton *quitButton; + QDialogButtonBox *buttonBox; + QSqlTableModel *model; +}; +//! [0] + +#endif diff --git a/examples/sql/connection.h b/examples/sql/connection.h new file mode 100644 index 0000000000..d4b77ef266 --- /dev/null +++ b/examples/sql/connection.h @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONNECTION_H +#define CONNECTION_H + +#include +#include +#include +#include + +/* + This file defines a helper function to open a connection to an + in-memory SQLITE database and to create a test table. + + If you want to use another database, simply modify the code + below. All the examples in this directory use this function to + connect to a database. +*/ +//! [0] +static bool createConnection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, qApp->tr("Cannot open database"), + qApp->tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it.\n\n" + "Click Cancel to exit."), QMessageBox::Cancel); + return false; + } + + QSqlQuery query; + query.exec("create table person (id int primary key, " + "firstname varchar(20), lastname varchar(20))"); + query.exec("insert into person values(101, 'Danny', 'Young')"); + query.exec("insert into person values(102, 'Christine', 'Holand')"); + query.exec("insert into person values(103, 'Lars', 'Gordon')"); + query.exec("insert into person values(104, 'Roberto', 'Robitaille')"); + query.exec("insert into person values(105, 'Maria', 'Papadopoulos')"); + + query.exec("create table offices (id int primary key," + "imagefile int," + "location varchar(20)," + "country varchar(20)," + "description varchar(100))"); + query.exec("insert into offices " + "values(0, 0, 'Oslo', 'Norway'," + "'Oslo is home to more than 500 000 citizens and has a " + "lot to offer.It has been called \"The city with the big " + "heart\" and this is a nickname we are happy to live up to.')"); + query.exec("insert into offices " + "values(1, 1, 'Brisbane', 'Australia'," + "'Brisbane is the capital of Queensland, the Sunshine State, " + "where it is beautiful one day, perfect the next. " + "Brisbane is Australia''s 3rd largest city, being home " + "to almost 2 million people.')"); + query.exec("insert into offices " + "values(2, 2, 'Redwood City', 'US'," + "'You find Redwood City in the heart of the Bay Area " + "just north of Silicon Valley. The largest nearby city is " + "San Jose which is the third largest city in California " + "and the 10th largest in the US.')"); + query.exec("insert into offices " + "values(3, 3, 'Berlin', 'Germany'," + "'Berlin, the capital of Germany is dynamic, cosmopolitan " + "and creative, allowing for every kind of lifestyle. " + "East meets West in the metropolis at the heart of a " + "changing Europe.')"); + query.exec("insert into offices " + "values(4, 4, 'Munich', 'Germany'," + "'Several technology companies are represented in Munich, " + "and the city is often called the \"Bavarian Silicon Valley\". " + "The exciting city is also filled with culture, " + "art and music. ')"); + query.exec("insert into offices " + "values(5, 5, 'Beijing', 'China'," + "'Beijing as a capital city has more than 3000 years of " + "history. Today the city counts 12 million citizens, and " + "is the political, economic and cultural centre of China.')"); + + query.exec("create table images (locationid int, file varchar(20))"); + query.exec("insert into images values(0, 'images/oslo.png')"); + query.exec("insert into images values(1, 'images/brisbane.png')"); + query.exec("insert into images values(2, 'images/redwood.png')"); + query.exec("insert into images values(3, 'images/berlin.png')"); + query.exec("insert into images values(4, 'images/munich.png')"); + query.exec("insert into images values(5, 'images/beijing.png')"); + + + + return true; +} +//! [0] + +#endif diff --git a/examples/sql/drilldown/drilldown.pro b/examples/sql/drilldown/drilldown.pro new file mode 100644 index 0000000000..e15a4ada6b --- /dev/null +++ b/examples/sql/drilldown/drilldown.pro @@ -0,0 +1,16 @@ +HEADERS = ../connection.h \ + imageitem.h \ + informationwindow.h \ + view.h +RESOURCES = drilldown.qrc +SOURCES = imageitem.cpp \ + informationwindow.cpp \ + main.cpp \ + view.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/drilldown +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS drilldown.pro *.png *.jpg images +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/drilldown +INSTALLS += target sources diff --git a/examples/sql/drilldown/drilldown.qrc b/examples/sql/drilldown/drilldown.qrc new file mode 100644 index 0000000000..d2fb12295b --- /dev/null +++ b/examples/sql/drilldown/drilldown.qrc @@ -0,0 +1,11 @@ + + + images/oslo.png + images/brisbane.png + images/redwood.png + images/berlin.png + images/munich.png + images/beijing.png + logo.png + + diff --git a/examples/sql/drilldown/imageitem.cpp b/examples/sql/drilldown/imageitem.cpp new file mode 100644 index 0000000000..620afed048 --- /dev/null +++ b/examples/sql/drilldown/imageitem.cpp @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "imageitem.h" + +//! [0] +ImageItem::ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent, + QGraphicsScene *scene) + : QGraphicsPixmapItem(pixmap, parent, scene) +{ + recordId = id; + setAcceptsHoverEvents(true); + + timeLine.setDuration(150); + timeLine.setFrameRange(0, 150); + + connect(&timeLine, SIGNAL(frameChanged(int)), this, SLOT(setFrame(int))); + connect(&timeLine, SIGNAL(finished()), this, SLOT(updateItemPosition())); + + adjust(); +} +//! [0] + +//! [1] +void ImageItem::hoverEnterEvent(QGraphicsSceneHoverEvent * /*event*/) +{ + timeLine.setDirection(QTimeLine::Forward); + + if (z != 1.0) { + z = 1.0; + updateItemPosition(); + } + + if (timeLine.state() == QTimeLine::NotRunning) + timeLine.start(); +} +//! [1] + +//! [2] +void ImageItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * /*event*/) +{ + timeLine.setDirection(QTimeLine::Backward); + if (z != 0.0) + z = 0.0; + + if (timeLine.state() == QTimeLine::NotRunning) + timeLine.start(); +} +//! [2] + +//! [3] +void ImageItem::setFrame(int frame) +{ + adjust(); + QPointF center = boundingRect().center(); + + translate(center.x(), center.y()); + scale(1 + frame / 330.0, 1 + frame / 330.0); + translate(-center.x(), -center.y()); +} +//! [3] + +//! [4] +void ImageItem::adjust() +{ + QMatrix matrix; + matrix.scale(150/ boundingRect().width(), 120/ boundingRect().height()); + setMatrix(matrix); +} +//! [4] + +//! [5] +int ImageItem::id() +{ + return recordId; +} +//! [5] + +//! [6] +void ImageItem::updateItemPosition() +{ + setZValue(z); +} +//! [6] + + diff --git a/examples/sql/drilldown/imageitem.h b/examples/sql/drilldown/imageitem.h new file mode 100644 index 0000000000..20c781139a --- /dev/null +++ b/examples/sql/drilldown/imageitem.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IMAGEITEM_H +#define IMAGEITEM_H + +#include +#include + +//! [0] +class ImageItem : public QObject, public QGraphicsPixmapItem +{ + Q_OBJECT + +public: + ImageItem(int id, const QPixmap &pixmap, QGraphicsItem *parent = 0, + QGraphicsScene *scene = 0); + + void adjust(); + int id(); + +protected: + void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + +private slots: + void setFrame(int frame); + void updateItemPosition(); + +private: + QTimeLine timeLine; + int recordId; + double z; +}; +//! [0] + +#endif diff --git a/examples/sql/drilldown/images/beijing.png b/examples/sql/drilldown/images/beijing.png new file mode 100644 index 0000000000..45df148506 Binary files /dev/null and b/examples/sql/drilldown/images/beijing.png differ diff --git a/examples/sql/drilldown/images/berlin.png b/examples/sql/drilldown/images/berlin.png new file mode 100644 index 0000000000..ead62f0421 Binary files /dev/null and b/examples/sql/drilldown/images/berlin.png differ diff --git a/examples/sql/drilldown/images/brisbane.png b/examples/sql/drilldown/images/brisbane.png new file mode 100644 index 0000000000..04fff57f9c Binary files /dev/null and b/examples/sql/drilldown/images/brisbane.png differ diff --git a/examples/sql/drilldown/images/munich.png b/examples/sql/drilldown/images/munich.png new file mode 100644 index 0000000000..741eb77b8d Binary files /dev/null and b/examples/sql/drilldown/images/munich.png differ diff --git a/examples/sql/drilldown/images/oslo.png b/examples/sql/drilldown/images/oslo.png new file mode 100644 index 0000000000..02fa25e5a0 Binary files /dev/null and b/examples/sql/drilldown/images/oslo.png differ diff --git a/examples/sql/drilldown/images/redwood.png b/examples/sql/drilldown/images/redwood.png new file mode 100644 index 0000000000..f39ac06578 Binary files /dev/null and b/examples/sql/drilldown/images/redwood.png differ diff --git a/examples/sql/drilldown/informationwindow.cpp b/examples/sql/drilldown/informationwindow.cpp new file mode 100644 index 0000000000..6573ae1fdb --- /dev/null +++ b/examples/sql/drilldown/informationwindow.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "informationwindow.h" + +//! [0] +InformationWindow::InformationWindow(int id, QSqlRelationalTableModel *offices, + QWidget *parent) + : QDialog(parent) +{ +//! [0] //! [1] + QLabel *locationLabel = new QLabel(tr("Location: ")); + QLabel *countryLabel = new QLabel(tr("Country: ")); + QLabel *descriptionLabel = new QLabel(tr("Description: ")); + QLabel *imageFileLabel = new QLabel(tr("Image file: ")); + + createButtons(); + + locationText = new QLabel; + countryText = new QLabel; + descriptionEditor = new QTextEdit; +//! [1] + +//! [2] + imageFileEditor = new QComboBox; + imageFileEditor->setModel(offices->relationModel(1)); + imageFileEditor->setModelColumn(offices->relationModel(1)->fieldIndex("file")); +//! [2] + +//! [3] + mapper = new QDataWidgetMapper(this); + mapper->setModel(offices); + mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); + mapper->setItemDelegate(new QSqlRelationalDelegate(mapper)); + mapper->addMapping(imageFileEditor, 1); + mapper->addMapping(locationText, 2, "text"); + mapper->addMapping(countryText, 3, "text"); + mapper->addMapping(descriptionEditor, 4); + mapper->setCurrentIndex(id); +//! [3] + +//! [4] + connect(descriptionEditor, SIGNAL(textChanged()), + this, SLOT(enableButtons())); + connect(imageFileEditor, SIGNAL(currentIndexChanged(int)), + this, SLOT(enableButtons())); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(locationLabel, 0, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(countryLabel, 1, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(imageFileLabel, 2, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(descriptionLabel, 3, 0, Qt::AlignLeft | Qt::AlignTop); + layout->addWidget(locationText, 0, 1); + layout->addWidget(countryText, 1, 1); + layout->addWidget(imageFileEditor, 2, 1); + layout->addWidget(descriptionEditor, 3, 1); + layout->addWidget(buttonBox, 4, 0, 1, 2); + setLayout(layout); + + locationId = id; + displayedImage = imageFileEditor->currentText(); + + setWindowFlags(Qt::Window); + enableButtons(false); + setWindowTitle(tr("Office: %1").arg(locationText->text())); + resize(320, sizeHint().height()); +} +//! [4] + +//! [5] +int InformationWindow::id() +{ + return locationId; +} +//! [5] + +//! [6] +void InformationWindow::revert() +{ + mapper->revert(); + enableButtons(false); +} +//! [6] + +//! [7] +void InformationWindow::submit() +{ + QString newImage(imageFileEditor->currentText()); + + if (displayedImage != newImage) { + displayedImage = newImage; + emit imageChanged(locationId, newImage); + } + + mapper->submit(); + mapper->setCurrentIndex(locationId); + + enableButtons(false); +} +//! [7] + +//! [8] +void InformationWindow::createButtons() +{ + closeButton = new QPushButton(tr("&Close")); + revertButton = new QPushButton(tr("&Revert")); + submitButton = new QPushButton(tr("&Submit")); + + closeButton->setDefault(true); + + connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); + connect(revertButton, SIGNAL(clicked()), this, SLOT(revert())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); +//! [8] + +//! [9] + buttonBox = new QDialogButtonBox; + buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); +} +//! [9] + +//! [10] +void InformationWindow::enableButtons(bool enable) +{ + revertButton->setEnabled(enable); + submitButton->setEnabled(enable); +} +//! [10] + + diff --git a/examples/sql/drilldown/informationwindow.h b/examples/sql/drilldown/informationwindow.h new file mode 100644 index 0000000000..f28b47d548 --- /dev/null +++ b/examples/sql/drilldown/informationwindow.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef INFORMATIONWINDOW_H +#define INFORMATIONWINDOW_H + +#include +#include + +//! [0] +class InformationWindow : public QDialog +{ + Q_OBJECT + +public: + InformationWindow(int id, QSqlRelationalTableModel *offices, + QWidget *parent = 0); + + int id(); + +signals: + void imageChanged(int id, const QString &fileName); +//! [0] + +//! [1] +private slots: + void revert(); + void submit(); + void enableButtons(bool enable = true); +//! [1] + +//! [2] +private: + void createButtons(); + + int locationId; + QString displayedImage; + + QComboBox *imageFileEditor; + QLabel *locationText; + QLabel *countryText; + QTextEdit *descriptionEditor; + + QPushButton *closeButton; + QPushButton *submitButton; + QPushButton *revertButton; + QDialogButtonBox *buttonBox; + + QDataWidgetMapper *mapper; +}; +//! [2] + +#endif diff --git a/examples/sql/drilldown/logo.png b/examples/sql/drilldown/logo.png new file mode 100644 index 0000000000..4b1d235488 Binary files /dev/null and b/examples/sql/drilldown/logo.png differ diff --git a/examples/sql/drilldown/main.cpp b/examples/sql/drilldown/main.cpp new file mode 100644 index 0000000000..ee5a4d59a8 --- /dev/null +++ b/examples/sql/drilldown/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "../connection.h" +#include "view.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(drilldown); + + QApplication app(argc, argv); + + if (!createConnection()) + return 1; + + View view("offices", "images"); + view.show(); + return app.exec(); +} diff --git a/examples/sql/drilldown/view.cpp b/examples/sql/drilldown/view.cpp new file mode 100644 index 0000000000..763d094343 --- /dev/null +++ b/examples/sql/drilldown/view.cpp @@ -0,0 +1,179 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "informationwindow.h" +#include "imageitem.h" +#include "view.h" + +//! [0] +View::View(const QString &offices, const QString &images, QWidget *parent) + : QGraphicsView(parent) +{ + officeTable = new QSqlRelationalTableModel(this); + officeTable->setTable(offices); + officeTable->setRelation(1, QSqlRelation(images, "locationid", "file")); + officeTable->select(); +//! [0] + +//! [1] + scene = new QGraphicsScene(this); + scene->setSceneRect(0, 0, 465, 615); + setScene(scene); + + addItems(); + + QGraphicsPixmapItem *logo = scene->addPixmap(QPixmap(":/logo.png")); + logo->setPos(30, 515); + + setMinimumSize(470, 620); + setMaximumSize(470, 620); + setWindowTitle(tr("Offices World Wide")); +} +//! [1] + +//! [3] +void View::addItems() +{ + int officeCount = officeTable->rowCount(); + + int imageOffset = 150; + int leftMargin = 70; + int topMargin = 40; + + for (int i = 0; i < officeCount; i++) { + ImageItem *image; + QGraphicsTextItem *label; + QSqlRecord record = officeTable->record(i); + + int id = record.value("id").toInt(); + QString file = record.value("file").toString(); + QString location = record.value("location").toString(); + + int columnOffset = ((i / 3) * 37); + int x = ((i / 3) * imageOffset) + leftMargin + columnOffset; + int y = ((i % 3) * imageOffset) + topMargin; + + image = new ImageItem(id, QPixmap(":/" + file)); + image->setData(0, i); + image->setPos(x, y); + scene->addItem(image); + + label = scene->addText(location); + QPointF labelOffset((150 - label->boundingRect().width()) / 2, 120.0); + label->setPos(QPointF(x, y) + labelOffset); + } +} +//! [3] + +//! [5] +void View::mouseReleaseEvent(QMouseEvent *event) +{ + if (QGraphicsItem *item = itemAt(event->pos())) { + if (ImageItem *image = qgraphicsitem_cast(item)) + showInformation(image); + } + QGraphicsView::mouseReleaseEvent(event); +} +//! [5] + +//! [6] +void View::showInformation(ImageItem *image) +{ + int id = image->id(); + if (id < 0 || id >= officeTable->rowCount()) + return; + + InformationWindow *window = findWindow(id); + if (window && window->isVisible()) { + window->raise(); + window->activateWindow(); + } else if (window && !window->isVisible()) { + window->show(); + } else { + InformationWindow *window; + window = new InformationWindow(id, officeTable, this); + + connect(window, SIGNAL(imageChanged(int, QString)), + this, SLOT(updateImage(int, QString))); + + window->move(pos() + QPoint(20, 40)); + window->show(); + informationWindows.append(window); + } +} +//! [6] + +//! [7] +void View::updateImage(int id, const QString &fileName) +{ + QList items = scene->items(); + + while(!items.empty()) { + QGraphicsItem *item = items.takeFirst(); + + if (ImageItem *image = qgraphicsitem_cast(item)) { + if (image->id() == id){ + image->setPixmap(QPixmap(":/" +fileName)); + image->adjust(); + break; + } + } + } +} +//! [7] + +//! [8] +InformationWindow* View::findWindow(int id) +{ + QList::iterator i, beginning, end; + + beginning = informationWindows.begin(); + end = informationWindows.end(); + + for (i = beginning; i != end; ++i) { + InformationWindow *window = (*i); + if (window && (window->id() == id)) + return window; + } + return 0; +} +//! [8] + diff --git a/examples/sql/drilldown/view.h b/examples/sql/drilldown/view.h new file mode 100644 index 0000000000..37ddabba54 --- /dev/null +++ b/examples/sql/drilldown/view.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VIEW_H +#define VIEW_H + +#include +#include + +class ImageItem; +class InformationWindow; + +//! [0] +class View : public QGraphicsView +{ + Q_OBJECT + +public: + View(const QString &offices, const QString &images, QWidget *parent = 0); + +protected: + void mouseReleaseEvent(QMouseEvent *event); +//! [0] + +//! [1] +private slots: + void updateImage(int id, const QString &fileName); +//! [1] + +//! [2] +private: + void addItems(); + InformationWindow* findWindow(int id); + void showInformation(ImageItem *image); + + QGraphicsScene *scene; + QList informationWindows; +//! [2] //! [3] + QSqlRelationalTableModel *officeTable; +}; +//! [3] + +#endif diff --git a/examples/sql/masterdetail/albumdetails.xml b/examples/sql/masterdetail/albumdetails.xml new file mode 100644 index 0000000000..aacbdac81d --- /dev/null +++ b/examples/sql/masterdetail/albumdetails.xml @@ -0,0 +1,98 @@ + + + Humming one of your songs + Are they saying goodbye + On off + I shot my heart + Drowning in Those Eyes + So you did it again + One more time + Headphone silence + What I want + Sleeping by the Fyris River + Wooden Body + Humming one of your songs (encore) + + + To let myself go + Rubber and Soul + Balloon ranger + My lover will go + Temporary dive + Laid in earth + This voice + Where friend rhymes with end + Song No.6 feat Ron Sexsmith + The Fight Song + + + From Grace + All's not last + That Great October Sound + Life Here Is Gold + Tomorrow Stays The Same + Postulate + Adelaide + John Wayne + Love's Lost + Dreamweaver + Outro + + + Rain down on me + Cecilia + Make a mess of yourself + Pale green eyes + Either way I'm gone + Honey + Rise in shame + Stray dogs + The willow + Stay home + Outro + + + + + Kontroll på kontinentet + Ompa til du dør + Bøn fra helvete + 170 + Rullett + Dr. Mowinckel + Fra sjåfør til passasjer + Resistansen + Dekk bord + Bak et halleluja + Bris + Mr. Kaizer, hans Constanze og meg + + + Di grind + Hevnervals + Evig pint + De involverte + Djevelens orkester + Container + Naade + Min kvite russer + Veterans klage + Til depotet + Salt og pepper + Drøm Hardt (Requiem Part I) + + + KGB + Maestro + Knekker deg til sist + Senor Flamingos Adieu + Blitzregn baby + Dieter Meyers Inst. + Christiania + Delikatessen + Jævel av en tango + Papa har lov + Auksjon (i Dieter Meyers hall) + På ditt skift + + diff --git a/examples/sql/masterdetail/database.h b/examples/sql/masterdetail/database.h new file mode 100644 index 0000000000..a760e41741 --- /dev/null +++ b/examples/sql/masterdetail/database.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DATABASE_H +#define DATABASE_H + +#include +#include +#include +#include + +static bool createConnection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, qApp->tr("Cannot open database"), + qApp->tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it.\n\n" + "Click Cancel to exit."), QMessageBox::Cancel); + return false; + } + + QSqlQuery query; + + query.exec("create table artists (id int primary key, " + "artist varchar(40), " + "albumcount int)"); + + query.exec("insert into artists values(0, '', 0)"); + query.exec("insert into artists values(1, 'Ane Brun', 2)"); + query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)"); + query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)"); + + query.exec("create table albums (albumid int primary key, " + "title varchar(50), " + "artistid int, " + "year int)"); + + query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, " + "2003)"); + query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)"); + query.exec("insert into albums values(3, '...The Great October Sound', 2, " + "2002)"); + query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)"); + query.exec("insert into albums values(5, " + "'One day you`ll dance for me, New York City', 2, 2004)"); + query.exec("insert into albums values(6, 'Ompa Til Du D\xf8r', 3, 2001)"); + query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)"); + query.exec("insert into albums values(8, 'Maestro', 3, 2005)"); + + return true; +} + +#endif + + diff --git a/examples/sql/masterdetail/dialog.cpp b/examples/sql/masterdetail/dialog.cpp new file mode 100644 index 0000000000..0adf20819f --- /dev/null +++ b/examples/sql/masterdetail/dialog.cpp @@ -0,0 +1,283 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dialog.h" + +int uniqueAlbumId; +int uniqueArtistId; + +Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details, + QFile *output, QWidget *parent) + : QDialog(parent) +{ + model = albums; + albumDetails = details; + outputFile = output; + + QGroupBox *inputWidgetBox = createInputWidgets(); + QDialogButtonBox *buttonBox = createButtons(); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(inputWidgetBox); + layout->addWidget(buttonBox); + setLayout(layout); + + setWindowTitle(tr("Add Album")); +} + +void Dialog::submit() +{ + QString artist = artistEditor->text(); + QString title = titleEditor->text(); + + if (artist.isEmpty() || title.isEmpty()) { + QString message(tr("Please provide both the name of the artist " + "and the title of the album.")); + QMessageBox::information(this, tr("Add Album"), message); + } else { + int artistId = findArtistId(artist); + int albumId = addNewAlbum(title, artistId); + + QStringList tracks; + tracks = tracksEditor->text().split(',', QString::SkipEmptyParts); + addTracks(albumId, tracks); + + increaseAlbumCount(indexOfArtist(artist)); + accept(); + } +} + +int Dialog::findArtistId(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + int row = 0; + + while (row < artistModel->rowCount()) { + QSqlRecord record = artistModel->record(row); + if (record.value("artist") == artist) + return record.value("id").toInt(); + else + row++; + } + return addNewArtist(artist); +} + + +int Dialog::addNewArtist(const QString &name) +{ + QSqlTableModel *artistModel = model->relationModel(2); + QSqlRecord record; + + int id = generateArtistId(); + + QSqlField f1("id", QVariant::Int); + QSqlField f2("artist", QVariant::String); + QSqlField f3("albumcount", QVariant::Int); + + f1.setValue(QVariant(id)); + f2.setValue(QVariant(name)); + f3.setValue(QVariant(0)); + record.append(f1); + record.append(f2); + record.append(f3); + + artistModel->insertRecord(-1, record); + return id; +} + +int Dialog::addNewAlbum(const QString &title, int artistId) +{ + int id = generateAlbumId(); + QSqlRecord record; + + QSqlField f1("albumid", QVariant::Int); + QSqlField f2("title", QVariant::String); + QSqlField f3("artistid", QVariant::Int); + QSqlField f4("year", QVariant::Int); + + f1.setValue(QVariant(id)); + f2.setValue(QVariant(title)); + f3.setValue(QVariant(artistId)); + f4.setValue(QVariant(yearEditor->value())); + record.append(f1); + record.append(f2); + record.append(f3); + record.append(f4); + + model->insertRecord(-1, record); + return id; +} + +void Dialog::addTracks(int albumId, QStringList tracks) +{ + QDomElement albumNode = albumDetails.createElement("album"); + albumNode.setAttribute("id", albumId); + + for (int i = 0; i < tracks.count(); i++) { + QString trackNumber = QString::number(i); + if (i < 10) + trackNumber.prepend("0"); + + QDomText textNode = albumDetails.createTextNode(tracks.at(i)); + + QDomElement trackNode = albumDetails.createElement("track"); + trackNode.setAttribute("number", trackNumber); + trackNode.appendChild(textNode); + + albumNode.appendChild(trackNode); + } + + QDomNodeList archive = albumDetails.elementsByTagName("archive"); + archive.item(0).appendChild(albumNode); + +/* + The following code is commented out since the example uses an in + memory database, i.e., altering the XML file will bring the data + out of sync. + + if (!outputFile->open(QIODevice::WriteOnly)) { + return; + } else { + QTextStream stream(outputFile); + archive.item(0).save(stream, 4); + outputFile->close(); + } +*/ +} + +void Dialog::increaseAlbumCount(QModelIndex artistIndex) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + QModelIndex albumCountIndex; + albumCountIndex = artistIndex.sibling(artistIndex.row(), 2); + + int albumCount = albumCountIndex.data().toInt(); + artistModel->setData(albumCountIndex, QVariant(albumCount + 1)); +} + + +void Dialog::revert() +{ + artistEditor->clear(); + titleEditor->clear(); + yearEditor->setValue(QDate::currentDate().year()); + tracksEditor->clear(); +} + +QGroupBox *Dialog::createInputWidgets() +{ + QGroupBox *box = new QGroupBox(tr("Add Album")); + + QLabel *artistLabel = new QLabel(tr("Artist:")); + QLabel *titleLabel = new QLabel(tr("Title:")); + QLabel *yearLabel = new QLabel(tr("Year:")); + QLabel *tracksLabel = new QLabel(tr("Tracks (separated by comma):")); + + artistEditor = new QLineEdit; + titleEditor = new QLineEdit; + + yearEditor = new QSpinBox; + yearEditor->setMinimum(1900); + yearEditor->setMaximum(QDate::currentDate().year()); + yearEditor->setValue(yearEditor->maximum()); + yearEditor->setReadOnly(false); + + tracksEditor = new QLineEdit; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artistLabel, 0, 0); + layout->addWidget(artistEditor, 0, 1); + layout->addWidget(titleLabel, 1, 0); + layout->addWidget(titleEditor, 1, 1); + layout->addWidget(yearLabel, 2, 0); + layout->addWidget(yearEditor, 2, 1); + layout->addWidget(tracksLabel, 3, 0, 1, 2); + layout->addWidget(tracksEditor, 4, 0, 1, 2); + box->setLayout(layout); + + return box; +} + +QDialogButtonBox *Dialog::createButtons() +{ + QPushButton *closeButton = new QPushButton(tr("&Close")); + QPushButton *revertButton = new QPushButton(tr("&Revert")); + QPushButton *submitButton = new QPushButton(tr("&Submit")); + + closeButton->setDefault(true); + + connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); + connect(revertButton, SIGNAL(clicked()), this, SLOT(revert())); + connect(submitButton, SIGNAL(clicked()), this, SLOT(submit())); + + QDialogButtonBox *buttonBox = new QDialogButtonBox; + buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole); + buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole); + + return buttonBox; +} + +QModelIndex Dialog::indexOfArtist(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + for (int i = 0; i < artistModel->rowCount(); i++) { + QSqlRecord record = artistModel->record(i); + if (record.value("artist") == artist) + return artistModel->index(i, 1); + } + + return QModelIndex(); +} + +int Dialog::generateArtistId() +{ + uniqueArtistId += 1; + return uniqueArtistId; +} + +int Dialog::generateAlbumId() +{ + uniqueAlbumId += 1; + return uniqueAlbumId; +} diff --git a/examples/sql/masterdetail/dialog.h b/examples/sql/masterdetail/dialog.h new file mode 100644 index 0000000000..0f4b54a2a5 --- /dev/null +++ b/examples/sql/masterdetail/dialog.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + Dialog(QSqlRelationalTableModel *albums, QDomDocument details, + QFile *output, QWidget *parent = 0); + +private slots: + void revert(); + void submit(); + +private: + int addNewAlbum(const QString &title, int artistId); + int addNewArtist(const QString &name); + void addTracks(int albumId, QStringList tracks); + QDialogButtonBox *createButtons(); + QGroupBox *createInputWidgets(); + int findArtistId(const QString &artist); + int generateAlbumId(); + int generateArtistId(); + void increaseAlbumCount(QModelIndex artistIndex); + QModelIndex indexOfArtist(const QString &artist); + + QSqlRelationalTableModel *model; + QDomDocument albumDetails; + QFile *outputFile; + + QLineEdit *artistEditor; + QLineEdit *titleEditor; + QSpinBox *yearEditor; + QLineEdit *tracksEditor; +}; + +#endif diff --git a/examples/sql/masterdetail/images/icon.png b/examples/sql/masterdetail/images/icon.png new file mode 100644 index 0000000000..31f68b8eb5 Binary files /dev/null and b/examples/sql/masterdetail/images/icon.png differ diff --git a/examples/sql/masterdetail/images/image.png b/examples/sql/masterdetail/images/image.png new file mode 100644 index 0000000000..1d7803746b Binary files /dev/null and b/examples/sql/masterdetail/images/image.png differ diff --git a/examples/sql/masterdetail/main.cpp b/examples/sql/masterdetail/main.cpp new file mode 100644 index 0000000000..932b67a051 --- /dev/null +++ b/examples/sql/masterdetail/main.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "database.h" +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(masterdetail); + + QApplication app(argc, argv); + + if (!createConnection()) + return 1; + + QFile *albumDetails = new QFile("albumdetails.xml"); + MainWindow window("artists", "albums", albumDetails); + window.show(); + return app.exec(); +} diff --git a/examples/sql/masterdetail/mainwindow.cpp b/examples/sql/masterdetail/mainwindow.cpp new file mode 100644 index 0000000000..67825d6149 --- /dev/null +++ b/examples/sql/masterdetail/mainwindow.cpp @@ -0,0 +1,430 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "dialog.h" + +#include +#include +#include + +extern int uniqueAlbumId; +extern int uniqueArtistId; + +MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, + QFile *albumDetails, QWidget *parent) + : QMainWindow(parent) +{ + file = albumDetails; + readAlbumData(); + + model = new QSqlRelationalTableModel(this); + model->setTable(albumTable); + model->setRelation(2, QSqlRelation(artistTable, "id", "artist")); + model->select(); + + QGroupBox *artists = createArtistGroupBox(); + QGroupBox *albums = createAlbumGroupBox(); + QGroupBox *details = createDetailsGroupBox(); + + artistView->setCurrentIndex(0); + uniqueAlbumId = model->rowCount(); + uniqueArtistId = artistView->count(); + + connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)), + this, SLOT(updateHeader(QModelIndex, int, int))); + connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)), + this, SLOT(updateHeader(QModelIndex, int, int))); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artists, 0, 0); + layout->addWidget(albums, 1, 0); + layout->addWidget(details, 0, 1, 2, 1); + layout->setColumnStretch(1, 1); + layout->setColumnMinimumWidth(0, 500); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + setCentralWidget(widget); + createMenuBar(); + + showImageLabel(); + resize(850, 400); + setWindowTitle(tr("Music Archive")); +} + +void MainWindow::changeArtist(int row) +{ + if (row > 0) { + QModelIndex index = model->relationModel(2)->index(row, 1); + model->setFilter("artist = '" + index.data().toString() + '\'') ; + showArtistProfile(index); + } else if (row == 0) { + model->setFilter(QString()); + showImageLabel(); + } else { + return; + } +} + +void MainWindow::showArtistProfile(QModelIndex index) +{ + QSqlRecord record = model->relationModel(2)->record(index.row()); + + QString name = record.value("artist").toString(); + QString count = record.value("albumcount").toString(); + profileLabel->setText(tr("Artist : %1 \n" \ + "Number of Albums: %2").arg(name).arg(count)); + + profileLabel->show(); + iconLabel->show(); + + titleLabel->hide(); + trackList->hide(); + imageLabel->hide(); +} + +void MainWindow::showAlbumDetails(QModelIndex index) +{ + QSqlRecord record = model->record(index.row()); + + QString artist = record.value("artist").toString(); + QString title = record.value("title").toString(); + QString year = record.value("year").toString(); + QString albumId = record.value("albumid").toString(); + + showArtistProfile(indexOfArtist(artist)); + titleLabel->setText(tr("Title: %1 (%2)").arg(title).arg(year)); + titleLabel->show(); + + QDomNodeList albums = albumData.elementsByTagName("album"); + for (int i = 0; i < albums.count(); i++) { + QDomNode album = albums.item(i); + if (album.toElement().attribute("id") == albumId) { + getTrackList(album.toElement()); + break; + } + } + if (!trackList->count() == 0) + trackList->show(); +} + +void MainWindow::getTrackList(QDomNode album) +{ + trackList->clear(); + + QDomNodeList tracks = album.childNodes(); + QDomNode track; + QString trackNumber; + + for (int j = 0; j < tracks.count(); j++) { + + track = tracks.item(j); + trackNumber = track.toElement().attribute("number"); + + QListWidgetItem *item = new QListWidgetItem(trackList); + item->setText(trackNumber + ": " + track.toElement().text()); + } +} + +void MainWindow::addAlbum() +{ + Dialog *dialog = new Dialog(model, albumData, file, this); + int accepted = dialog->exec(); + + if (accepted == 1) { + int lastRow = model->rowCount() - 1; + albumView->selectRow(lastRow); + albumView->scrollToBottom(); + showAlbumDetails(model->index(lastRow, 0)); + } +} + +void MainWindow::deleteAlbum() +{ + QModelIndexList selection = albumView->selectionModel()->selectedRows(0); + + if (!selection.empty()) { + QModelIndex idIndex = selection.at(0); + int id = idIndex.data().toInt(); + QString title = idIndex.sibling(idIndex.row(), 1).data().toString(); + QString artist = idIndex.sibling(idIndex.row(), 2).data().toString(); + + QMessageBox::StandardButton button; + button = QMessageBox::question(this, tr("Delete Album"), + QString(tr("Are you sure you want to " \ + "delete '%1' by '%2'?")) + .arg(title).arg(artist), + QMessageBox::Yes | QMessageBox::No); + + if (button == QMessageBox::Yes) { + removeAlbumFromFile(id); + removeAlbumFromDatabase(idIndex); + decreaseAlbumCount(indexOfArtist(artist)); + + showImageLabel(); + } + } else { + QMessageBox::information(this, tr("Delete Album"), + tr("Select the album you want to delete.")); + } +} + +void MainWindow::removeAlbumFromFile(int id) +{ + + QDomNodeList albums = albumData.elementsByTagName("album"); + + for (int i = 0; i < albums.count(); i++) { + QDomNode node = albums.item(i); + if (node.toElement().attribute("id").toInt() == id) { + albumData.elementsByTagName("archive").item(0).removeChild(node); + break; + } + } +/* + The following code is commented out since the example uses an in + memory database, i.e., altering the XML file will bring the data + out of sync. + + if (!file->open(QIODevice::WriteOnly)) { + return; + } else { + QTextStream stream(file); + albumData.elementsByTagName("archive").item(0).save(stream, 4); + file->close(); + } +*/ +} + +void MainWindow::removeAlbumFromDatabase(QModelIndex index) +{ + model->removeRow(index.row()); +} + +void MainWindow::decreaseAlbumCount(QModelIndex artistIndex) +{ + int row = artistIndex.row(); + QModelIndex albumCountIndex = artistIndex.sibling(row, 2); + int albumCount = albumCountIndex.data().toInt(); + + QSqlTableModel *artists = model->relationModel(2); + + if (albumCount == 1) { + artists->removeRow(row); + showImageLabel(); + } else { + artists->setData(albumCountIndex, QVariant(albumCount - 1)); + } +} + +void MainWindow::readAlbumData() +{ + if (!file->open(QIODevice::ReadOnly)) + return; + + if (!albumData.setContent(file)) { + file->close(); + return; + } + file->close(); +} + +QGroupBox* MainWindow::createArtistGroupBox() +{ + artistView = new QComboBox; + artistView->setModel(model->relationModel(2)); + artistView->setModelColumn(1); + + connect(artistView, SIGNAL(currentIndexChanged(int)), + this, SLOT(changeArtist(int))); + + QGroupBox *box = new QGroupBox(tr("Artist")); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(artistView, 0, 0); + box->setLayout(layout); + + return box; +} + +QGroupBox* MainWindow::createAlbumGroupBox() +{ + QGroupBox *box = new QGroupBox(tr("Album")); + + albumView = new QTableView; + albumView->setEditTriggers(QAbstractItemView::NoEditTriggers); + albumView->setSortingEnabled(true); + albumView->setSelectionBehavior(QAbstractItemView::SelectRows); + albumView->setSelectionMode(QAbstractItemView::SingleSelection); + albumView->setShowGrid(false); + albumView->verticalHeader()->hide(); + albumView->setAlternatingRowColors(true); + albumView->setModel(model); + adjustHeader(); + + QLocale locale = albumView->locale(); + locale.setNumberOptions(QLocale::OmitGroupSeparator); + albumView->setLocale(locale); + + connect(albumView, SIGNAL(clicked(QModelIndex)), + this, SLOT(showAlbumDetails(QModelIndex))); + connect(albumView, SIGNAL(activated(QModelIndex)), + this, SLOT(showAlbumDetails(QModelIndex))); + + QVBoxLayout *layout = new QVBoxLayout; + layout->addWidget(albumView, 0, 0); + box->setLayout(layout); + + return box; +} + +QGroupBox* MainWindow::createDetailsGroupBox() +{ + QGroupBox *box = new QGroupBox(tr("Details")); + + profileLabel = new QLabel; + profileLabel->setWordWrap(true); + profileLabel->setAlignment(Qt::AlignBottom); + + titleLabel = new QLabel; + titleLabel->setWordWrap(true); + titleLabel->setAlignment(Qt::AlignBottom); + + iconLabel = new QLabel(); + iconLabel->setAlignment(Qt::AlignBottom | Qt::AlignRight); + iconLabel->setPixmap(QPixmap(":/images/icon.png")); + + imageLabel = new QLabel; + imageLabel->setWordWrap(true); + imageLabel->setAlignment(Qt::AlignCenter); + imageLabel->setPixmap(QPixmap(":/images/image.png")); + + trackList = new QListWidget; + + QGridLayout *layout = new QGridLayout; + layout->addWidget(imageLabel, 0, 0, 3, 2); + layout->addWidget(profileLabel, 0, 0); + layout->addWidget(iconLabel, 0, 1); + layout->addWidget(titleLabel, 1, 0, 1, 2); + layout->addWidget(trackList, 2, 0, 1, 2); + layout->setRowStretch(2, 1); + box->setLayout(layout); + + return box; +} + +void MainWindow::createMenuBar() +{ + QAction *addAction = new QAction(tr("&Add album..."), this); + QAction *deleteAction = new QAction(tr("&Delete album..."), this); + QAction *quitAction = new QAction(tr("&Quit"), this); + QAction *aboutAction = new QAction(tr("&About"), this); + QAction *aboutQtAction = new QAction(tr("About &Qt"), this); + + addAction->setShortcut(tr("Ctrl+A")); + deleteAction->setShortcut(tr("Ctrl+D")); + quitAction->setShortcut(tr("Ctrl+Q")); + + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(addAction); + fileMenu->addAction(deleteAction); + fileMenu->addSeparator(); + fileMenu->addAction(quitAction); + + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAction); + helpMenu->addAction(aboutQtAction); + + connect(addAction, SIGNAL(triggered(bool)), this, SLOT(addAlbum())); + connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteAlbum())); + connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close())); + connect(aboutAction, SIGNAL(triggered(bool)), this, SLOT(about())); + connect(aboutQtAction, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); +} + +void MainWindow::showImageLabel() +{ + profileLabel->hide(); + titleLabel->hide(); + iconLabel->hide(); + trackList->hide(); + + imageLabel->show(); +} + +QModelIndex MainWindow::indexOfArtist(const QString &artist) +{ + QSqlTableModel *artistModel = model->relationModel(2); + + for (int i = 0; i < artistModel->rowCount(); i++) { + QSqlRecord record = artistModel->record(i); + if (record.value("artist") == artist) + return artistModel->index(i, 1); + } + return QModelIndex(); +} + +void MainWindow::updateHeader(QModelIndex, int, int) +{ + adjustHeader(); +} + +void MainWindow::adjustHeader() +{ + albumView->hideColumn(0); + albumView->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch); + albumView->resizeColumnToContents(2); + albumView->resizeColumnToContents(3); +} + +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Music Archive"), + tr("

The Music Archive example shows how to present " + "data from different data sources in the same application. " + "The album titles, and the corresponding artists and release dates, " + "are kept in a database, while each album's tracks are stored " + "in an XML file.

The example also shows how to add as " + "well as remove data from both the database and the " + "associated XML file using the API provided by the QtSql and " + "QtXml modules, respectively.

")); +} diff --git a/examples/sql/masterdetail/mainwindow.h b/examples/sql/masterdetail/mainwindow.h new file mode 100644 index 0000000000..4d05022ba5 --- /dev/null +++ b/examples/sql/masterdetail/mainwindow.h @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QComboBox; +class QFile; +class QGroupBox; +class QLabel; +class QListWidget; +class QSqlRelationalTableModel; +class QTableView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(const QString &artistTable, const QString &albumTable, + QFile *albumDetails, QWidget *parent = 0); + +private slots: + void about(); + void addAlbum(); + void changeArtist(int row); + void deleteAlbum(); + void showAlbumDetails(QModelIndex index); + void showArtistProfile(QModelIndex index); + void updateHeader(QModelIndex, int, int); + +private: + void adjustHeader(); + QGroupBox *createAlbumGroupBox(); + QGroupBox *createArtistGroupBox(); + QGroupBox *createDetailsGroupBox(); + void createMenuBar(); + void decreaseAlbumCount(QModelIndex artistIndex); + void getTrackList(QDomNode album); + QModelIndex indexOfArtist(const QString &artist); + void readAlbumData(); + void removeAlbumFromDatabase(QModelIndex album); + void removeAlbumFromFile(int id); + void showImageLabel(); + + QTableView *albumView; + QComboBox *artistView; + QListWidget *trackList; + + QLabel *iconLabel; + QLabel *imageLabel; + QLabel *profileLabel; + QLabel *titleLabel; + + QDomDocument albumData; + QFile *file; + QSqlRelationalTableModel *model; +}; + +#endif diff --git a/examples/sql/masterdetail/masterdetail.pro b/examples/sql/masterdetail/masterdetail.pro new file mode 100644 index 0000000000..205c54405c --- /dev/null +++ b/examples/sql/masterdetail/masterdetail.pro @@ -0,0 +1,16 @@ +HEADERS = database.h \ + dialog.h \ + mainwindow.h +RESOURCES = masterdetail.qrc +SOURCES = dialog.cpp \ + main.cpp \ + mainwindow.cpp + +QT += sql +QT += xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/masterdetail +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS masterdetail.pro *.xml images +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/masterdetail +INSTALLS += target sources diff --git a/examples/sql/masterdetail/masterdetail.qrc b/examples/sql/masterdetail/masterdetail.qrc new file mode 100644 index 0000000000..717c2eb945 --- /dev/null +++ b/examples/sql/masterdetail/masterdetail.qrc @@ -0,0 +1,6 @@ + + + images/icon.png + images/image.png + + diff --git a/examples/sql/querymodel/customsqlmodel.cpp b/examples/sql/querymodel/customsqlmodel.cpp new file mode 100644 index 0000000000..e122ce89bb --- /dev/null +++ b/examples/sql/querymodel/customsqlmodel.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "customsqlmodel.h" + +CustomSqlModel::CustomSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +QVariant CustomSqlModel::data(const QModelIndex &index, int role) const +{ + QVariant value = QSqlQueryModel::data(index, role); + if (value.isValid() && role == Qt::DisplayRole) { + if (index.column() == 0) + return value.toString().prepend("#"); + else if (index.column() == 2) + return value.toString().toUpper(); + } + if (role == Qt::TextColorRole && index.column() == 1) + return qVariantFromValue(QColor(Qt::blue)); + return value; +} +//! [0] diff --git a/examples/sql/querymodel/customsqlmodel.h b/examples/sql/querymodel/customsqlmodel.h new file mode 100644 index 0000000000..f4a0eab454 --- /dev/null +++ b/examples/sql/querymodel/customsqlmodel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CUSTOMSQLMODEL_H +#define CUSTOMSQLMODEL_H + +#include + +//! [0] +class CustomSqlModel : public QSqlQueryModel +{ + Q_OBJECT + +public: + CustomSqlModel(QObject *parent = 0); + + QVariant data(const QModelIndex &item, int role) const; +}; +//! [0] + +#endif diff --git a/examples/sql/querymodel/editablesqlmodel.cpp b/examples/sql/querymodel/editablesqlmodel.cpp new file mode 100644 index 0000000000..d7841f6457 --- /dev/null +++ b/examples/sql/querymodel/editablesqlmodel.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "editablesqlmodel.h" + +EditableSqlModel::EditableSqlModel(QObject *parent) + : QSqlQueryModel(parent) +{ +} + +//! [0] +Qt::ItemFlags EditableSqlModel::flags( + const QModelIndex &index) const +{ + Qt::ItemFlags flags = QSqlQueryModel::flags(index); + if (index.column() == 1 || index.column() == 2) + flags |= Qt::ItemIsEditable; + return flags; +} +//! [0] + +//! [1] +bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int /* role */) +{ + if (index.column() < 1 || index.column() > 2) + return false; + + QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0); + int id = data(primaryKeyIndex).toInt(); + + clear(); + + bool ok; + if (index.column() == 1) { + ok = setFirstName(id, value.toString()); + } else { + ok = setLastName(id, value.toString()); + } + refresh(); + return ok; +} +//! [1] + +void EditableSqlModel::refresh() +{ + setQuery("select * from person"); + setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +//! [2] +bool EditableSqlModel::setFirstName(int personId, const QString &firstName) +{ + QSqlQuery query; + query.prepare("update person set firstname = ? where id = ?"); + query.addBindValue(firstName); + query.addBindValue(personId); + return query.exec(); +} +//! [2] + +bool EditableSqlModel::setLastName(int personId, const QString &lastName) +{ + QSqlQuery query; + query.prepare("update person set lastname = ? where id = ?"); + query.addBindValue(lastName); + query.addBindValue(personId); + return query.exec(); +} diff --git a/examples/sql/querymodel/editablesqlmodel.h b/examples/sql/querymodel/editablesqlmodel.h new file mode 100644 index 0000000000..3606ebccde --- /dev/null +++ b/examples/sql/querymodel/editablesqlmodel.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EDITABLESQLMODEL_H +#define EDITABLESQLMODEL_H + +#include + +class EditableSqlModel : public QSqlQueryModel +{ + Q_OBJECT + +public: + EditableSqlModel(QObject *parent = 0); + + Qt::ItemFlags flags(const QModelIndex &index) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + +private: + bool setFirstName(int personId, const QString &firstName); + bool setLastName(int personId, const QString &lastName); + void refresh(); +}; + +#endif diff --git a/examples/sql/querymodel/main.cpp b/examples/sql/querymodel/main.cpp new file mode 100644 index 0000000000..90d4265e6f --- /dev/null +++ b/examples/sql/querymodel/main.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "../connection.h" +#include "customsqlmodel.h" +#include "editablesqlmodel.h" + +void initializeModel(QSqlQueryModel *model) +{ + model->setQuery("select * from person"); + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +void createView(const QString &title, QSqlQueryModel *model) +{ + static int offset = 0; + + QTableView *view = new QTableView; + view->setModel(model); + view->setWindowTitle(title); + view->move(100 + offset, 100 + offset); + offset += 20; + view->show(); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + QSqlQueryModel plainModel; + EditableSqlModel editableModel; + CustomSqlModel customModel; + + initializeModel(&plainModel); + initializeModel(&editableModel); + initializeModel(&customModel); + + createView(QObject::tr("Plain Query Model"), &plainModel); + createView(QObject::tr("Editable Query Model"), &editableModel); + createView(QObject::tr("Custom Query Model"), &customModel); + + return app.exec(); +} diff --git a/examples/sql/querymodel/querymodel.pro b/examples/sql/querymodel/querymodel.pro new file mode 100644 index 0000000000..e0e0813890 --- /dev/null +++ b/examples/sql/querymodel/querymodel.pro @@ -0,0 +1,13 @@ +HEADERS = ../connection.h \ + customsqlmodel.h \ + editablesqlmodel.h +SOURCES = customsqlmodel.cpp \ + editablesqlmodel.cpp \ + main.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/querymodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS querymodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/querymodel +INSTALLS += target sources diff --git a/examples/sql/relationaltablemodel/relationaltablemodel.cpp b/examples/sql/relationaltablemodel/relationaltablemodel.cpp new file mode 100644 index 0000000000..b38cdda6fb --- /dev/null +++ b/examples/sql/relationaltablemodel/relationaltablemodel.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "../connection.h" + +void initializeModel(QSqlRelationalTableModel *model) +{ +//! [0] + model->setTable("employee"); +//! [0] + + model->setEditStrategy(QSqlTableModel::OnManualSubmit); +//! [1] + model->setRelation(2, QSqlRelation("city", "id", "name")); +//! [1] //! [2] + model->setRelation(3, QSqlRelation("country", "id", "name")); +//! [2] + +//! [3] + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("City")); + model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country")); +//! [3] + + model->select(); +} + +QTableView *createView(const QString &title, QSqlTableModel *model) +{ +//! [4] + QTableView *view = new QTableView; + view->setModel(model); + view->setItemDelegate(new QSqlRelationalDelegate(view)); +//! [4] + view->setWindowTitle(title); + return view; +} + +void createRelationalTables() +{ + QSqlQuery query; + query.exec("create table employee(id int primary key, name varchar(20), city int, country int)"); + query.exec("insert into employee values(1, 'Espen', 5000, 47)"); + query.exec("insert into employee values(2, 'Harald', 80000, 49)"); + query.exec("insert into employee values(3, 'Sam', 100, 1)"); + + query.exec("create table city(id int, name varchar(20))"); + query.exec("insert into city values(100, 'San Jose')"); + query.exec("insert into city values(5000, 'Oslo')"); + query.exec("insert into city values(80000, 'Munich')"); + + query.exec("create table country(id int, name varchar(20))"); + query.exec("insert into country values(1, 'USA')"); + query.exec("insert into country values(47, 'Norway')"); + query.exec("insert into country values(49, 'Germany')"); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + createRelationalTables(); + + QSqlRelationalTableModel model; + + initializeModel(&model); + + QTableView *view = createView(QObject::tr("Relational Table Model"), &model); + view->show(); + + return app.exec(); +} diff --git a/examples/sql/relationaltablemodel/relationaltablemodel.pro b/examples/sql/relationaltablemodel/relationaltablemodel.pro new file mode 100644 index 0000000000..38f1e210cc --- /dev/null +++ b/examples/sql/relationaltablemodel/relationaltablemodel.pro @@ -0,0 +1,9 @@ +HEADERS = ../connection.h +SOURCES = relationaltablemodel.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/relationaltablemodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS relationaltablemodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/relationaltablemodel +INSTALLS += target sources diff --git a/examples/sql/sql.pro b/examples/sql/sql.pro new file mode 100644 index 0000000000..6d0d4026f3 --- /dev/null +++ b/examples/sql/sql.pro @@ -0,0 +1,12 @@ +TEMPLATE = subdirs +SUBDIRS = cachedtable \ + drilldown \ + relationaltablemodel \ + sqlwidgetmapper + +!wince*: SUBDIRS += querymodel tablemodel masterdetail + +# install +sources.files = connection.h sql.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/sql +INSTALLS += sources diff --git a/examples/sql/sqlwidgetmapper/main.cpp b/examples/sql/sqlwidgetmapper/main.cpp new file mode 100644 index 0000000000..055ac21f6c --- /dev/null +++ b/examples/sql/sqlwidgetmapper/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro b/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro new file mode 100644 index 0000000000..1881fce1fd --- /dev/null +++ b/examples/sql/sqlwidgetmapper/sqlwidgetmapper.pro @@ -0,0 +1,10 @@ +HEADERS = window.h +SOURCES = main.cpp \ + window.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/sqlwidgetmapper +sources.files = $$SOURCES $$HEADERS *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/sqlwidgetmapper +INSTALLS += target sources diff --git a/examples/sql/sqlwidgetmapper/window.cpp b/examples/sql/sqlwidgetmapper/window.cpp new file mode 100644 index 0000000000..ebfa2cf247 --- /dev/null +++ b/examples/sql/sqlwidgetmapper/window.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "window.h" + +//! [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(); + typeLabel = new QLabel(tr("&Type:")); + typeComboBox = new QComboBox(); + nextButton = new QPushButton(tr("&Next")); + previousButton = new QPushButton(tr("&Previous")); + + nameLabel->setBuddy(nameEdit); + addressLabel->setBuddy(addressEdit); + typeLabel->setBuddy(typeComboBox); +//! [Set up widgets] + +//! [Set up the mapper] + QSqlTableModel *relModel = model->relationModel(typeIndex); + typeComboBox->setModel(relModel); + typeComboBox->setModelColumn(relModel->fieldIndex("description")); + + mapper = new QDataWidgetMapper(this); + mapper->setModel(model); + mapper->setItemDelegate(new QSqlRelationalDelegate(this)); + mapper->addMapping(nameEdit, model->fieldIndex("name")); + mapper->addMapping(addressEdit, model->fieldIndex("address")); + mapper->addMapping(typeComboBox, typeIndex); +//! [Set up the mapper] + +//! [Set up connections and layouts] + connect(previousButton, SIGNAL(clicked()), + mapper, SLOT(toPrevious())); + connect(nextButton, SIGNAL(clicked()), + mapper, SLOT(toNext())); + connect(mapper, SIGNAL(currentIndexChanged(int)), + this, SLOT(updateButtons(int))); + + 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(typeLabel, 3, 0, 1, 1); + layout->addWidget(typeComboBox, 3, 1, 1, 1); + setLayout(layout); + + setWindowTitle(tr("SQL Widget Mapper")); + mapper->toFirst(); +} +//! [Set up connections and layouts] + +//! [Set up the main table] +void Window::setupModel() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + QMessageBox::critical(0, tr("Cannot open database"), + tr("Unable to establish a database connection.\n" + "This example needs SQLite support. Please read " + "the Qt SQL driver documentation for information how " + "to build it."), QMessageBox::Cancel); + return; + } + + QSqlQuery query; + query.exec("create table person (id int primary key, " + "name varchar(20), address varchar(200), typeid int)"); + query.exec("insert into person values(1, 'Alice', " + "'123 Main Street
Market Town
', 101)"); + query.exec("insert into person values(2, 'Bob', " + "'PO Box 32
Mail Handling Service" + "
Service City
', 102)"); + query.exec("insert into person values(3, 'Carol', " + "'The Lighthouse
Remote Island
', 103)"); + query.exec("insert into person values(4, 'Donald', " + "'47338 Park Avenue
Big City
', 101)"); + query.exec("insert into person values(5, 'Emma', " + "'Research Station
Base Camp
" + "Big Mountain
', 103)"); +//! [Set up the main table] + +//! [Set up the address type table] + query.exec("create table addresstype (id int, description varchar(20))"); + query.exec("insert into addresstype values(101, 'Home')"); + query.exec("insert into addresstype values(102, 'Work')"); + query.exec("insert into addresstype values(103, 'Other')"); + + model = new QSqlRelationalTableModel(this); + model->setTable("person"); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + + typeIndex = model->fieldIndex("typeid"); + + model->setRelation(typeIndex, + QSqlRelation("addresstype", "id", "description")); + model->select(); +} +//! [Set up the address type table] + +//! [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/sql/sqlwidgetmapper/window.h b/examples/sql/sqlwidgetmapper/window.h new file mode 100644 index 0000000000..a3628a955b --- /dev/null +++ b/examples/sql/sqlwidgetmapper/window.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QComboBox; +class QDataWidgetMapper; +class QItemSelectionModel; +class QLabel; +class QLineEdit; +class QPushButton; +class QSqlRelationalTableModel; +class QStandardItemModel; +class QStringListModel; +class QTextEdit; +QT_END_NAMESPACE + +//! [Window definition] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +private slots: + void updateButtons(int row); + +private: + void setupModel(); + + QLabel *nameLabel; + QLabel *addressLabel; + QLabel *typeLabel; + QLineEdit *nameEdit; + QTextEdit *addressEdit; + QComboBox *typeComboBox; + QPushButton *nextButton; + QPushButton *previousButton; + + QSqlRelationalTableModel *model; + QItemSelectionModel *selectionModel; + QDataWidgetMapper *mapper; + int typeIndex; +}; +//! [Window definition] + +#endif diff --git a/examples/sql/tablemodel/tablemodel.cpp b/examples/sql/tablemodel/tablemodel.cpp new file mode 100644 index 0000000000..f24c62f52a --- /dev/null +++ b/examples/sql/tablemodel/tablemodel.cpp @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "../connection.h" + +void initializeModel(QSqlTableModel *model) +{ + model->setTable("person"); + model->setEditStrategy(QSqlTableModel::OnManualSubmit); + model->select(); + + model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); + model->setHeaderData(1, Qt::Horizontal, QObject::tr("First name")); + model->setHeaderData(2, Qt::Horizontal, QObject::tr("Last name")); +} + +QTableView *createView(const QString &title, QSqlTableModel *model) +{ + QTableView *view = new QTableView; + view->setModel(model); + view->setWindowTitle(title); + return view; +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + if (!createConnection()) + return 1; + + QSqlTableModel model; + + initializeModel(&model); + + QTableView *view1 = createView(QObject::tr("Table Model (View 1)"), &model); + QTableView *view2 = createView(QObject::tr("Table Model (View 2)"), &model); + + view1->show(); + view2->move(view1->x() + view1->width() + 20, view1->y()); + view2->show(); + + return app.exec(); +} diff --git a/examples/sql/tablemodel/tablemodel.pro b/examples/sql/tablemodel/tablemodel.pro new file mode 100644 index 0000000000..4d099d09cf --- /dev/null +++ b/examples/sql/tablemodel/tablemodel.pro @@ -0,0 +1,9 @@ +HEADERS = ../connection.h +SOURCES = tablemodel.cpp +QT += sql + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/sql/tablemodel +sources.files = $$SOURCES *.h $$RESOURCES $$FORMS tablemodel.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/sql/tablemodel +INSTALLS += target sources -- cgit v1.2.1 From aecb4af947acd5e5c4a59641512269348ca97f07 Mon Sep 17 00:00:00 2001 From: Norwegian Rock Cat Date: Tue, 12 May 2009 13:03:20 +0200 Subject: Replace all instances of "Ctrl+Q" with QKeySequence::Quit. We introduce this new enum, we should be using it. --- examples/sql/masterdetail/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/sql') diff --git a/examples/sql/masterdetail/mainwindow.cpp b/examples/sql/masterdetail/mainwindow.cpp index 67825d6149..5bf4cb9fb1 100644 --- a/examples/sql/masterdetail/mainwindow.cpp +++ b/examples/sql/masterdetail/mainwindow.cpp @@ -362,7 +362,7 @@ void MainWindow::createMenuBar() addAction->setShortcut(tr("Ctrl+A")); deleteAction->setShortcut(tr("Ctrl+D")); - quitAction->setShortcut(tr("Ctrl+Q")); + quitAction->setShortcuts(QKeySequence::Quit); QMenu *fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(addAction); -- cgit v1.2.1