Flickable Wrapped TextEdit

A method of having a Flickable TextEdit with wrapped text within another Item.

  1. import QtQuick 1.0
  2.  
  3. FocusScope {
  4.     width: 200; height: 200
  5.     Rectangle {
  6.         property int margins: 20
  7.         x: margins; y: margins
  8.         width: parent.width - margins*2; height: parent.height - margins*2
  9.         border.color: "black"; border.width: 2; radius: 10
  10.         color: "#23896363"
  11.         Flickable {
  12.             id: flick
  13.             width: parent.width - 10; height: parent.height;
  14.             clip: true
  15.             function ensureVisible(r)
  16.             {
  17.                 if (contentY >= r.y)
  18.                     contentY = r.y;
  19.                 else if (contentY+height <= r.y+r.height)
  20.                     contentY = r.y+r.height-height;
  21.             }
  22.             TextEdit {
  23.                 id: message
  24.                 x: 5;
  25.                 width: parent.width; height: parent.height;
  26.                 wrapMode: "WrapAtWordBoundaryOrAnywhere"
  27.                 onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
  28.                 font.pixelSize: 16
  29.             }
  30.         }
  31.     }
  32. }

Related forum thread: http://developer.qt.nokia.com/forums/viewthread/1956/

Please feel free to fix up the example if you can improve upon it.

Categories: