[PyQt] [Python plugin] menu items: actions binding with cycle

Anne Ghisla a.ghisla at studenti.uninsubria.it
Wed Jul 30 10:15:09 BST 2008


Hello all,

I'm developing a plugin for QGIS using:
- PyQt 4.3.3-2ubuntu4.1 
- Python 2.5.2
- Qt4

I'm populating a submenu with 4 items:

[code]
# create action for MCP submenu
self.actionmcp = QAction(QIcon(""), "MCP", self.iface.getMainWindow())
self.actionmcp.setWhatsThis("None")
QObject.connect(self.actionmcp, SIGNAL("activated()"), lambda
method="mcp": self.showInterface(method))  

# create action for kernel href submenu
self.actionhref = QAction(QIcon(""), "Kernel
href",self.iface.getMainWindow())
self.actionhref.setWhatsThis("None")
QObject.connect(self.actionhref, SIGNAL("activated()"),  lambda
method="href": self.showInterface(method))
[/code]

...and so on.

The code is clearly self-similar. 
I tried to write a cycle to populate the menu, but it doesn't work.

[code]
mth = {"mcp":"MCP", "href":"Kernel h ref", "lscv":"Kernel h LSCV",
"hadj":"Kernel with adjusted h"}
[declaration of self.action* outside cycle]
for k, v in self.mth.iteritems():
 a = getattr(self,  "action" + k)
 a = QAction(QIcon(""), v , self.iface.getMainWindow())
 a.setWhatsThis("None")
 QObject.connect(a, SIGNAL("activated()"), lambda method=k:
self.showInterface(method))
[/code]

I get no errors when running QGIS from a shell, but no submenu is
displayed.
I've also tried to assign QAction(QIcon(""), v ,
self.iface.getMainWindow()) directly on getattr(), but Python didn't
allow to assign to a function.

Any suggestions? 

regards,

Anne Ghisla
-------------- 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://www.riverbankcomputing.com/pipermail/pyqt/attachments/20080730/df968be0/attachment.bin


More information about the PyQt mailing list