Guest User

Untitled

a guest
Feb 23rd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 1.65 KB | None | 0 0
  1. import QtQuick
  2. import QtQuick.Window
  3. import QtQuick.Controls
  4.  
  5. Window {
  6.     id: root
  7.     width: 240
  8.     height: 300
  9.     visible: true
  10.     title: qsTr("Test")
  11.  
  12.     ListModel {
  13.         id: listModel
  14.         ListElement {
  15.             txt: "Item 1"
  16.         }
  17.     }
  18.  
  19.     ListView {
  20.         id: listView
  21.         anchors.fill: parent
  22.         clip: true
  23.         model: listModel
  24.         reuseItems: true
  25.         topMargin: root.height / 2
  26.         bottomMargin: root.height / 2
  27.         delegate: TextArea {
  28.             id: delegate
  29.  
  30.             required property int index
  31.             required property string txt
  32.  
  33.             width: root.width
  34.             text: txt
  35.  
  36.             onCursorPositionChanged: {
  37.                 listView.positionViewAtIndex(delegate.index, ListView.Center);
  38.             }
  39.  
  40.             Keys.onReturnPressed: {
  41.                 listModel.append({txt: "Item " + (listModel.count + 1)});
  42.             }
  43.  
  44.             Keys.onPressed: (event) => {
  45.                 if (event.key === Qt.Key_Backspace) {
  46.                     if (delegate.index > 0) {
  47.                         listView.positionViewAtIndex(delegate.index - 1, ListView.Center);
  48.                         listView.itemAtIndex(delegate.index - 1).forceActiveFocus();
  49.                     }
  50.                     listModel.remove(delegate.index);
  51.                 }
  52.             }
  53.  
  54.             ListView.onAdd: {
  55.                 delegate.forceActiveFocus();
  56.                 delegate.cursorPosition = delegate.text.length;
  57.                 listView.positionViewAtIndex(delegate.index , ListView.Center);
  58.             }
  59.         }
  60.         ScrollBar.vertical: ScrollBar {}
  61.     }
  62. }
Add Comment
Please, Sign In to add comment