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. Can I construct a custom Qml item that contains a placeholder child?
Forum Updated to NodeBB v4.3 + New Features

Can I construct a custom Qml item that contains a placeholder child?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.5k 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.
  • C Offline
    C Offline
    cscooper
    wrote on last edited by
    #1

    I have a series of items that I need to apply the same OpacityMask to, and to simplify my code I'd like to create something like this:

    MaskedItem.qml:
    @Item {
    MyMaskSource {
    id: maskSource
    anchors.fill: parent
    visible: false
    }

    (Placeholder) {
        id: item
        anchors.fill: parent
        visible: false
    }
    
    OpacityMask {
        anchors.fill: parent
        source: item
        maskSource: maskSource
    }
    

    }
    @

    That way, in my main Qml, I can do something like this:

    @RowLayout
    {
    MaskedItem {
    Image {
    source: "Image1.png"
    }
    }
    MaskedItem {
    Image {
    source: "Image2.png"
    }
    }
    }
    @

    But I can't figure out how to do that "placeholder" in my MaskedItem. I thought about doing some JavaScript but that's also eluding me:

    MaskedItem.qml:
    @Item {
    default property Item myItem

    onItemChanged: {
    children.add(myItem);
    myItem.visible = false;
    myItem.anchors.fill = this;
    OpacityMask* mask = new OpacityMask;
    mask.anchors.fill = this;
    mask.source = myItem;
    mask.maskSource = maskSource;
    children.add(mask);
    }

    MyMaskSource {
        id: maskSource
        anchors.fill: parent
        visible: false
    }
    

    }
    @

    Any suggestions? I'm pretty new to Qml so I may be missing something obvious!

    Thanks,
    Chris

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Have you tried making your item the default property (http://qt-project.org/doc/qt-5/qtqml-syntax-objectattributes.html#default-properties) of MaskedItem?

      Alternatively you could try working with a Loader element.

      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