2010/12/3 Matti Airas <[email protected]>:
> Hi,
>
> I just happened to notice that Thomas Perl has been quietly cranking out
> PySide QML tutorials in our wiki:
>
> http://developer.qt.nokia.com/wiki/PySideDocumentation

Unfortunately this one is not working
http://developer.qt.nokia.com/wiki/Selectable_list_of_Python_objects_in_QML
with ubuntu 10.10.
Result is emty window and message:
ile:///home/user/PythonList.qml:18: TypeError: Result of expression
'model.thing' [undefined] is not an object.

can this be fixed?

Niki

-- 
|  (\_/)  This is Bunny. Copy and paste
| (='.'=) Bunny into your signature to help
| (")_(") him gain world domination
import sys
 
from PySide import QtCore
from PySide import QtGui
from PySide import QtDeclarative
from PySide import QtOpenGL

class ThingWrapper(QtCore.QObject):
    def __init__(self, thing):
        QtCore.QObject.__init__(self)
        self._thing = thing

    def _name(self):
        return str(self._thing)

    changed = QtCore.Signal()

    name = QtCore.Property(unicode, _name, notify=changed)

class ThingListModel(QtCore.QAbstractListModel):
    COLUMNS = ('thing',)

    def __init__(self, things):
        QtCore.QAbstractListModel.__init__(self)
        self._things = things
        self.setRoleNames(dict(enumerate(ThingListModel.COLUMNS)))

    def rowCount(self, parent=None): #QtCore.QModelIndex()
        return len(self._things)

    def data(self, index, role):
        print [[index, role]]
        if index.isValid() and role == ThingListModel.COLUMNS.index('thing'):
            return self._things[index.row()]
        return None

class Controller(QtCore.QObject):
    @QtCore.Slot(QtCore.QObject)
    def thingSelected(self, wrapper):
        print 'User clicked on:', wrapper._thing.name
        if wrapper._thing.number > 10:
            print 'The number is greater than ten!'

class Person(object):
    def __init__(self, name, number):
        self.name = name
        self.number = number
 
    def __str__(self):
        return 'Person "%s" (%d)' % (self.name, self.number)
 
people = [
        Person('Locke', 4),
        Person('Reyes', 8),
        Person('Ford', 15),
        Person('Jarrah', 16),
        Person('Shephard', 23),
        Person('Kwon', 42),
]

def main():
    things = [ThingWrapper(thing) for thing in people]
    thingList = ThingListModel(things)

    app = QtGui.QApplication(sys.argv)
     
    m = QtGui.QMainWindow()
     
    view = QtDeclarative.QDeclarativeView()
    glw = QtOpenGL.QGLWidget()
    view.setViewport(glw)
    view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)

    controller = Controller()
     
    rc = view.rootContext()
    rc.setContextProperty('controller', controller)
    rc.setContextProperty('pythonListModel', thingList)
     
    view.setSource('PythonList.qml')
    m.setCentralWidget(view)
    m.show()
    app.exec_()

main()

Attachment: PythonList.qml
Description: Binary data

_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to