[Qgis-developer] Python dynamic forms

Bernhard Ströbl Bernhard.Stroebl at jena.de
Thu Sep 16 02:24:47 EDT 2010


Hi Jeremy,

I had a similar problem recently when I wanted to share the same 
connection amongst several plugins. What I did is to create a "master" 
plugin which adds a menu in the menu bar when initialized. I created a 
psycopg.connection - Object and attached it to the menu. Each plugin 
using this connection creates an instance of the master-plugin class. If 
the master-plugin class has been initialized by another plugin (menu 
added, connection set up) the master-plugin object is initialized 
differently: the existing connection is used.
I am not sure if this is the way one should do this but it works :)
Be sure to end the transaction after each sql-command issued 
(con.commit() or con.rollback()) even after a SELECT, see:
http://initd.org/psycopg/docs/faq.html

How do you access the widgets in the custom ui in your ui-init function?


Here is the code:

class MasterPlugin():
     def __init__( self, iface ):
         self.iface = iface
         self.menu = None
         self.title = "&Master Plugin"
         self.con = None # ini
         menu_bar = self.iface.mainWindow().menuBar()
         actions = menu_bar.actions()

         # has the menu been added by another plugin?
         for action in actions:
             if action.text() == self.title:
                 self.menu = action.menu()
                 self.actMenu = action
                 self.con = self.menu.con
                 break

         if self.menu == None:
             # create the menu
             self.menu = QMenu()
             self.menu.setTitle( self.title )
             lastAction = actions[ len( actions ) - 1 ]
             self.actMenu = menu_bar.insertMenu( lastAction, self.menu )
	    #some stuff left out here
	    con = self.dbConnect( selected )

             if con:
                self.con = con
                self.menu.con = con


class OtherPlugin():
     def initGui(self):
         # create MasterPlugin object
         self.mp = MasterPlugin( self.iface )

     def selectFromDB( self ):
	cur = self.mp.con.cursor()

hope this helps

Bernhard

Jeremy Palmer schrieb:
> Hi All!
> 
>  
> 
> I’m trying to use custom forms for adding and editing features within a 
> layers.
> 
>  
> 
> The forms that I’m creating need to query a PostgreSQL database to 
> populate controls. Using the UI init function seems like the thing to do 
> here. I’ve managed to populate the controls by setting up a new 
> connection and executing the query from within this init function.
> 
>  
> 
> However these forms will be opened up all the time, and from a database 
> administrators point of view I would like cache the database connection 
> so a new connection is not created every time the form is opened. To 
> make things more complicated I would like to share this connection 
> across multiple feature forms within one QGIS user project. I also have 
> a few other functions relating to the admin of these layers that I 
> currently have in a plug-in which also needs the same database connection.
> 
>  
> 
> Is there a way to do connection caching with all of this? I thought 
> about having a singleton class in my plug-in that stores this 
> connection, but it looks like I can’t access the plug-in registry from 
> the PyQGIS api L
> 
>  
> 
> Note I’m using psycopg2 for the database connections.
> 
>  
> 
> Thanks,
> 
> Jeremy
> 
> ------------------------------------------------------------------------
> This message contains information, which is confidential and may be 
> subject to legal privilege.
> If you are not the intended recipient, you must not peruse, use, 
> disseminate, distribute or copy this message.
> If you have received this message in error, please notify us immediately 
> (Phone 0800 665 463 or info at linz.govt.nz <mailto:info at linz.govt.nz> ) 
> and destroy the original message.
> LINZ accepts no responsibility for changes to this email, or for any 
> attachments, after its transmission from LINZ.
>  
> *Thank you.*
> ------------------------------------------------------------------------
> 
> 
> *________ Information from NOD32 ________*
> This message was checked by NOD32 Antivirus System for Linux Mail Server.
> http://www.nod32.com
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> 
> ________ Information from NOD32 ________
> This message was checked by NOD32 Antivirus System for Linux Mail Server.
> http://www.nod32.com


-- 
Dipl.-Geogr. Bernhard Ströbl
Anwendungsbetreuer GIS
Kommunale Immobilien Jena
  - Datenverarbeitung -
mailto:Bernhard.Stroebl at jena.de
http://kij.jena.de/
----------------------------


________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com


More information about the Qgis-developer mailing list