[PyQt] Fixes for example models and the ModelTest

Andreas Pakulat apaku at gmx.de
Fri Apr 6 20:12:07 BST 2007


Hi,

I tried the modeltest on the various tree models in PyQt4 examples and
the attached diff fixes the 2 small bugs that the test finds. Especially
the bounds-check is important in Python because lists allow to index
with negative indices in python. This can lead to weird behaviour
instead of crashes like in C++.

Also the ModelTest class itself had a few problems, so attached is an
updated version.

Andreas

-- 
You will be successful in love.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: modeltest.py
Type: text/x-python
Size: 18060 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070406/501e6c3b/modeltest-0001.py
-------------- next part --------------
diff -u -ru PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/pixelator/pixelator.py PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/pixelator/pixelator.py
--- PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/pixelator/pixelator.py	2007-04-05 04:39:26.000000000 +0200
+++ PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/pixelator/pixelator.py	2007-04-06 21:04:59.000000000 +0200
@@ -28,7 +28,6 @@
 
 import pixelator_rc
 
-
 ItemSize = 256
 
 
diff -u -ru PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/simpledommodel.py PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/simpledommodel.py
--- PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/simpledommodel.py	2007-04-05 04:39:26.000000000 +0200
+++ PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/simpledommodel.py	2007-04-06 21:08:57.000000000 +0200
@@ -26,7 +26,6 @@
 import sys
 from PyQt4 import QtCore, QtGui, QtXml
 
-
 class DomItem:
     def __init__(self, node, row, parent=None):
         self.domNode = node
@@ -116,6 +115,8 @@
         return QtCore.QVariant()
 
     def index(self, row, column, parent):
+        if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
+            return QtCore.QModelIndex()
         if not parent.isValid():
             parentItem = self.rootItem
         else:
@@ -140,6 +141,8 @@
         return self.createIndex(parentItem.row(), 0, parentItem)
 
     def rowCount(self, parent):
+        if parent.column() > 0:
+            return 0
         if not parent.isValid():
             parentItem = self.rootItem
         else:
diff -u -ru PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/simpletreemodel/simpletreemodel.py PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/simpletreemodel/simpletreemodel.py
--- PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/simpletreemodel/simpletreemodel.py	2007-04-05 04:39:26.000000000 +0200
+++ PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/simpletreemodel/simpletreemodel.py	2007-04-06 20:45:31.000000000 +0200
@@ -100,6 +100,8 @@
         return QtCore.QVariant()
 
     def index(self, row, column, parent):
+        if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
+            return QtCore.QModelIndex()
         if not parent.isValid():
             parentItem = self.rootItem
         else:
@@ -124,6 +126,8 @@
         return self.createIndex(parentItem.row(), 0, parentItem)
 
     def rowCount(self, parent):
+        if parent.column() > 0:
+            return 0
         if not parent.isValid():
             parentItem = self.rootItem
         else:
diff -u -ru PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/sortingmodel/sortingmodel.py PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/sortingmodel/sortingmodel.py
--- PyQt-x11-gpl-4-snapshot-20070404/examples/itemviews/sortingmodel/sortingmodel.py	2007-04-05 04:39:26.000000000 +0200
+++ PyQt-x11-gpl-4-snapshot-20070404.new/examples/itemviews/sortingmodel/sortingmodel.py	2007-04-06 21:08:04.000000000 +0200
@@ -28,7 +28,6 @@
 
 import sortingmodel_rc
 
-
 class TreeModel(QtCore.QAbstractItemModel):
     def __init__(self, data, parent=None):
         QtCore.QAbstractItemModel.__init__(self, parent)
@@ -66,6 +65,8 @@
         return QtCore.QVariant()
     
     def index(self, row, column, parent=QtCore.QModelIndex()):
+        if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
+            return QtCore.QModelIndex()
         if not parent.isValid():
             parentItem = self.rootItem
         else:
@@ -90,6 +91,8 @@
         return self.createIndex(parentItem.row(), 0, parentItem)
     
     def rowCount(self, parent = QtCore.QModelIndex()):
+        if parent.column() > 0:
+            return 0
         if not parent.isValid():
             parentItem = self.rootItem
         else:


More information about the PyQt mailing list