DataSource
#include <bb/data/DataSource>
To link against this class, add the following line to your .pro file: LIBS += -lbbdata
Provides access to data from an external data source.
The DataSource class lets you access data from a local data source, such as a JSON file, XML file or SQL database. It can also be used to access remote JSON or XML data using an HTTP data source URL. You can use this class in C++ code as well as QML, but it is specifically intended to make it easier to load data in QML.
Internally, this class makes use of classes such as JsonDataAccess, SqlDataAccess, XmlDataAccess and QNetworkAccessManager to perform the work.
The properties source, query, type and remote will allow the location and the type of data to be specified and whether the source is remote or local.
Start with an application containing QML code that defines a ListView within a Page, as well as a GroupDataModel that's associated with the ListView.
- In the QML source file:
Add an import statement: "import bb.data 1.0"
Add a DataSource below the definition of your data model as an attached object.
Add code in DataSource.onDataLoaded to connect the DataSource to the data model.
Trigger loading by calling DataSource.load() from Page.onCreationCompleted().
Specify the fields to display in the list items by adding binding code in the ListView.
Here is an example of the QML code required:
import bb.cascades 1.0
import bb.data 1.0
Page {
content: ListView {
id: myListView
dataModel: dataModel
listItemComponents: [
ListItemComponent {
type: "item"
StandardListItem {
title: ListItemData.firstname + " " + ListItemData.lastname
imageSource: "asset:///" + ListItemData.image
description: ListItemData.title
}
}
]
//...
}
attachedObjects: [
GroupDataModel {
id: dataModel
},
DataSource {
id: dataSource
source: "sql/contacts1k.db"
query: "select * from contact order by firstname, lastname"
onDataLoaded: {
dataModel.insertList(data);
}
}
]
onCreationCompleted: { dataSource.load(); }
}
BlackBerry 10.0.0
Public Functions Index
| DataSource (QObject *parent=0) | |
| virtual | ~DataSource () |
| QUrl | source () const |
| Q_SLOT void | setSource (const QUrl &source) |
| QString | query () const |
| Q_SLOT void | setQuery (const QString &query) |
| bb::data::DataSourceType::Type | type () const |
| Q_SLOT void | setType (bb::data::DataSourceType::Type type) |
| bool | remote () const |
| Q_SLOT void | setRemote (bool remote) |
| Q_SLOT void | load () |
| Q_SLOT void | abort () |
Static Public Functions Index
| void | registerQmlTypes () |
Signals Index
| void | sourceChanged (QUrl source) |
| void | queryChanged (QString query) |
| void | typeChanged (bb::data::DataSourceType::Type newType) |
| void | remoteChanged (bool remote) |
| void | dataLoaded (const QVariant &data) |
| void | error (bb::data::DataAccessErrorType::Type errorType, const QString &errorMessage) |
Properties
QUrl
The path to the external data source.
In QML, this path is relative to the QML document in which this DataSource is declared. When setting this property from C++, this path is instead relative to the application working directory.
BlackBerry 10.0.0
QString
The query property contains an SQL query statement or an XML path.
An SQL select could be something like: "select * from contacts"
<contacts version="1.0">
<contact>
<id>1</id>
<title>Sr. Editor</title>
<firstname>Mike</firstname>
<lastname>Chepesky</lastname>
</contact>
<contact>
<id>2</id>
<title>Talent Scout</title>
<firstname>Westlee</firstname>
<lastname>Barichak</lastname>
</contact>
...
</contacts>
BlackBerry 10.0.0
bb::data::DataSourceType::Type
The type of data source.
The value of this property is usually inferred from the query property or the format of the data content associated with source.
Note that for remote data the type cannot be determined from the data content until the data is loaded.
The type is Xml if the query property contains a query path such as "/contacts/contact" or the query path is empty but the source data is in XML format.
The type is Sql if the query property contains an SQL select statement.
The type is Json if the source data is in JSON format.
Otherwise, the type will be "Unknown" unless it is set using this property.
BlackBerry 10.0.0
bool
If true the data is at a remote source otherwise it is local.
The value of this property can usually be inferred from the source. If the source url scheme is "http" then remote is assumed. If remote the data is loaded using network access.
Otherwise, the value for remote will be false unless it is set using this property.
BlackBerry 10.0.0
Public Functions
Constructs a DataSource object with the specified parent.
If the parent is not 0, ownership of this object will be transferred to the parent.
| Parameters | |
|---|---|
| parent |
The parent owner or 0. Optional and will default to 0 if not specified. |
BlackBerry 10.0.0
virtual
Destructor.
BlackBerry 10.0.0
QUrl
Gets the current value of the source property.
The current path to the external data source.
BlackBerry 10.0.0
Q_SLOT void
Sets a new path to the external data source.
| Parameters | |
|---|---|
| source |
The new path of the external data source. |
BlackBerry 10.0.0
QString
Gets the current value of the query property.
The query to use with this data source.
BlackBerry 10.0.0
Q_SLOT void
Sets a new query to use with this data source.
| Parameters | |
|---|---|
| query |
The new query to use. |
BlackBerry 10.0.0
bb::data::DataSourceType::Type
Gets the current value of the type property.
The type of data for the data source based on the DataSourceType::Type enumeration.
BlackBerry 10.0.0
Q_SLOT void
Sets a new type to use with this SQL data source.
Typically, the type is inferred from other information and does not need to be explicitly set when you use this class. See the type property above for the rules.
| Parameters | |
|---|---|
| type |
The data source type. |
BlackBerry 10.0.0
bool
Gets the current value of the remote property.
True if the data source is remote and false if it is local.
BlackBerry 10.0.0
Q_SLOT void
Sets a new value for the remote property.
Normally the remote value is inferred from the source url and does not need to be explicitly set by the user of this class. See the remote property above for the rules.
| Parameters | |
|---|---|
| remote |
True if the data source is remote and false if it is local. |
BlackBerry 10.0.0
Q_SLOT void
Loads the data from the data source.
BlackBerry 10.0.0
Q_SLOT void
Abort the current load operation if a network download is in progress.
This will not abort synchronous load operations such as loading from a local file. If a load operation is aborted an error signal will be omitted.
Static Public Functions
void
Deprecated.
Registers DataSource and related classes for use in QML.
No longer needed as a QML plugin is used to do the registration instead.
BlackBerry 10.0.0
Signals
void
Emitted when the source property changes.
| Parameters | |
|---|---|
| source |
The new path of the external data source. |
BlackBerry 10.0.0
void
Emitted when the type property changes.
Due to a workaround for a Qt Core issue with accessing enumerations from QML, the argument of this signal doesn't follow naming convention for signals, in which the signal arguments are typically named to match the associated property's name. To avoid runtime errors, use the object's property to access current property value instead of the signal argument (use type instead of newType).
| Parameters | |
|---|---|
| newType |
The new type to use. |
BlackBerry 10.0.0
void
Emitted when the remote property changes.
| Parameters | |
|---|---|
| remote |
The new value for remote property. |
BlackBerry 10.0.0
void
Emitted when new data is loaded.
Depending on the type of data and how it's loaded, the data passed by this signal can be either a QVariantList or a QVariantMap.
For SQL data, this signal always passes a QVariantList containing a QVariantMap for each item.
For JSON data, this signal passes either a QVariantList or a QVariantMap depending on the top-most JSON type used in the input data (QVariantList if it's an array, and QVariantMap if it's an object).
If you don't specify a queryPath, the data will be a QVariantMap representing the entire document.
If you do specify a queryPath, and the path refers to a non-repeating XML element, the data will be a QVariantMap containing the single item.
If you do specify a queryPath, and the path refers to a repeating XML element, the data will be a QVariantList (if the actual data contains more than one repetition) or a QVariantMap (if the actual data contains only one repetitiion).
| Parameters | |
|---|---|
| data |
The new data that was loaded. |
BlackBerry 10.0.0
void
Emitted when data failed to load and generated an error.
| Parameters | |
|---|---|
| errorType |
The type of error that was generated. |
| errorMessage |
The detailed error message. |
BlackBerry 10.0.0