【问题标题】:How to use QML Repeater for page indicator?如何将 QML Repeater 用于页面指示器?
【发布时间】:2021-03-14 03:47:20
【问题描述】:

以下是我的部分代码。它工作得很好,但是我花了很多时间试图用 Repeater 来解决它,使其更加优雅和灵活,但没有成功。

您能否向我提出一个解决方案? areaMachine 在另一个 qml 文件中。

Row {
        id: pageIndicatorBoxes
        anchors.centerIn: pageIndicatorLine
        spacing: 5

        Rectangle {
            id: pageIndicatorBox1
            width: 10
            height: 10
            color: areaMachine.page == 1 ? "#e5e5e5" : "#404040"
        }

        Rectangle {
            id: pageIndicatorBox2
            width: 10
            height: 10
            color: areaMachine.page == 2 ? "#e5e5e5" : "#404040"
        }

        Rectangle {
            id: pageIndicatorBox3
            width: 10
            height: 10
            color: areaMachine.page == 3 ? "#e5e5e5" : "#404040"
        }

        Rectangle {
            id: pageIndicatorBox4
            width: 10
            height: 10
            color: areaMachine.page == 4 ? "#e5e5e5" : "#404040"
        }
    }

【问题讨论】:

    标签: qml repeater


    【解决方案1】:

    您似乎没有查看有关Repeater 及其主要delegate property 的文档。在那里您可以找到适合您问题的代码示例,因此您可以将其组合起来并拥有类似的内容:

      Row {
        id: pageIndicatorBoxes
        anchors.centerIn: pageIndicatorLine
        spacing: 5
        
        Repeater {
          model: 4
          Rectangle {
            id: pageIndicatorBox1
            width: 10
            height: 10
            color: (areaMachine.page === (index + 1)) ? "#e5e5e5" : "#404040"
          }
        }
      }
    

    【讨论】:

    • 我做到了,并尝试了几个功能/变体。只是不是这种“最简单”的方法。 :S 谢谢。
    • 很高兴听到它有帮助。然后,您可以将其标记为答案。对于进一步的开发,我可以建议您拥有一个单独的尽可能简单的应用程序,您可以在其中尝试新的组件/概念,然后 -> 将您的结果应用到您正在开发的实际应用程序中。
    • 唯一要更正的是:我需要 (index + 1)。
    • 当然可以。更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多