[Qgis-user] Tool to copy the content of the map canvas to the clipboard

PIERRE Sylvain sylvain.pierre at bas-rhin.fr
Thu Dec 22 00:21:37 PST 2016


Hi,

For a standalone Pyqgis App I needed such function, so here it is (implemented for a QgsMapTool) :

from PyQt4 import QtCore

from PyQt4.QtGui import QPixmap , QApplication

# QGIS bindings for mapping functions
from qgis.gui import QgsMapTool

import tempfile

try:
   _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
   _fromUtf8 = lambda s: s

class SnapshotTool(QgsMapTool):
    ''' Provide a tool copy map canvas to clipboard '''
    def __init__(self, parent):
        QgsMapTool.__init__(self, parent.canvas)
        self.mainwindow = parent
        self.canvas = parent.canvas
#-------------------------------------------------------------------------------
    ''' Overridden QgsMapTool Events '''
#-------------------------------------------------------------------------------
    def canvasPressEvent(self, event):
        ''' Get point and transform to map coordinates '''
        point = event.pos()
        self.transform = self.mainwindow.canvas.getCoordinateTransform()
        # returns a QgsPoint object in map coordinates
        qgsPoint = self.transform.toMapCoordinates(point.x(), point.y())

        self.copyPaste2Clipboard(point, qgsPoint)


    def canvasReleaseEvent(self, event):
        pass

    def copyPaste2Clipboard(self, point, qgsPoint):
        ''' Get canvas and copy to clipboard '''
        # debugging
        print "copyPaste2Clipboard()"
        pxmap = QPixmap.grabWidget(self.canvas)
        QApplication.clipboard().setPixmap(pxmap)


→  Sylvain PIERRE
         Chef de projet système d’information
         Direction des Systèmes d’Information
         Service Projets et Applications Numériques
       Conseil Départemental du Bas-Rhin

[cid:image001.jpg at 01D25C34.CB1FA830]<http://www.bas-rhin.fr/>

         Hôtel du Département
         1 place du Quartier Blanc 67964 Strasbourg Cedex 9
         Tél : 03 88 76 68 88 - mobile :
         Mobile : 06 30 96 31 76
         Email : sylvain.pierre at bas-rhin.fr<mailto:sylvain.pierre at bas-rhin.fr>
         www.bas-rhin.fr<http://www.bas-rhin.fr>


De : Qgis-user [mailto:qgis-user-bounces at lists.osgeo.org] De la part de Larry Shaffer
Envoyé : mardi 20 décembre 2016 21:44
À : DelazJ
Cc : qgis-user at lists.osgeo.org
Objet : Re: [Qgis-user] Tool to copy the content of the map canvas to the clipboard

Hi,

On Tue, Dec 20, 2016 at 11:02 AM, DelazJ <delazj at gmail.com<mailto:delazj at gmail.com>> wrote:
Hi,
2016-12-20 18:23 GMT+01:00 Bo Victor Thomsen <bo.victor.thomsen at gmail.com<mailto:bo.victor.thomsen at gmail.com>>:
Hi List-members -

Is there in Qgis a function / button / menu-item / short-cut / plugin to make a simple bitmap copy of the current content of the map-canvas and save it to the clipboard ? My users need this to make a copy of the map and paste into MS-Word or other software.

While not part of the QGIS app, one could use this PyQGIS snippet to do so (tested quickly on macOS 10.11.6 under QGIS 2.14) from the PyQGIS console:

from PyQt4.QtGui import *
QApplication.clipboard().setImage(QImage(QPixmap.grabWidget(iface.mapCanvas())))

This avoids having to re-render the QGraphicsView (QgsMapCanvas) to a paint device, like an image. Code can be wrapped in a plugin, then have some key bindings applied (would need some more PyQt).

Larry Shaffer
Dakota Cartography
Black Hills, South Dakota

I really need the functionality for my users and I can't seem to find it. The closest I've come is the "save as png" menu-item in the project menu. But it's to cumbersome to use in this case.

Afaics, there's no "save as png" option but "save as image" in Project menu, option from which you can choose the image file format (bmp, jpg...). It's not on the clipboard but easier and more accessible than print composer.
Regards,
Harrissou


I'm familiar with the composer function. My users can't use this neither because it takes to long to activate (to many choices and to many buttons to click)

So I've decided to make a simple plugin to cover this functionality. But first I want to be very sure the function actually is missing ;-)

Regards
Bo Victor Thomsen
Municipality of Frederikssund, Denmark.
_______________________________________________
Qgis-user mailing list
Qgis-user at lists.osgeo.org<mailto:Qgis-user at lists.osgeo.org>
List info: http://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-user


_______________________________________________
Qgis-user mailing list
Qgis-user at lists.osgeo.org<mailto:Qgis-user at lists.osgeo.org>
List info: http://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20161222/c4044bd0/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 3560 bytes
Desc: image001.jpg
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20161222/c4044bd0/attachment.jpg>


More information about the Qgis-user mailing list