Sample application using C++ and QtQuick
The sample application HelloWorldQtQuick demonstrates how to use the QtQuickMapControl in a QtQuick application.
Install Qt Visual Studio Add-in and use the "Open Qt project file (.pro)" item in the Qt menu to open the sample project in Visual Studio.
The sample can also be built and run on Linux, using the Qt qmake command to generate a makefile from the project file, followed by make or by opening the sample project file in QtCreator.
When using QtCreator the sample can also be built for Android on Windows and Linux using cmake and the CMakeLists.txt project file.
For instructions on how to build and deploy a Qt application on Linux/ARM, see Deploying Carmenta Engine 5 applications on Linux/ARM.
When using the QtQuickMapControl on Linux, the Qt libraries have some requirements regarding the use of OpenGL or OpenGL ES, see the Graphics renderers supported by Carmenta Engine page for more information.
Using the MapControl in a QtQuick application
QtQuick uses a declarative language called QML, similar to a combination of HTML/CSS and Javascript, to define the user interface. Before the Carmenta Engine MapControl can be used from QML it must be registered with the QML runtime. The sample application does this in main.cpp and then launches the GUI defined in the file main.qml.
The Controller class
The sample application also registers a C++ Controller class with the QML runtime to show how C++ code can be called from Javascript in QML, for example to zoom in/out.
The Controller class is registered as a singleton (it can not be instantiated from QML but you can call methods on the singleton instance) and provides three methods that can be called from Javascript in the QML file:
// Zoom in.
Q_INVOKABLE void zoomIn();
// Zoom out.
Q_INVOKABLE void zoomOut();
// Load a map configuration and display the first view.
Q_INVOKABLE void loadConfiguration(const QString& fileName);
Replace the contents of the main.qml file with the following QML and the rebuild the application to see how this works:
import QtQuick 2.9
import QtQuick.Window 2.2
import com.carmenta 1.0
Window {
width: 640
height: 480
visible: true
title: qsTr("HelloWorldQtQuick")
MapControl {
objectName: "mapControl"
anchors.fill: parent
}
// Zoom in button
Rectangle {
x: 50
y: 50
width: 100; height: 100
color: "#aadddddd"
Text {
anchors.centerIn: parent
text: "Zoom in"
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onClicked: { Controller.zoomIn(); }
}
}
// Zoom out button
Rectangle {
x: 160
y: 50
width: 100; height: 100
color: "#aadddddd"
Text {
anchors.centerIn: parent
text: "Zoom out"
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onClicked: { Controller.zoomOut(); }
}
}
}
The official Qt documentation provides more information on how to use QtQuick to implement the GUI of an application that calls business logic implemented in C++.