首页 > 解决方案 > 如何在 qml 的中继器中使用工具分隔符?

问题描述

Toolseparator 在中继器内不起作用。我需要在每个内容之后的那一行下显示图像和文本,所以为了显示我使用工具分隔符但它不起作用的行,它只是通过仅显示行来覆盖所有内容。这是示例

Repeater {
  model: 10
  Row {
    leftPadding: 10
    spacing: 10
    Rectangle {
      height: 100
      width: 200
    }
    Text {
      text: "Username"
    }
    ToolSeparator {
      height: 25
      width: 335
      orientation: Qt.Horizontal
    }
  }
}

标签: qtqml

解决方案


你犯了一些错误,我的朋友。

  • 如果你打算使用Repeater——通常你应该在一些定位组件(例如ColumnRow)中使用它。所以在你的情况下,Repeater应该在里面Row。顺便说一句,千万不要忽视查看官方 Qt 文档:Repeater 详细说明使用 QML 定位器和中继器项目。除此之外,还有很棒的学习资源 QmlBook,例如它的Quick Starter

  • 如果你想在另一个下显示你的组件——你应该使用Column组件,而不是Row.


如果我对你的理解正确,你会得到这样的东西:

申请截图

使用此代码作为参考:

 Column {
    spacing: 10

    Repeater {
      model: 4

      Rectangle {
        id: background
        height: 50
        width: 200
        color: "bisque"

        Text {
          id: text
          color: "chocolate"
          text: "Username " + modelData
          anchors.centerIn: parent
        }

        Rectangle {
          id: separator
          anchors {
            bottom: parent.bottom
            left: parent.left
            right: parent.right
          }
          height: 5
          color: "goldenrod"
        }
      }
    }
  }

推荐阅读