启用控件 QML ListView

huangapple go评论91阅读模式
英文:

Enable Controls QML ListView

问题

  1. >问题
  2. QML ListView委托中,有一个自定义文本字段。当在列表委托内部时,此文本字段无法正常工作,但在该委托之外可以完美运行。
  3. >我已经知道的(或者我认为我知道的)
  4. 我已经将问题缩小到焦点问题(列表中的任何内容都无法用鼠标或键盘聚焦),并且相同的问题仍然适用于其他Qt Quick Controls,如按钮和滑块。
  5. >我需要什么
  6. 我需要能够在列表视图和委托中使用和交互Qt Quick Controls,如按钮、文本字段等。感谢您的时间和帮助!
  7. >代码
  8. **列表视图**
  9. ListView {
  10. id: root
  11. anchors.horizontalCenter: parent.horizontalCenter
  12. width: 800
  13. height: 900
  14. focus: true
  15. spacing: 15
  16. reuseItems: true
  17. maximumFlickVelocity: 2000
  18. verticalLayoutDirection: ListView.TopToBottom
  19. model: listModel
  20. delegate: listDelegate
  21. ListModel {
  22. id: listModel
  23. Component.onCompleted: {
  24. // 获取文档处理程序数据对象键
  25. var keys = DocumentHandler.dataModelKeys
  26. // 创建数据对象列表元素
  27. for (const key of keys) {
  28. listModel.append(createElement(key))
  29. }
  30. }
  31. // 创建列表元素
  32. function createElement(key) {
  33. // 返回新的列表元素
  34. return {
  35. key: key,
  36. type: DocumentHandler.getDataObjectType(key),
  37. documentPage: 1
  38. }
  39. }
  40. }
  41. Component {
  42. id: listDelegate
  43. Item {
  44. id: item
  45. anchors.horizontalCenter: parent.horizontalCenter
  46. width: root.width
  47. height: 50
  48. Text {
  49. id: text
  50. anchors.left: parent.left
  51. anchors.verticalCenter: parent.verticalCenter
  52. text: model.key
  53. font.bold: true
  54. font.pointSize: 18
  55. }
  56. CustomTextField {
  57. id: textField
  58. anchors.right: parent.right
  59. placeholderText: key
  60. widthScale: 1.75
  61. onAccepted: {
  62. // 设置新的占位文本
  63. placeholderText = text
  64. // 使用键设置新的文档处理程序数据对象值
  65. DocumentHandler.setDataObjectValue(key, text)
  66. }
  67. }
  68. }
  69. }
  70. ScrollBar.vertical: ScrollBar {
  71. policy: ScrollBar.AsNeeded
  72. }
  73. MouseArea {
  74. anchors.fill: parent
  75. onWheel: (wheel)=> {
  76. // 使用滚轮滚动列表视图
  77. if (wheel.modifiers & Qt.ControlModifier){
  78. if (wheel.angleDelta.y > 0) {
  79. mainTextEdit.font.pixelSize++
  80. } else {
  81. mainTextEdit.font.pixelSize--
  82. }
  83. wheel.accepted = true
  84. } else {
  85. wheel.accepted = false
  86. }
  87. }
  88. }
  89. }
  90. **自定义文本字段**
  91. TextField {
  92. id: root
  93. property var borderColor: "#3569be"
  94. property var backgroundColor: "#ffffff"
  95. property var textScale: 1.0
  96. property var widthScale: 1.0
  97. property var heightScale: 1.0
  98. color: "#000000"
  99. leftPadding: 7.5
  100. font.bold: true
  101. font.pointSize: 14 * root.textScale
  102. selectionColor: "#3569be"
  103. selectedTextColor: "#e0e0df"
  104. background: Rectangle {
  105. id: background
  106. anchors.centerIn: parent
  107. implicitWidth: 400 * root.widthScale
  108. implicitHeight: 45 * root.heightScale
  109. radius: 5
  110. color: root.backgroundColor
  111. border.width: 1.5
  112. border.color: root.borderColor
  113. }
  114. onEditingFinished: {
  115. // 取消焦点
  116. focus = false
  117. }
  118. }
英文:

>The Problem

In a QML ListView delegate, there is a custom text field. This text field does not function when inside the list delegate, but works perfectly outside of said delegate.

> What I Already Know (Or think I know...)

I have already narrowed the problem down to focusing issues (Nothing in the list can brought into focus with a mouse or keyboard), and the same problem still applies to other Qt Quick Controls such as buttons and sliders.

> What I Need

I need to be able to use and interact with Qt Quick Controls such as buttons, text fields, etc. inside of list views and delegates. Thanks for your time and help!

> The Code

List View

  1. ListView {
  2. id: root
  3. anchors.horizontalCenter: parent.horizontalCenter
  4. width: 800
  5. height: 900
  6. focus: true
  7. spacing: 15
  8. reuseItems: true
  9. maximumFlickVelocity: 2000
  10. verticalLayoutDirection: ListView.TopToBottom
  11. model: listModel
  12. delegate: listDelegate
  13. ListModel {
  14. id: listModel
  15. Component.onCompleted: {
  16. // Get document handler data object keys
  17. var keys = DocumentHandler.dataModelKeys
  18. // Create data object list elements
  19. for (const key of keys) {
  20. listModel.append(createElement(key))
  21. }
  22. }
  23. // Create list element
  24. function createElement(key) {
  25. // Return new list element
  26. return {
  27. key: key,
  28. type: DocumentHandler.getDataObjectType(key),
  29. documentPage: 1
  30. }
  31. }
  32. }
  33. Component {
  34. id: listDelegate
  35. Item {
  36. id: item
  37. anchors.horizontalCenter: parent.horizontalCenter
  38. width: root.width
  39. height: 50
  40. Text {
  41. id: text
  42. anchors.left: parent.left
  43. anchors.verticalCenter: parent.verticalCenter
  44. text: model.key
  45. font.bold: true
  46. font.pointSize: 18
  47. }
  48. CustomTextField {
  49. id: textField
  50. anchors.right: parent.right
  51. placeholderText: key
  52. widthScale: 1.75
  53. onAccepted: {
  54. // Set new placeholder text
  55. placeholderText = text
  56. // Set new document handler data object value with key
  57. DocumentHandler.setDataObjectValue(key, text)
  58. }
  59. }
  60. }
  61. }
  62. ScrollBar.vertical: ScrollBar {
  63. policy: ScrollBar.AsNeeded
  64. }
  65. MouseArea {
  66. anchors.fill: parent
  67. onWheel: (wheel)=> {
  68. // Scroll list view with wheel
  69. if (wheel.modifiers & Qt.ControlModifier){
  70. if (wheel.angleDelta.y > 0) {
  71. mainTextEdit.font.pixelSize++
  72. } else {
  73. mainTextEdit.font.pixelSize--
  74. }
  75. wheel.accepted = true
  76. } else {
  77. wheel.accepted = false
  78. }
  79. }
  80. }
  81. }

Custom Text Field

  1. TextField {
  2. id: root
  3. property var borderColor: "#3569be"
  4. property var backgroundColor: "#ffffff"
  5. property var textScale: 1.0
  6. property var widthScale: 1.0
  7. property var heightScale: 1.0
  8. color: "#000000"
  9. leftPadding: 7.5
  10. font.bold: true
  11. font.pointSize: 14 * root.textScale
  12. selectionColor: "#3569be"
  13. selectedTextColor: "#e0e0df"
  14. background: Rectangle {
  15. id: background
  16. anchors.centerIn: parent
  17. implicitWidth: 400 * root.widthScale
  18. implicitHeight: 45 * root.heightScale
  19. radius: 5
  20. color: root.backgroundColor
  21. border.width: 1.5
  22. border.color: root.borderColor
  23. }
  24. onEditingFinished: {
  25. // Remove from focus
  26. focus = false
  27. }
  28. }

答案1

得分: 1

MouseArea捕获了所有鼠标事件,因为它的z顺序高于屏幕上的所有其他元素。请更改元素的z顺序或删除MouseArea。

英文:

The MouseArea captures all the mouse events since its z-order is higher than all other elements on the screen. Please change the z-order of the elements or Remove the MouseArea.

huangapple
  • 本文由 发表于 2023年6月25日 23:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定