View on GitHub Sample viewer app

Take a screenshot of the map.

Use case

GIS users may want to export a screenshot of a map to enable sharing as an image or printing.

How to use the sample

Pan and zoom to find an interesting location. Press Take screenshot, and a screenshot image will display over the map. Note that there may be a small delay if the map is still rendering when you push the button.

How it works

  1. The exportImage function is executed on the MapView.
  2. Once the asynchronous task completes, a QImage is returned.
  3. QML cannot directly render a QImage. Instead, a QML Image Provider must be created for generating an in-memory URL to the returned QImage. This is done with the following steps:
  • Subclass QQuickImageProvider.
  • Reimplement the requestImage function.
  • Add an image provider to the QQmlEngine instance using QQmlEngine::addImageProvider.
  • Create a Q_PROPERTY that returns the image URL from the Image Provider.
  • Bind the QML Image to the image URL property. When this property changes, it will automatically trigger the requestImage function and return the URL to the image.

Relevant API

  • GeoView::exportImage

Tags

capture, export, image, print, screen capture, screenshot, share, shot

Sample Code

MapImageProvider.cpp MapImageProvider.cpp MapImageProvider.h TakeScreenshot.cpp TakeScreenshot.h TakeScreenshot.qml main.cpp
// [Legal]
// Copyright 2018 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]
// sample headers
#include "MapImageProvider.h"
// Qt headers
#include <QQuickImageProvider>
MapImageProvider::MapImageProvider() :
QQuickImageProvider(QQuickImageProvider::Image)
{
}
// reimplemented function for QML to request Images from the provider
QImage MapImageProvider::requestImage(const QString& id, QSize* size, const QSize &requestedSize)
{
Q_UNUSED(size)
Q_UNUSED(requestedSize)
return m_images[id];
}
// helper to add images to the the provider
void MapImageProvider::addImage(const QString& id, const QImage& img)
{
m_images[id] = img;
}
// static function to return the image provider id
QString MapImageProvider::imageProviderId()
{
return QStringLiteral("mapimageprovider");
}