views:

704

answers:

2

Coming from the .NET world over to Python and PyQt4. Was wondering if anyone is familiar with any functionality that would allow me to bind data to Qt widgets? For example (using sqlalchemy for data):

gems = session.query(Gem).all()
list = QListWidget()
list.datasource = gems

Is such a thing possible?

+2  A: 

One option would have a function that returns a list (or tuple) object from a query, and then use that to update the QListWidget. Remember that the QListWidget stores QListStrings. Your update function might look like this:

def updateQListWidget(qlistwidget, values):
        """ Updates a QListWidget object with a list of values
        ARGS:
            qlistwidget  - QListWidget object
            values       - list of values to add to list widget
        """
        qlistwidget.clear()
        qlist = QtCore.QStringList()
        for v in values:
            s = QtCore.QString(v)
            qlist.append(s)
        qlistwidget.addItems(qlist)
jcoon
+1  A: 

Although not a direct replacement, you might find it useful to look at the QDataWidgetMapper class:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qdatawidgetmapper.html

If you're not scared of reading C++ code, this example might also prove to be helpful:

http://doc.trolltech.com/4.5/sql-sqlwidgetmapper.html

Note that the mapper operates within Qt's Model/View framework. In this example, the model just happens to be a SQL database model.

David Boddie