[QGIS-Developer] QgsLayout.itemById returns wrong object

Nyall Dawson nyall.dawson at gmail.com
Wed Jan 24 15:23:28 PST 2018


On 24 January 2018 at 19:20, Etienne Trimaille
<etienne.trimaille at gmail.com> wrote:
> Is-it possible to remove this function from SIP bindings? According to SIP
> version?
>

Unfortunately I don't think this would help - we'd have to remove
every method which returns a QgsLayoutItem to do so - and there's some
things in API which are only possible with these methods. It's also
proven to be an issue even in the most recent Qt and SIP versions, so
we'd have to just remove these methods from the bindings full stop.
There's also underlying Qt base classes which returns these items
which we can't control (eg QGraphicsScene::items() ).

> It's a common issue, already raised on the mailing list and in plugin issue
> trackers.

I think the real solution is to get it fixed upstream.

> I'm copy/pasting another message/snippet from Martin which works perfectly:
>
> Not sure if that is related, was just looking into code of a plugin we
> use with QGIS 2.18 and there for getting composer items we have this
> helper method:
>
>     @staticmethod
>     def get_composition_item(comp, item_id, item_class):
>         for item in comp.items():
>             if isinstance(item, item_class):
>                 if item.id() == item_id:
>                     return item
>
> And you would call it e.g.:
> ver_label = self.get_composition_item(self.oview_compn, 'version',
> QgsComposerLabel)

I don't see how this helps though - unless some magic in the
isinstance call forces sip to correctly cast the item type? Martin?

> Not sure if that is to work around the same problem as you have - I
> didn't write that code. And to get composer map items, we use
> composition.getComposerMapById(id)

That's gone in 3.0 ;) Map ids where old and crufty, and now everything
is done with the uuids. But unfortunately the methods for retrieving
items by uuid all return a QgsLayoutItem pointer, so will suffer the
same sip casting bug.

It's bad all round. That's why I think we need to try as hard as
possible to get this resolved upstream so we can do away with all the
hacks.

Nyall


More information about the QGIS-Developer mailing list