Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved]How to implement Delegate in seperate file?
Qt 6.11 is out! See what's new in the release blog

[solved]How to implement Delegate in seperate file?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kathy
    wrote on last edited by
    #1

    I have a small QML project using GridView and XmlListModel to diaplay images:

    in my main.qml -
    @import QtQuick 1.0
    import "Shared" as SHARED

    Rectangle {
    id: mainPage
    width: 520; height: 640
    color: "#000000"

    SHARED.MyModel { id: xmlModel }
    

    // SHARED.GridDelegate { id: theDelegate }

    Component {
        id: theDelegate
        Item {
            width: 130; height: 130
            Image {
                id: thumbnailImg
                source: thumbPath
                x: 1
                y: 1
                anchors.centerIn: parent
                smooth: true
            }
        }
    }
    
    GridView {
        id: photoGridView
        model: xmlModel
        delegate: theDelegate
    
        cacheBuffer: 100
        cellWidth: (parent.width-2)/4; cellHeight: cellWidth; width: parent.width; height: parent.height
    }
    

    }@

    the GridDelegate.qml -
    @Item {
    width: 130; height: 130
    Image {
    id: thumbnailImg
    source: thumbPath
    x: 1
    y: 1
    anchors.centerIn: parent
    smooth: true
    }
    }@
    If I use the delegate defined in the main.qml. It run as expected.

    But If I use the GridDelegate.qml file defined in the subdirectory "Shared" and keep all other no change. No image show up.

    MyModel.qml -
    @XmlListModel {
    source: "./img.xml"
    query: "/root/item"

    XmlRole { name: "title"; query: "title/string()" }
    XmlRole { name: "thumbPath"; query: "thumbPath/string()" }
    XmlRole { name: "imgPath"; query: "imgPath/string()" }
    

    }@

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wladek
      wrote on last edited by
      #2

      Hi kathy,

      The Delegate must be a Component.
      So the GridDelegate.qml has to look like this:
      @Component{
      Item {
      width: 130; height: 130
      Image {
      id: thumbnailImg
      source: thumbPath
      x: 1
      y: 1
      anchors.centerIn: parent
      smooth: true
      }
      }
      }@

      Regards,
      Wladek

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved