Advertisement
artemmarchenko

Resizing ListView to the content (for small models only)

Aug 25th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import QtQuick 1.0
  2.  
  3. Rectangle {
  4.     width: 400
  5.     height: 300
  6.     color: "lightyellow"
  7.  
  8.     property int lineHeight: 30
  9.  
  10.     Component {
  11.         id: demoDelegate
  12.  
  13.         Rectangle {
  14.             color: "lightblue"
  15.             width: 120
  16.             height: lineHeight
  17.             Text {
  18.                 text: model.index
  19.             }
  20.         }
  21.     }
  22.  
  23.     ListView {
  24.         id: view
  25.         model: 6
  26.         anchors.left: parent.left
  27.         anchors.bottom: parent.bottom
  28.         width: childrenRect.width
  29.         height: 100
  30.  
  31.         // if you uncomment the following line
  32.         // you get a binding loop as contentHeight depends on total height available
  33. //        height: contentHeight
  34.  
  35.         delegate: demoDelegate
  36.  
  37.         onContentHeightChanged: {
  38.             console.log("view's contentHeight changed to " + contentHeight)
  39.         }
  40.     }
  41.  
  42.     Component.onCompleted: {
  43.         console.log("Screen construction completed, resizing listView to fit all the elements")
  44.         view.height = view.contentHeight
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement