Item views: prevent negative cache buffer
authorJ-P Nurmi <jpnurmi@digia.com>
Tue, 6 May 2014 10:22:32 +0000 (12:22 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 7 May 2014 12:46:50 +0000 (14:46 +0200)
A negative cache buffer does not make much sense, and the item views
would go crazy and start creating/destructing delegates endlessly.

Task-number: QTBUG-38725
Change-Id: I1fbba1f3130a99af67fbc4c2aba4d3199d0554a9
Reviewed-by: Liang Qi <liang.qi@digia.com>
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
src/quick/items/qquickgridview.cpp
src/quick/items/qquickitemview.cpp
src/quick/items/qquicklistview.cpp
tests/auto/quick/qquicklistview/tst_qquicklistview.cpp

index 82658c7..2c03903 100644 (file)
@@ -1524,7 +1524,7 @@ void QQuickGridView::setHighlightFollowsCurrentItem(bool autoHighlight)
     This property determines whether delegates are retained outside the
     visible area of the view.
 
-    If non-zero the view may keep as many delegates
+    If this value is greater than zero, the view may keep as many delegates
     instantiated as will fit within the buffer specified.  For example,
     if in a vertical view the delegate is 20 pixels high, there are 3
     columns and \c cacheBuffer is
@@ -1535,7 +1535,7 @@ void QQuickGridView::setHighlightFollowsCurrentItem(bool autoHighlight)
     delegates outside the visible area are not painted.
 
     The default value of this property is platform dependent, but will usually
-    be a non-zero value.
+    be a value greater than zero. Negative values are ignored.
 
     Note that cacheBuffer is not a pixel buffer - it only maintains additional
     instantiated delegates.
index b4f6c34..87e6728 100644 (file)
@@ -454,6 +454,11 @@ int QQuickItemView::cacheBuffer() const
 void QQuickItemView::setCacheBuffer(int b)
 {
     Q_D(QQuickItemView);
+    if (b < 0) {
+        qmlInfo(this) << "Cannot set a negative cache buffer";
+        return;
+    }
+
     if (d->buffer != b) {
         d->buffer = b;
         if (isComponentComplete()) {
index 8f9dbb5..ba4f1c5 100644 (file)
@@ -2138,7 +2138,7 @@ void QQuickListView::setOrientation(QQuickListView::Orientation orientation)
     This property determines whether delegates are retained outside the
     visible area of the view.
 
-    If this value is non-zero, the view may keep as many delegates
+    If this value is greater than zero, the view may keep as many delegates
     instantiated as it can fit within the buffer specified.  For example,
     if in a vertical view the delegate is 20 pixels high and \c cacheBuffer is
     set to 40, then up to 2 delegates above and 2 delegates below the visible
@@ -2148,7 +2148,7 @@ void QQuickListView::setOrientation(QQuickListView::Orientation orientation)
     delegates outside the visible area are not painted.
 
     The default value of this property is platform dependent, but will usually
-    be a non-zero value.
+    be a value greater than zero. Negative values are ignored.
 
     Note that cacheBuffer is not a pixel buffer - it only maintains additional
     instantiated delegates.
index 29755e3..5cc3c7e 100644 (file)
@@ -3012,6 +3012,10 @@ void tst_QQuickListView::cacheBuffer()
         controller.incubateWhile(&b);
     }
 
+    // negative cache buffer is ignored
+    listview->setCacheBuffer(-1);
+    QCOMPARE(listview->cacheBuffer(), 200);
+
     delete window;
     delete testObject;
 }