TableViewDelegate.qml 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Qt.labs.qmlmodels as QtLabsQmlModels
  7. import QtQuick.Templates as T
  8. T.TableViewDelegate {
  9. id: control
  10. // same as AbstractButton.qml
  11. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  12. implicitContentWidth + leftPadding + rightPadding)
  13. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  14. implicitContentHeight + topPadding + bottomPadding)
  15. highlighted: control.selected
  16. required property int column
  17. required property int row
  18. required property var model
  19. background: Rectangle {
  20. border.width: control.current ? 2 : Qt.styleHints.accessibility.contrastPreference === Qt.HighContrast ? 1 : 0
  21. border.color: control.current ? control.palette.highlight : control.palette.windowText
  22. color: control.highlighted
  23. ? control.palette.highlight
  24. : (control.tableView.alternatingRows && control.row % 2 !== 0
  25. ? control.palette.alternateBase : control.palette.base)
  26. }
  27. contentItem: Label {
  28. clip: false
  29. text: control.model.display ?? ""
  30. elide: Text.ElideRight
  31. color: control.highlighted ? control.palette.highlightedText : control.palette.buttonText
  32. visible: !control.editing
  33. }
  34. // The edit delegate is a separate component, and doesn't need
  35. // to follow the same strict rules that are applied to a control.
  36. // qmllint disable attached-property-reuse
  37. // qmllint disable controls-attached-property-reuse
  38. // qmllint disable controls-sanity
  39. TableView.editDelegate: FocusScope {
  40. width: parent.width
  41. height: parent.height
  42. TableView.onCommit: {
  43. let model = control.tableView.model
  44. if (!model)
  45. return
  46. // The setData() APIs are different in QAbstractItemModel and QQmlTableModel.
  47. // This is an issue and will be fixed later, probably by deprecating the wrong
  48. // API in QQmlTableModel. There is a ticket reported this issue and a workaround
  49. // is provided in the description: https://bugreports.qt.io/browse/QTBUG-104733
  50. // But temporarily we need to manage this by checking the model's type.
  51. let succeed = false
  52. const index = model.index(control.row, control.column)
  53. if (model instanceof QtLabsQmlModels.TableModel)
  54. succeed = model.setData(index, "edit", textField.text)
  55. else
  56. succeed = model.setData(index, textField.text, Qt.EditRole)
  57. if (!succeed)
  58. console.warn("The model does not allow setting the EditRole data.")
  59. }
  60. Component.onCompleted: textField.selectAll()
  61. TextField {
  62. id: textField
  63. anchors.fill: parent
  64. text: control.model.edit ?? control.model.display ?? ""
  65. focus: true
  66. }
  67. }
  68. // qmllint enable attached-property-reuse
  69. // qmllint enable controls-attached-property-reuse
  70. // qmllint enable controls-sanity
  71. }