Skip To Content

Add a map to your app

In this topic

To enhance your development experience, custom project templates are added to the Qt Creator integrated development environment (IDE). A project template includes all the files and property settings necessary to begin an ArcGIS Runtime application project. You can create new applications using one of these project templates and extend these applications with your own business logic.

The ArcGIS Runtime SDK for Qt includes the following project templates:

  • ArcGIS Runtime 10.2.6 Qt Quick Application
  • ArcGIS Runtime 10.2.6 Qt Widgets Application

ArcGIS Runtime has supported Qt Widgets applications written in C++ since its first release. ArcGIS Runtime 10.2.5 introduced support for developing Qt Quick applications using the QML language.

Get started with the Qt Widgets Application template

This type of project is programmed primarily with the C++ programming language. The ArcGIS Runtime 10.2.6 Qt C++ Application template demonstrates how to add online and local service layers to your application.

Here are the steps to get started.

  • In Qt Creator, click File > New File or Project. The New Project dialog box appears with the list of installed project templates.

  • In the Projects list, browse to ArcGIS and you will see the ArcGIS Runtime 10.2.6 Qt Widgets Application template. Highlight this template and click Choose.
  • In the Introduction and Project Location dialog, name the project MyFirstQtWidgetsApp.

  • In the Kit Selection dialog, select the Qt kit(s) to use to build your project.

  • Click Next to bring up the Project Management dialog.

  • Click the button to close the dialog.
  • Accept the defaults on the Target Setup and click the button to close the dialog. For details about configuring projects with kits, see the Qt Creator Documentation. The template consists of a .pro file, header file, and .cpp files.
  • Examine the MyFirstQtWidgetsApp.h and MyFirstQtWidgetsApp.cpp code in the created project and notice the following:
    • A map has been created and added as the content to the widget.
    • There are a number of local and online service layers commented out.
  • In the file MyFirstQtWidgetsApp.cpp, uncomment #include "ArcGISTiledMapServiceLayer.h". In the file MyFirstQtWidgetsApp.h, uncomment the line that declares the member variable m_tiledServiceLayer.
  • In the file MyFirstQtWidgetsApp.cpp uncomment the two lines of code referring to ArcGIS Online Tiled Basemap Layer. The ArcGIS Online Tiled Basemap Layer has a URL property set to the world topographic map. This map is available for users online. It is added to the map created above.
  • Build and run the application. The map application will appear.

Note:

If you are using Red Hat Linux 6 and the Esri-provided build of the Qt SDK, you must copy the plugins/platform folder from the Qt SDK installation folder to the folder containing the executable before running your app for the first time from Qt Creator.

Work with local data in your Qt Widgets Application

  • To customize the Qt Widgets application to use local data rather than data from an online service, edit the code provided. To work quickly with local data, uncomment the code in the files MyFirstQtWidgetsApp.h and MyFirstQtWidgetsApp.cpp to add the local layers required. Local tiled basemap layers are often used as a way to help users orient themselves or navigate the map. Typically any operational layers are placed on top of basemap layers. To use the local tiled basemap layer provided with the SDK samples, leave the code as is to automatically find the path to the sample data or provide a relative or absolute path to the data. The code should look similar to that shown below:
    // Local Tiled Basemap Layer using: sdk/samples/data/tpks/Topographic.tpk
    QString tiledBaseMapLayer = pathSampleData + "tpks" + QDir::separator() + "Topographic.tpk";
    m_tiledLayer = new EsriRuntimeQt::ArcGISLocalTiledLayer(tiledBaseMapLayer, this);
    m_map->addLayer(m_tiledLayer);
  • Operational data can be added to the map using Local Dynamic Layers. To add a Local Dynamic Layer, uncomment the corresponding code in the files MyFirstQtWidgetsApp.h and MyFirstQtWidgetsApp.cpp. Leave the code as is to automatically find the path to the sample data or provide a relative or absolute path to the data.
    //Local Dynamic Layer using: sdk/samples/data/mpks/USCitiesStates.mpk
    QString dataPath = pathSampleData + "mpks" + QDir::separator() + "USCitiesStates.mpk";
    m_localMapService = EsriRuntimeQt::LocalMapService(dataPath);
      
    connect(&m_localMapService, SIGNAL(serviceCreationSuccess(QString,QString)), this, SLOT(onLocalServiceCreationSuccess(QString,QString)));
    connect(&m_localMapService, SIGNAL(serviceCreationFailure(QString)), this, SLOT(onLocalServiceCreationFailure(QString)));
    m_localMapService.start();
  • You will also need to uncomment the code for the onLocalServiceCreationSuccess slot.
    void MyFirstQtWidgetsApp::onLocalServiceCreationSuccess(const QString& url, const QString& name)
    {
      Q_UNUSED(url);
      ...
    
       // create the ArcGISDynamicMapServiceLayer using the LocalMapService's url
       m_dynamicLocalServiceLayer = new EsriRuntimeQt::ArcGISDynamicMapServiceLayer(m_localMapService.urlMapService(), this);
       m_map->addLayer(m_dynamicLocalServiceLayer);
    
    }
  • Uncomment the code for the onLocalServiceCreationFailure slot.
    void MyFirstQtWidgetsApp::onLocalServiceCreationFailure(const QString& name)
    {
      qWarning() << name + " failed to start";
      qWarning() << m_localMapService.error().what();
    }
  • Local Feature Layers are a special type of Graphics layer that allow you to display graphic features that consist of geometry and attributes. To add a Local Feature Layer, modify the code in the files MyFirstQtWidgetsApp.h and MyFirstQtWidgetsApp.cpp to look like the code below. Leave the code as is to automatically find the path to the sample data or provide a relative or absolute path to the data.

MyFirstQtWidgetsApp.cpp—The added code should appear as follows:

//Feature service using: sdk/samples/data/mpks/USCitiesStates.mpk
QString localFeatureService = pathSampleData + "mpks" + QDir::separator() + "USCitiesStates.mpk";
m_localFeatureService = EsriRuntimeQt::LocalFeatureService(localFeatureService);
m_localFeatureService.setMaximumRecords(3000);
    
//connect to signal that is emitted when the service is created successfully
connect(&m_localFeatureService, SIGNAL(serviceCreationSuccess(const QString&, const QString&)),
        this, SLOT(onLocalServiceCreationSuccess(const QString&, const QString&)));
    
//connect to signal that is emitted when the service failed
connect(&m_localFeatureService, SIGNAL(serviceCreationFailure(const QString&)),
        this, SLOT(onLocalServiceCreationFailure(const QString&)));
    
//start the service
m_localFeatureService.start();
...

// The methods to handle the success or failure of the Local Service...

void MyFirstQtWidgetsApp::onLocalServiceCreationSuccess(const QString& url, const QString& name)
{
  Q_UNUSED(url);
  Q_UNUSED(name);

  EsriRuntimeQt::FeatureServiceInfo featureServiceInfo = m_localFeatureService.featureServiceInfo();
  QList<EsriRuntimeQt::FeatureLayerInfo*> layerInfoList= featureServiceInfo.layers("Cities");

  if(!layerInfoList.isEmpty())
    serviceUrl = layerInfoList.at(0)->url();

  if (serviceUrl.isEmpty())
  {
    qWarning() << "The required layer does not exist";
    return;
  }

  m_localFeatureLayer = new EsriRuntimeQt::ArcGISFeatureLayer(serviceUrl, this);
  m_map->addLayer(m_localFeatureLayer);

...

}

void MyFirstQtWidgetsApp::onLocalServiceCreationFailure(const QString& name)
{
  qWarning() << name + " failed to start";
  qWarning() << m_localFeatureService.error().what();

...
}