TableViewDelegate.qml 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2024 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import QtQuick.Controls.impl
  6. import QtQuick.Templates as T
  7. T.TableViewDelegate {
  8. id: control
  9. // same as AbstractButton.qml
  10. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  11. implicitContentWidth + leftPadding + rightPadding)
  12. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  13. implicitContentHeight + topPadding + bottomPadding)
  14. highlighted: control.selected
  15. required property int column
  16. required property int row
  17. required property var model
  18. background: Rectangle {
  19. border.width: control.current ? 2 : Qt.styleHints.accessibility.contrastPreference === Qt.HighContrast ? 1 : 0
  20. border.color: control.current ? control.palette.highlight : control.palette.windowText
  21. color: control.highlighted
  22. ? control.palette.highlight
  23. : (control.tableView.alternatingRows && control.row % 2 !== 0
  24. ? control.palette.alternateBase : control.palette.base)
  25. }
  26. contentItem: Label {
  27. clip: false
  28. text: control.model.display ?? ""
  29. elide: Text.ElideRight
  30. color: control.highlighted ? control.palette.highlightedText : control.palette.buttonText
  31. visible: !control.editing
  32. }
  33. // The edit delegate is a separate component, and doesn't need
  34. // to follow the same strict rules that are applied to a control.
  35. // qmllint disable attached-property-reuse
  36. // qmllint disable controls-attached-property-reuse
  37. // qmllint disable QuickControlsSanity.controls-sanity
  38. TableView.editDelegate: FocusScope {
  39. width: parent.width
  40. height: parent.height
  41. TableView.onCommit: {
  42. let model = control.tableView.model
  43. if (!model)
  44. return
  45. const index = model.index(control.row, control.column)
  46. if (!model.setData(index, textField.text, Qt.EditRole))
  47. console.warn("The model does not allow setting the EditRole data.")
  48. }
  49. Component.onCompleted: textField.selectAll()
  50. TextField {
  51. id: textField
  52. anchors.fill: parent
  53. text: control.model.edit ?? control.model.display ?? ""
  54. focus: true
  55. }
  56. }
  57. // qmllint enable attached-property-reuse
  58. // qmllint enable controls-attached-property-reuse
  59. // qmllint enable QuickControlsSanity.controls-sanity
  60. }