[QGIS-Developer] QgsLayout.itemById returns wrong object

Alessandro Pasotti apasotti at gmail.com
Tue Jan 23 05:51:15 PST 2018


On Tue, Jan 23, 2018 at 1:34 PM, Enrico Ferreguti <enricofer at gmail.com>
wrote:

> I'm trying to programmatically set a QgsLayoutPicture content in QGIS3.0:
>
> 1) I create a new print composition and I name it 'test'
> 2) I create a picture frame in the composition and I call it 'picture'
> 3) I run the following code:
>
> from qgis.core import QgsProject
> myLayout = QgsProject.instance().layoutManager().layoutByName('test')
> myLayoutPicture = myLayout.itemById("picture")
>
myLayoutPicture.setPicturePath("path to an image")
>
> and I get the following Exception:
> AttributeError: 'QgsLayoutItem' object has no attribute 'setPicturePath'
>
> If I inspect myLayoutPicture object I found it is a
> qgis._core.QgsLayoutItem not a QgsLayoutPicture
>
> Is this a correct behaviour or is it an issue? How can I get the right
> Layout object?
>

Yes, it is the correct behavior, the method returns  a pointer to the base
class which is QgsLayoutItem.

In the C++ API there is a template method to retrieve all objects of a
certain type but it's not exposed to Python (that's not even possible with
current SIP I think):

template<class T> void layoutItems( QList<T *> &itemList ) const SIP_SKIP

Sorry but I don't have an answer to your last question.

-- 
Alessandro Pasotti
w3:   www.itopen.it
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180123/66c8a7ac/attachment.html>


More information about the QGIS-Developer mailing list