[Qgis-developer] [Python plugin] menu items: actions binding with cycle

Anne Ghisla a.ghisla at studenti.uninsubria.it
Wed Jul 30 07:42:25 EDT 2008


Hi Barry,

On Wed, 2008-07-30 at 12:18 +0100, Barry Rowlingson wrote:
> 2008/7/30 Anne Ghisla <a.ghisla at studenti.uninsubria.it>:
> > for k, v in self.mth.iteritems():
> >  a = getattr(self,  "action" + k)
> >  a = QAction(QIcon(""), v , self.iface.getMainWindow())
> 
>  I'm guessing the repetition of "a=" here is a mistake?

It is in fact a mistake. But getattr(self,  "action" + k)=
QAction(QIcon(""), v , self.iface.getMainWindow()), that was closer to
the original line, is not allowed.

[...]

> Are there some lines missing that call addMenu?

Yes, sorry, I missed them in previous mail:
[code]
for m in self.mth:
  self.iface.addPluginMenu("Home range analysis",  getattr(self,
"action" + m))
[/code]

this part works fine. For each method, it adds its item in menu "Home range analysis".
> 
>  What could be going wrong with your code is the assignment to a local
> variable 'a'. As the variable gets overwritten each time in the loop,
> the underlying Qt QAction item might get cleaned up and deleted since
> there are now no references to it. I've had code crash on me because
> of this in the past. Note that my actions (self.summary,
> self.histogram) are stored in the object.
> 
>  If you store all your actions in a list that persists in the object
> (e.g. self.actions[]) then they'll persist for the life of your
> application.
> 
>  Also, I'd suggest storing your action information in a python list
> rather than a dict so that you can decide on the order. But I think
> this approach might get fiddly once you decide to put separators in to
> your menu. My spqr code explicitly adds actions and separators to the
> menu in the order of the code.

I'll try to use a list-like object. Many thanks for hints!
cheers,

Anne

> 
>  Hope that helps.
> 
> Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.osgeo.org/pipermail/qgis-developer/attachments/20080730/8a63977a/attachment.bin


More information about the Qgis-developer mailing list