1 #Written by Robin Burchell
2 # No licence specified or required, but please give credit where it's due, and please let me know if this helped you.
3 # Feel free to contact with corrections or suggestions.
5 # We're using PySide, Nokia's official LGPL bindings.
6 # You can however easily use PyQt (Riverside Computing's GPL bindings) by commenting these and fixing the appropriate imports.
7 #from PySide.QtCore import *
8 #from PySide.QtGui import *
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
14 # This is our model. It will maintain, modify, and present data to our view(s).
15 # As this is read-only, it's pretty straightforward, but it can get pretty complex.
16 # This is something that Qt Development Frameworks/Nokia are aware of and working on, in terms of
17 # better documentation, as well as a better implementation of all this, but both of those aren't
18 # really within the scope of this tutorial. ;)
20 # For more information on list models, take a look at:
21 # http://doc.trolltech.com/4.6/qabstractitemmodel.html
22 # but do bear in mind there are other models (like tables) available, depending on your data needs.
23 # Again, beyond the scope of this tutorial for now. :)
24 class SimpleListModel(QAbstractListModel):
25 def __init__(self, mlist):
26 QAbstractListModel.__init__(self)
28 # Cache the passed data list as a class member.
31 # We need to tell the view how many rows we have present in our data.
32 # For us, at least, it's fairly straightforward, as we have a python list of data,
33 # so we can just return the length of that list.
34 def rowCount(self, parent = QModelIndex()):
35 return len(self._items)
37 # Here, it's a little more complex.
38 # data() is where the view asks us for all sorts of information about our data:
39 # this can be purely informational (the data itself), as well as all sorts of 'extras'
40 # such as how the data should be presented.
42 # For the sake of keeping it simple, I'm only going to show you the data, and one presentational
45 # For more information on what kind of data the views can ask for, take a look at:
46 # http://doc.trolltech.com/4.6/qabstractitemmodel.html#data
48 # Oh, and just to clarify: when it says 'invalid QVariant', it means a null QVariant.
51 # 'index' is of type QModelIndex, which actually has a whole host of stuff, but we
52 # only really care about the row number for the sake of this tutorial.
53 # For more information, see:
54 # http://doc.trolltech.com/4.6/qmodelindex.html
55 def data(self, index, role = Qt.DisplayRole):
56 if role == Qt.DisplayRole:
57 # The view is asking for the actual data, so, just return the item it's asking for.
58 return QVariant(self._items[index.row()])
59 elif role == Qt.BackgroundRole:
60 # Here, it's asking for some background decoration.
61 # Let's mix it up a bit: mod the row number to get even or odd, and return different
63 # (you can, and should, more easily do this using this:
64 # http://doc.trolltech.com/4.6/qabstractitemview.html#alternatingRowColors-prop
65 # but I deliberately chose to show that you can put your own logic/processing here.)
67 # Exercise for the reader: make it print different colours for each row.
68 # Implementation is up to you.
69 if index.row() % 2 == 0:
70 return QVariant(QColor(Qt.gray))
72 return QVariant(QColor(Qt.lightGray))
74 # We don't care about anything else, so make sure to return an empty QVariant.
77 # This widget is our view of the readonly list.
78 # Obviously, in a real application, this will be more complex, with signals/etc usage, but
79 # for the scope of this tutorial, let's keep it simple, as always.
81 # For more information, see:
82 # http://doc.trolltech.com/4.6/qlistview.html
83 class SimpleListView(QListView):
84 def __init__(self, parent = None):
85 QListView.__init__(self, parent)
87 # Our main application window.
88 # You should be used to this from previous tutorials.
89 class MyMainWindow(QWidget):
91 QWidget.__init__(self, None)
93 # main section of the window
96 # create a data source:
97 m = SimpleListModel(["test", "tes1t", "t3est", "t5est", "t3est"])
99 # let's add two views of the same data source we just created:
109 # bottom section of the window
112 # add bottom to main window layout
115 # set layout on the window
118 # set things up, and run it. :)
119 if __name__ == '__main__':
120 app = QApplication(sys.argv)