Synchronizing QGIS Print Composer with map view?

With a little help from Python, we could create a function which refreshes your composer map each time the canvas extent (from the main window) has changed. In otherwords, whenever you pan or zoom in/out of the canvas, the composer map will also update. You can type the following code into the Python Console:

def update_composer():
    active_Composer = iface.activeComposers()
    canvas = iface.mapCanvas()
    composer = active_Composer[0]
    composition = composer.composition()
    maps = [item for item in composition.items() if item.type() == QgsComposerItem.ComposerMap and item.scene()]
    composer_map = maps[0]
    moveX = composer_map.extent().center().x()-canvas.extent().center().x()
    moveY = composer_map.extent().center().y()-canvas.extent().center().y()
    unitCon = composer_map.mapUnitsToMM()
    composer_map.moveContent(-moveX * unitCon, moveY * unitCon)
    composer_map.setNewScale(canvas.scale())

iface.mapCanvas().extentsChanged.connect(update_composer)

Credit to the author Ed B for his plugin: Sync Composer With Map of which this code was based upon (you can download/install this plugin from the menubar: Plugins > Manage and Install Plugins...).


I try this solution:

def print_image(self):
    layers = QgsMapLayerRegistry.instance().mapLayers()
    """select layer by name"""
    for name, layer in layers.iteritems():
        if(layer.name()=="[YOUR LAYER NAME]"):

            lay=layer
    self.iface.mapCanvas().zoomToSelected( lay )
    self.iface.mapCanvas().refresh()
    path = 'C:\new folder\image.png'
    self.iface.mapCanvas().saveAsImage(path)

This makes a photo of the map view and I get the selected objects


I have finally used this solution:

In layer Properties->Style->Rule-based enter image description here

With $id=$atlasfeatureid I can see the items selected by the atlas

enter image description here