1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example sql/cachedtable
|
---|
30 | \title Cached Table Example
|
---|
31 |
|
---|
32 | The Cached Table example shows how a table view can be used to access a database,
|
---|
33 | caching any changes to the data until the user explicitly submits them using a
|
---|
34 | push button.
|
---|
35 |
|
---|
36 | \image cachedtable-example.png
|
---|
37 |
|
---|
38 | The example consists of a single class, \c TableEditor, which is a
|
---|
39 | custom dialog widget that allows the user to modify data stored in
|
---|
40 | a database. We will first review the class definiton and how to
|
---|
41 | use the class, then we will take a look at the implementation.
|
---|
42 |
|
---|
43 | \section1 TableEditor Class Definition
|
---|
44 |
|
---|
45 | The \c TableEditor class inherits QDialog making the table editor
|
---|
46 | widget a top-level dialog window.
|
---|
47 |
|
---|
48 | \snippet examples/sql/cachedtable/tableeditor.h 0
|
---|
49 |
|
---|
50 | The \c TableEditor constructor takes two arguments: The first is a
|
---|
51 | pointer to the parent widget and is passed on to the base class
|
---|
52 | constructor. The other is a reference to the database table the \c
|
---|
53 | TableEditor object will operate on.
|
---|
54 |
|
---|
55 | Note the QSqlTableModel variable declaration: As we will see in
|
---|
56 | this example, the QSqlTableModel class can be used to provide data
|
---|
57 | to view classes such as QTableView. The QSqlTableModel class
|
---|
58 | provides an editable data model making it possible to read and
|
---|
59 | write database records from a single table. It is build on top of
|
---|
60 | the lower-level QSqlQuery class which provides means of executing
|
---|
61 | and manipulating SQL statements.
|
---|
62 |
|
---|
63 | We are also going to show how a table view can be used to cache
|
---|
64 | any changes to the data until the user explicitly requests to
|
---|
65 | submit them. For that reason we need to declare a \c submit() slot
|
---|
66 | in additon to the model and the editor's buttons.
|
---|
67 |
|
---|
68 | \table 100%
|
---|
69 | \header \o Connecting to a Database
|
---|
70 | \row
|
---|
71 | \o
|
---|
72 |
|
---|
73 | Before we can use the \c TableEditor class, we must create a
|
---|
74 | connection to the database containing the table we want to edit:
|
---|
75 |
|
---|
76 | \snippet examples/sql/cachedtable/main.cpp 0
|
---|
77 |
|
---|
78 | The \c createConnection() function is a helper function provided
|
---|
|
---|