File: u1db.tutorial.qdoc

package info (click to toggle)
u1db-qt 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 688 kB
  • sloc: cpp: 2,089; python: 60; sql: 40; makefile: 19; sh: 14; xml: 14
file content (310 lines) | stat: -rw-r--r-- 7,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*!

\page tutorial.html

\title Tutorial
  
  
\section1 1.0.0 Overview of U1Db-Qt  

U1Db-Qt is a QML plugin written in Qt C++. It provides declarative, easy to use, local data storage for QML applications, and in the future will also include remote sync capabilities.

U1Db-Qt is based on a procedural implementation in Python, but has been designed and developed from the start with declarative programming in mind. 

While U1Db-Qt makes use of SQLite in the back end, and relies heavily on JSON, these are largely invisible to a QML developer who makes use of the plugin. However, because U1Db-Qt does rely on both technologies it is possible to easily debug applications using existing programs. For example, a developer could make use of SQLiteBrowser to read U1Db database files, which contain human readable JSON objects representing content derived from a QML application. 

\section2 1.0.1 How to Make U1Db-Qt Available to a QML Application

Here is an example import statement:

\code
import U1db 1.0 as U1db 				
\endcode
 
\section1 2.0.0 Database Element

In U1Db-Qt, the Database element is the the central figure that works in conjunction with Document elements, and in the future indexing (currently under development), and querying (currently under development) elements.	 It is the Database element that defines the location of the data repository.

A Database element is also a valuable tool for cases where specific data from a repository needs to be retrieved quickly, without indexing or querying ahead of time.	The 'contents' object associated with the Database element can be used together with base items such as TextField or TextArea and model-based items like ListView and \l {Standard}{ListItems.Standard}.
   
\section2 2.0.1 Database Properties

\code
QString path
QString error   
\endcode   
    
\section2 2.1.0 Creating a Database
    
A Database is very simple to create. It requires nothing more than an id and a path where the file will be created. 
    
\section3 2.1.1 Example of Creating a Database

\code
import QtQuick 2.0
import U1db 1.0 as U1db 

Item{   
	U1db.Database {
		id: aDatabase
 		path: "aU1DbDatabase"
	}
}	 
\endcode
        
\section1 3.0.0 Document Element

The Document element is the primary vehicle for entering data into a repository, and can be helpful in some cases for getting data out as well. Indexing and querying would normally provide more robust functionality for extracting data from a repository, but in the case of U1Db-Qt both are still under development at the time of writing (and therefore not available for developers to use). 

However, the Document element together with Database can still help developers in many common situations, and will continue to be very useful even when the indexing and querying functionality becomes available. When a developer wants unfiltered results from a database, or the cost of working with unfiltered results is reasonable, the Document+Database combination is fast and easy to use. In quite a number of use cases this may be exactly what a developer needs.

\section2 3.0.1 Document Properties

\code
U1db.Database database 
QString docId 
bool create 
QVariant defaults 
QVariant contents   
\endcode

\section2 3.1.0 Creating a Basic Document

A Document declaration should contain at least a unique 'docId' and 'database', but these alone won't do anything by themselves. Additionally, although the 'id' property is not mandatory, this property will need to be set in order to more easily reference it from elsewhere in the program (e.g. within a function call). 

\section3 3.1.0.1 Example of Creating a Basic Document

\code
import QtQuick 2.0
import U1db 1.0 as U1db 
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
	
	U1db.Database {
		id: aDatabase
		path: "aU1DbDatabase"
	}
			       
}
\endcode

\section2 3.1.1 Creating a Document At Runtime

A Document can be declared at runtime, and default data entered into the repository. This requires the same properties to be set as in the basic example ('id', 'database' and 'docId'), plus setting 'create' (to true) and a 'default' string.  
	
\section3 3.1.1.1 Example of Creating a Document At Runtime

\code
import QtQuick 2.0
import U1db 1.0 as U1db 
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
	
	U1db.Database {
		id: aDatabase
		path: "aU1DbDatabase"
	}
			       
	U1db.Document {
		id: aDocument
		database: aDatabase
		docId: 'helloworld'
		create: true
		defaults: { "hello": "Hello World!" }
	}

}
\endcode
 
\section2 3.1.2 Creating a Document Dynamically

Creating a Document in a dynamic fashion is the most common way of putting data into a data repository based on UI activity (e.g. when a user presses a button). 

\section3 3.1.2.1 Example 1 of Creating a Document Dynamically

Another way of creating a new Document is to copy an existing Document:

\code
import QtQuick 2.0
import U1db 1.0 as U1db
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
	
	U1db.Database {
		id: aDatabase
 		path: "aU1DbDatabase"
	}

	U1db.Document {
		id: aDocument
	 	database: aDatabase
 		docId: 'helloworld'
	}

	function someFunction() {
		var tempDocument = {}
		tempDocument = aDocument
	}

}
\endcode

\section3 3.1.2.2 Example 2 of Creating a Document Dynamically

One way of creating a new Document dynamically is to make use of Qt.createQmlObject:

\code
import QtQuick 2.0
import U1db 1.0 as U1db 
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
	
	U1db.Database {

		id: aDatabase
		path: "aU1DbDatabase"
		Component.onCompleted: { newDocumentObject() }
	
		function newDocumentObject() {

			var qmlString = "import QtQuick 2.0; import U1db 1.0 as U1db; U1db.Document {id: aDcoument; database: aDatabase; docId: 'helloworld'; create: true; defaults: { 'hello': 'Hello New Document!' }}"

			Qt.createQmlObject(qmlString, aDatabase);

		}

	}

}
\endcode




\section1 4.0.0 U1Db-Qt and QML Elements and Models

\section2 4.1.0 U1Db-Qt and Standard Elements

\section3 4.1.1 U1Db-Qt and TextField & TextArea

\section4 4.1.2 Example of Using U1Db-Qt with Standard Elements

\code
import QtQuick 2.0
import U1db 1.0 as U1db
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
	
	function getContent(fieldName){
	
		var tempContents = {};
		tempContents = aDocument.contents
		return tempContents[fieldName]

	}
 
	U1db.Database {
		id: aDatabase
 		path: "aU1DbDatabase"
	}

	U1db.Document {
		id: aDocument
	 	database: aDatabase
 		docId: 'helloworld'
 		create: true
 		defaults: { "hello": "Hello World 1!" }
	}


	TextField {
		id: addressBar
		width: units.gu(45)
		text: getContent('hello')
	}

}
\endcode
\section2 4.2.0 U1Db-Qt and Model-Based Elements

\section3 4.2.1 U1Db-Qt and ListView

\section4 4.2.2 Example of Using U1Db-Qt with Model-Based Elements 

\code
import QtQuick 2.0
import U1db 1.0 as U1db
import Lomiri.Components 0.1

Item{

	width: units.gu(45)
	height: units.gu(80)
 
	U1db.Database {
		id: aDatabase
 		path: "aU1DbDatabase"
	}

	U1db.Document {
		id: aDocument1
	 	database: aDatabase
 		docId: 'helloworld'
 		create: true
 		defaults: { "hello": "Hello World 1!" }
	}

	U1db.Document {
		id: aDocument2
 		database: aDatabase
 		docId: 'helloworld'
 		create: true
 		defaults: { "hello": "Hello World 2!" }
	}

	ListView {

		model: aDatabase
		width: units.gu(45)
		height: units.gu(80)
		
		delegate: Text {
		x: 66; y: 77
		text: contents.hello
		}

	}

}

\endcode

\section1 5.0.0 Resources

\section2 5.0.1 Examples   

One can find several examples of U1Db-Qt either in the subdirectory "examples" or from the following url:

https://gitlab.com/ubports/development/core/u1db-qt/-/tree/main/examples

*/