QML:自定义ComboBox中元素在鼠标光标上弹跳

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

QML: Elements bouncing in customized ComboBox on mouse cursor

问题

我的任务是创建带有图像的ComboBox。我尝试根据Qt的文档自定义ComboBox。它可以正常工作,但当你用鼠标指向元素时,它会水平弹跳。我希望它保持静态。这是我的实现:

  1. ComboBox {
  2. Component.onCompleted: {
  3. // 这里执行一些与索引相关的操作
  4. }
  5. id: languageComboBox
  6. contentItem: Image {
  7. id: countryFlag
  8. anchors.left: parent.left
  9. anchors.leftMargin: 5
  10. horizontalAlignment: Image.AlignLeft
  11. smooth: false
  12. antialiasing: false
  13. sourceSize: Qt.size(40, 20)
  14. fillMode: Image.Pad
  15. }
  16. delegate: ItemDelegate {
  17. contentItem: Image {
  18. id: currentFlag
  19. anchors.left: parent.left
  20. anchors.leftMargin: 5
  21. horizontalAlignment: Image.AlignLeft
  22. sourceSize: Qt.size(40, 20)
  23. smooth: false
  24. antialiasing: false
  25. opacity: 1.0
  26. fillMode: Image.Pad
  27. source: imgSource
  28. }
  29. highlighted: languageComboBox.highlightedIndex === index
  30. }
  31. popup: Popup {
  32. y: languageComboBox.height - 1
  33. width: languageComboBox.width
  34. implicitHeight: contentItem.implicitHeight
  35. padding: 1
  36. contentItem: ListView {
  37. implicitHeight: contentHeight
  38. clip: true
  39. model: languageComboBox.popup.visible ? languageComboBox.delegateModel : null
  40. currentIndex: languageComboBox.highlightedIndex
  41. boundsBehavior: Flickable.StopAtBounds
  42. }
  43. }
  44. model: ListModel {
  45. // 这里有用于更改图像源和其他内容的元素数据
  46. }
  47. onActivated: {
  48. // 当激活时执行一些操作
  49. }
  50. }

从我理解的情况来看,问题可能出在PopupListView上。我完全不知道如何解决它。我尝试了boundsBehavior: Flickable.StopAtBounds,但没有效果。

这可能看起来有点生疏。这是我找到的第一个实现查看图像的解决方案。

编辑

我已经测试了Qt提供的示例 - 其中的元素(文本)也会在鼠标悬停时弹跳!

英文:

My task is to make ComboBox with images. I have tried to implement ComboBox customization from Qt documentation. It's works well, but when you point an element with mouse cursor it bouncing horizontally. I want it static look. Here is my implementation:

  1. ComboBox {
  2. Component.onCompleted: {
  3. //Some actions with index here
  4. }
  5. id: languageComboBox
  6. contentItem: Image {
  7. id: countryFlag
  8. anchors.left: parent.left
  9. anchors.leftMargin: 5
  10. horizontalAlignment: Image.AlignLeft
  11. smooth: false
  12. antialiasing: false
  13. sourceSize: Qt.size(40, 20)
  14. fillMode: Image.Pad
  15. }
  16. delegate: ItemDelegate {
  17. contentItem: Image {
  18. id: currentFlag
  19. anchors.left: parent.left
  20. anchors.leftMargin: 5
  21. horizontalAlignment: Image.AlignLeft
  22. sourceSize: Qt.size(40, 20)
  23. smooth: false
  24. antialiasing: false
  25. opacity: 1.0
  26. fillMode: Image.Pad
  27. source: imgSource
  28. }
  29. highlighted: languageComboBox.highlightedIndex === index
  30. }
  31. popup: Popup {
  32. y: languageComboBox.height - 1
  33. width: languageComboBox.width
  34. implicitHeight: contentItem.implicitHeight
  35. padding: 1
  36. contentItem: ListView {
  37. implicitHeight: contentHeight
  38. clip: true
  39. model: languageComboBox.popup.visible ? languageComboBox.delegateModel : null
  40. currentIndex: languageComboBox.highlightedIndex
  41. boundsBehavior: Flickable.StopAtBounds
  42. }
  43. }
  44. model: ListModel {
  45. // There is data in elements I use to change image source and other
  46. }
  47. onActivated : {
  48. // Some actions on activated
  49. }
  50. }

As I understand, there is troubles with Popup or ListView. And absolutely have no idea how to fix it. I'm tried boundsBehavior: Flickable.StopAtBounds but it have no effect on this.
It's may seem rusty. This is first solution I found to implement possibility to see images in

EDIT

I have tested example provided from Qt - it's elements (text) is bouncing on mouse too!

答案1

得分: 0

只需在Popup中的您的ListView上添加锚点。使用anchors.fill: parent

因此,您的弹出窗口将如下所示:

  1. popup: Popup {
  2. y: languageComboBox.height - 1
  3. implicitWidth: languageComboBox.width
  4. implicitHeight: contentItem.implicitHeight
  5. padding: 1
  6. contentItem: ListView {
  7. implicitHeight: contentHeight
  8. clip: true
  9. anchors.fill: parent
  10. model: languageComboBox.popup.visible ? languageComboBox.delegateModel : null
  11. currentIndex: languageComboBox.highlightedIndex
  12. boundsBehavior: Flickable.StopAtBounds
  13. }
  14. }
英文:

Just add anchors to your ListView in Popup. Use anchors.fill: parent.

So your popup would look like this:

  1. popup: Popup {
  2. y: languageComboBox.height - 1
  3. implicitWidth: languageComboBox.width
  4. implicitHeight: contentItem.implicitHeight
  5. padding: 1
  6. contentItem: ListView {
  7. implicitHeight: contentHeight
  8. clip: true
  9. anchors.fill: parent
  10. model: languageComboBox.popup.visible ? languageComboBox.delegateModel : null
  11. currentIndex: languageComboBox.highlightedIndex
  12. boundsBehavior: Flickable.StopAtBounds
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年7月20日 21:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730242.html
匿名

发表评论

匿名网友

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

确定