Tuesday, 22 September 2015

qgis - How to make my programmatically added map items show up in the composer items list?


I try to make a composer via python. I add item, make a composer view and open, but i can't see item in "Items" dock. Also, when i select it, i don't see item properties in "Item properties" dock.


from qgis.core import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# make a composition
mapRenderer = iface.mapCanvas().mapRenderer()

c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)
view = iface.createNewComposer()

# add label
lbl = QgsComposerLabel(c)
lbl.setText("test text")
lbl.setItemPosition(10,10)
lbl.adjustSizeToText()
c.addComposerLabel(lbl)


# view composer
view.setComposition(c)

enter image description here


How i can fix it?


UPD: I setup model for my item and now i see it in Items dock, but properties still doesn't show.... Additional code:


# find items and items prop widget
win = view.composerWindow()
items_widget = win.findChildren(QDockWidget, 'ItemsDock')[0]

items_prop_widget = win.findChildren(QDockWidget, 'ItemDock')[0]

# set items model
items_list_widget = items_widget.findChildren(QTreeView,'')[0]
items_model = c.itemsModel()
items_list_widget.setModel(items_model)

enter image description here



Answer



I have created a issue in the bug tracker,because is so strange.



Great job in your code,but for fix the hide values when you click on the label only need change some things. The method mapRenderer = iface.mapCanvas().mapRenderer() is deprecated you need change for mapRenderer = iface.mapCanvas().mapSettings(). Another change is that you need call to view.setComposition(c) before you add a label.


the complete code.


from qgis.core import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# make a composition
mapRenderer = iface.mapCanvas().mapSettings()
c = QgsComposition(mapRenderer)

c.setPlotStyle(QgsComposition.Print)
view = iface.createNewComposer()

# view composer
view.setComposition(c)

#Fix error
view.setCurrentTool( view.AddLabel )
view.setCurrentTool( view.Select )


# add label
lbl = QgsComposerLabel(c)
lbl.setText("test text")
lbl.setItemPosition(10,10)
lbl.adjustSizeToText()
c.addComposerLabel(lbl)

# find items and items prop widget
win = view.composerWindow()
items_widget = win.findChildren(QDockWidget, 'ItemsDock')[0]

#items_prop_widget = win.findChildren(QWidget, 'ItemDock')[0]

# set items model
items_list_widget = items_widget.findChildren(QTreeView,'')[0]
items_model = c.itemsModel()
items_list_widget.setModel(items_model)

Update Code: Solved signal problem.


I'm deleted some unnecessary lines and add the signal connect.


QObject.connect( items_list_widget.selectionModel(), SIGNAL("currentChanged( QModelIndex, QModelIndex)"), c.itemsModel(),SLOT("setSelected( QModelIndex )"))


Complete code


from qgis.core import *
from qgis.utils import iface
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# make a composition
mapRenderer = iface.mapCanvas().mapSettings()
c = QgsComposition(mapRenderer)

c.setPlotStyle(QgsComposition.Print)
view = iface.createNewComposer()

# view composer
view.setComposition(c)

#Add Map
rect = QgsRectangle(mapRenderer.fullExtent())
composer_map = QgsComposerMap(c, 0, 0, c.paperWidth(), c.paperHeight())
composer_map.setNewExtent(rect)

composer_map.updateItem()
c.addComposerMap(composer_map)

# add label
lbl = QgsComposerLabel(c)
lbl.setText("test text")
lbl.setItemPosition(10,10)
lbl.adjustSizeToText()
c.addComposerLabel(lbl)


# add label
lbl = QgsComposerLabel(c)
lbl.setText("other text")
lbl.setItemPosition(50,10)
lbl.adjustSizeToText()
c.addComposerLabel(lbl)

# find items and items prop widget
win = view.composerWindow()
items_widget = win.findChildren(QDockWidget, 'ItemsDock')[0]


items_widget_prop = win.findChildren(QDockWidget, 'ItemDock')[0]
items_widget_prop.show()

# set items model
items_list_widget = items_widget.findChildren(QTreeView,'')[0]
items_model = c.itemsModel()
items_list_widget.setModel(items_model)

QObject.connect( items_list_widget.selectionModel(), SIGNAL("currentChanged( QModelIndex, QModelIndex)"), c.itemsModel(),SLOT("setSelected( QModelIndex )"))


Result.


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...