[Qgis-developer] please help me write a QGIS-PostGIS GUI in Python

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Jan 31 11:54:21 EST 2008


tutey wrote:

> 0. Reproduce, in Python, the interface that "Add PostGIS Table(s)"
> provides (qgsdbsourceselectbase.ui).
> 
> I've been unable to, so far. Is there anyone willing to do it? Even a
> part of it would be great - the connection selector, working
> "Connect" and "Edit" buttons. If I had these, I would figure out how
> to proceed with the rest of the interface and more, including:
> 

  Just clarify to us that you aren't trying to redo this from scratch?

You can generate python from a .ui file, and then you just need to add 
signal code and call it.

  You use the pyuic (or pyuic4) command to convert the .ui to a .py:

  % pyuic qgsdbsourceselectbase.ui > qgsdbsourceselectbase.py

  Then the following little snippet shows you how to use the .py in a 
dialog in python code:

from qgsdbsourceselectbase import Ui_QgsDbSourceSelectBase

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Dialog(QDialog, Ui_QgsDbSourceSelectBase):
     def __init__(self, iface):
         QDialog.__init__(self)
         self.iface=iface
         # Set up the user interface from Designer.
         self.setupUi(self)

app=QApplication([])

dialog=Dialog(app)
dialog.exec_()

  This dialog does nothing because no slots or signals are connected. If 
you look in the C code you'll see which signals and slots are connected.

  For example, there's a bunch of 'autoconnected' slots which are 
connected to GUI objects (buttons and so on) such as:

// Slot for adding a new connection
void QgsDbSourceSelect::on_btnNew_clicked()
{
   addNewConnection();
}

  - so that when the 'New' button is clicked it calls the 
addNewConnection method. In Python code this would be a method in the 
Dialog class above, like:

   def on_btnNew_clicked(self):
     self.addNewConnection()

  and then you have to write the addNewConnection method as well.

Now, I'm not sure if this autoconnected slot business works in Python...

  If you want to explicitly connect a signal to a slot then you need to 
set it up in the __init__ method of the Dialog class. When a user 
interacts with a dialog, signals are sent out, and those signals can be 
connected to 'slots' which are methods. For example this code calls the 
'changed()' method when the layerList object emits the 
'currentItemChanged' signal:

QObject.connect(self.layerList, 
SIGNAL("currentItemChanged(QListWidgetItem*, QListWidgetItem*)"), 
self.changed)

  - then in the changed() method I can do something whenever the user 
picks a new layer in a list of layers.

  I'm not sure how much of this wiring you need to do in the interface, 
it's normally a case of reacting to button presses.

  The C++ code of QGIS is pretty darn fine, and is a good way to learn 
about Qt's signals and slots, and its also quite easy to translate to 
python.

  I can't rattle on about this any longer! Hope this helps.

Barry



More information about the Qgis-developer mailing list