1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "bookwindow.h"
52#include "bookdelegate.h"
53#include "initdb.h"
54
55#include <QtSql>
56
57BookWindow::BookWindow()
58{
59 ui.setupUi(this);
60
61 if (!QSqlDatabase::drivers().contains(str: "QSQLITE"))
62 QMessageBox::critical(
63 parent: this,
64 title: "Unable to load database",
65 text: "This demo needs the SQLITE driver"
66 );
67
68 // Initialize the database:
69 QSqlError err = initDb();
70 if (err.type() != QSqlError::NoError) {
71 showError(err);
72 return;
73 }
74
75 // Create the data model:
76 model = new QSqlRelationalTableModel(ui.bookTable);
77 model->setEditStrategy(QSqlTableModel::OnManualSubmit);
78 model->setTable("books");
79
80 // Remember the indexes of the columns:
81 authorIdx = model->fieldIndex(fieldName: "author");
82 genreIdx = model->fieldIndex(fieldName: "genre");
83
84 // Set the relations to the other database tables:
85 model->setRelation(column: authorIdx, relation: QSqlRelation("authors", "id", "name"));
86 model->setRelation(column: genreIdx, relation: QSqlRelation("genres", "id", "name"));
87
88 // Set the localized header captions:
89 model->setHeaderData(section: authorIdx, orientation: Qt::Horizontal, value: tr(s: "Author Name"));
90 model->setHeaderData(section: genreIdx, orientation: Qt::Horizontal, value: tr(s: "Genre"));
91 model->setHeaderData(section: model->fieldIndex(fieldName: "title"),
92 orientation: Qt::Horizontal, value: tr(s: "Title"));
93 model->setHeaderData(section: model->fieldIndex(fieldName: "year"), orientation: Qt::Horizontal, value: tr(s: "Year"));
94 model->setHeaderData(section: model->fieldIndex(fieldName: "rating"),
95 orientation: Qt::Horizontal, value: tr(s: "Rating"));
96
97 // Populate the model:
98 if (!model->select()) {
99 showError(err: model->lastError());
100 return;
101 }
102
103 // Set the model and hide the ID column:
104 ui.bookTable->setModel(model);
105 ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
106 ui.bookTable->setColumnHidden(column: model->fieldIndex(fieldName: "id"), hide: true);
107 ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
108
109 // Initialize the Author combo box:
110 ui.authorEdit->setModel(model->relationModel(column: authorIdx));
111 ui.authorEdit->setModelColumn(
112 model->relationModel(column: authorIdx)->fieldIndex(fieldName: "name"));
113
114 ui.genreEdit->setModel(model->relationModel(column: genreIdx));
115 ui.genreEdit->setModelColumn(
116 model->relationModel(column: genreIdx)->fieldIndex(fieldName: "name"));
117
118 // Lock and prohibit resizing of the width of the rating column:
119 ui.bookTable->horizontalHeader()->setSectionResizeMode(
120 logicalIndex: model->fieldIndex(fieldName: "rating"),
121 mode: QHeaderView::ResizeToContents);
122
123 QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
124 mapper->setModel(model);
125 mapper->setItemDelegate(new BookDelegate(this));
126 mapper->addMapping(widget: ui.titleEdit, section: model->fieldIndex(fieldName: "title"));
127 mapper->addMapping(widget: ui.yearEdit, section: model->fieldIndex(fieldName: "year"));
128 mapper->addMapping(widget: ui.authorEdit, section: authorIdx);
129 mapper->addMapping(widget: ui.genreEdit, section: genreIdx);
130 mapper->addMapping(widget: ui.ratingEdit, section: model->fieldIndex(fieldName: "rating"));
131
132 connect(sender: ui.bookTable->selectionModel(),
133 signal: &QItemSelectionModel::currentRowChanged,
134 receiver: mapper,
135 slot: &QDataWidgetMapper::setCurrentModelIndex
136 );
137
138 ui.bookTable->setCurrentIndex(model->index(row: 0, column: 0));
139 createMenuBar();
140}
141
142void BookWindow::showError(const QSqlError &err)
143{
144 QMessageBox::critical(parent: this, title: "Unable to initialize Database",
145 text: "Error initializing database: " + err.text());
146}
147
148void BookWindow::createMenuBar()
149{
150 QAction *quitAction = new QAction(tr(s: "&Quit"), this);
151 QAction *aboutAction = new QAction(tr(s: "&About"), this);
152 QAction *aboutQtAction = new QAction(tr(s: "&About Qt"), this);
153
154 QMenu *fileMenu = menuBar()->addMenu(title: tr(s: "&File"));
155 fileMenu->addAction(action: quitAction);
156
157 QMenu *helpMenu = menuBar()->addMenu(title: tr(s: "&Help"));
158 helpMenu->addAction(action: aboutAction);
159 helpMenu->addAction(action: aboutQtAction);
160
161 connect(sender: quitAction, signal: &QAction::triggered, receiver: this, slot: &BookWindow::close);
162 connect(sender: aboutAction, signal: &QAction::triggered, receiver: this, slot: &BookWindow::about);
163 connect(sender: aboutQtAction, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt);
164}
165
166void BookWindow::about()
167{
168 QMessageBox::about(parent: this, title: tr(s: "About Books"),
169 text: tr(s: "<p>The <b>Books</b> example shows how to use Qt SQL classes "
170 "with a model/view framework."));
171}
172

source code of qtbase/examples/sql/books/bookwindow.cpp