【问题标题】:Reusing a delegate component in QML在 QML 中重用委托组件
【发布时间】:2014-07-31 14:48:36
【问题描述】:

我在使用 QML 设计我的应用程序时遇到了一个奇怪的问题。以下代码有效:

TableView {
  itemDelegate: Item {
    function a() {}
    Loader {}
  }
}

我在这个项目委托中有一堆函数、属性和加载器,它是一个项目对象。当我尝试在 ListView 中重用此委托时会出现问题。我不能这样引用它:

Item {
    id: myitem
    function a() {}
    Loader {}
}

TableView {
   itemDelegate: myitem
}

error: Unable to assign QQuickItem to QQmlComponent

这是因为itemDelegateComponent

http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop

因此,QML 可以在嵌入时将 Item 转换为 Component,但在引用时不能。

而且我不能让它成为一个组件,因为组件不能有函数、加载器、属性等。

如何重复使用我的委托?

【问题讨论】:

    标签: qt qml qt5 qqmlcomponent


    【解决方案1】:

    通过将item包裹在组件内解决:

    Component {
       Item{...}
    }
    

    【讨论】:

    • 我也在努力解决这个问题,但你的代码对我不起作用,因为我实际上有一个 listComponents。在我的例子中,我有一个 PageIndicator 内部有一个 listImage 类型组件,我正在尝试将 list 的一个元素分配给 delegate 属性。您是否曾经使用容器为delegate 赋值?
    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多