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. Creating QML Item via function defined in js file, and inserting that item into layout, doesn't show the item

Creating QML Item via function defined in js file, and inserting that item into layout, doesn't show the item

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
layoutinstantiation
3 Posts 2 Posters 1.3k Views
  • 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.
  • S Offline
    S Offline
    Stefan Monov76
    wrote on 31 Jan 2017, 15:48 last edited by
    #1

    Note: the function in QtQuickUtils.js in the following testcase is just to abstract away the boilerplate involved in creating a QML object from a component URL.

    Testcase:

    main.qml:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.3
    import "QtQuickUtils.js" as QtQuickUtils
    
    Window {
        visible: true
        width: 640
        height: 480
    
        GridLayout {
            anchors.fill: parent
            id: container
            columns: 1
        }
    
        Component.onCompleted: {
            QtQuickUtils.createObjectFromComponent("qrc:///MyItem.qml", container, {
                "Layout.fillWidth": true, "Layout.fillHeight": true
                // "width": 100, "height": 100
            });
        }
    }
    

    MyItem.qml:

    import QtQuick 2.0
    
    Rectangle {
        color: "red"
    }
    

    QtQuickUtils.js:

    .import QtQml 2.0 as Qml
    .pragma library
    
    function createObjectFromComponent(componentUrl, parent, properties) {
        var component = Qt.createComponent(componentUrl);
        function finishCreation() {
            console.log("finishCreation");
            if (component.status === Qml.Component.Ready) {
                var obj = component.createObject(parent, properties);
                if (obj === null) {
                    console.log("Error creating object");
                    return;
                }
                console.log("success in creating obj");
            } else if (component.status === Qml.Component.Error) {
                console.log("Error loading component:", component.errorString());
                return;
            }
        }
        if (component.status === Qml.Component.Ready) {
            finishCreation();
        } else {
            component.statusChanged.connect(function() { finishCreation(); });
        }
    }
    

    This shows nothing (but "finishCreation" and "success in creating obj" are printed).

    If I comment out the "Layout.fillWidth": true, "Layout.fillHeight": true line and uncomment the one after that, the item is displayed.

    Also if I move the function from the JS file to main.qml, the item is displayed.

    Any idea what I'm doing wrong, and a proper fix?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 31 Jan 2017, 16:58 last edited by
      #2

      Try specifying the width and height inside myitem.qml

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Stefan Monov76
        wrote on 1 Feb 2017, 16:48 last edited by
        #3

        Thanks but I got a working answer here.

        1 Reply Last reply
        1

        1/3

        31 Jan 2017, 15:48

        • Login

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