Enginio C++ Examples - Cloud Address Book
The Cloud Address Book demonstrates how to use sorting, filtering, and the full text search functionality.
This example explains how to use the full text search feature of Enginio, and how to sort and filter data showed from the EnginioModel. A simple addressbook-like application will be created to illustrate this. This example doesn't cover security or multi-user management. For such topics, please refer to the Social Todo example.
[Missing image cloudaddressbook-example.png]
Preconditions
To start the example, a backend id and a backend secret have to be provided for an existing and configured Enginio backend. The backend can be created using the dashboard, where the Cloud Address Book preconfigured backend can be chosen.
Backend Description
We recommend to use preconfigured backend, because it contains already all data and structures that are needed to run these examples, but it is not difficult to create the backend from scratch too. The backend should contain one custom object type objects.addressbook
having properties:
- firstName
- lastName
- phone
- address
All properties are of string
type and have to be indexed, because only indexed properties will be searched by the full text search.
Application Design
The application's ui mainly consists of a table showing all addresses and a text filed where a query can be typed. A user should be able to sort addresses or highlight an address containing a specified phrase.
Implementation
From a developer point of view the application consists of a few simple components:
- EnginioClient which encapsulates all information needed to keep the connection to the backend
- EnginioModel which queries all addresses
- QSortFilterProxyModel which sorts the data
- QTableView which shows the data
First we need to establish a connection to the Enginio service. We need to specify the backend id as well as backend secret.
The second step is to create EnginioModel which queries all data from the backend. The query is quite simple, it specifies an object type of which all objects need to be returned.
EnginioModel can sort or filter data only initially, which means that, for example, a newly added item will not be placed correctly. To solve the problem QSortFilterProxyModel has to be used.
Now is a time to look deeper into EngnioModel. EnginioModel should define data roles.
and as always the data() and setData() functions have to be overridden to provide Qt::DisplayRole in such a way as to nicely cooperate with QTableView.
QTableView requires the specification of columns headers, so that they can be shown in the user interface:
Integration of the full text search is the last step in this tutorial. The goal is to highlight items that contain a given phrase. The highlighting is done by data(), which returns a bold font for Qt::FontRole if an item is matching the search query. For reasons of simplicity, this example assumes that the matching items count is not big and can be kept in a QSet, which would be recreated on each search.
To do a full text search, a JSON query needs to be constructed:
The query contains objectTypes
property (note the 's' at the end) which is an array of all object types which have to be searched. In this case, it is only one type: objects.addressbook
. Next the search
property has to be specified. It is an object that is required to have a phrase
property, containing the phrase to search for. phrase that has to be found. Please use the wildcard *
in the search criteria, otherwise only full words will be found. To avoid substrings, for example in an object id
, which is not visible for a user, the search is limited to a selected array of properties
. When the search query is constructed, it is enough to call fullTextSearch():
The result will be delivered to the searchResultsArrived
slot. All objects ids found will be gathered in a markedItems
set.