<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Custom c++ QQuickItem is not visible in QML rendering]]></title><description><![CDATA[<p dir="auto">The custom item is registered and loads in the QML with no problem.  I just can't see any output.  One problem is that it has no height and width.  Though, I set it manually to bypass that problem for now.  The next problem is that it does not render.  I have debugged the code, and I found the instantiation of the QComponent works correctly (by checking the children in the debugger).  The width is set and the children are present before it's parent is set to the engine and it's parent item is set to the RootedLoader.  Any help with this problem would be greatly appreciated.  Thanks!</p>
<p dir="auto"><strong>RootedLoader.h:</strong></p>
<pre><code>#ifndef ROOTEDLOADER_H
#define ROOTEDLOADER_H

#include &lt;QQmlEngine&gt;
#include &lt;QQuickItem&gt;

class RootedLoader : public QQuickItem
{
    Q_OBJECT
    Q_PROPERTY( QString root READ root WRITE set_root NOTIFY root_changed REQUIRED )
    Q_PROPERTY( QString location READ location WRITE set_location NOTIFY location_changed )
    QML_ELEMENT

protected:
    QString _root;
    QString _location;

public:

    RootedLoader( QQuickItem* parent = nullptr );

    QString location() const;
    void set_location( const QString&amp; location );

    QString root() const;
    void set_root( const QString&amp; root );

protected:
    virtual void componentComplete() override;

    void load();

signals:
    void root_changed( const QString&amp; location );
    void location_changed( const QString&amp; location );

};

#endif // ROOTEDLOADER_H```
</code></pre>
<p dir="auto"><strong>RootedLoader.cpp:</strong></p>
<pre><code>#include "RootedLoader.h"
#include &lt;QQmlContext&gt;
#include &lt;QQmlComponent&gt;



#include "application_engine.h"

RootedLoader::RootedLoader( QQuickItem* parent ):
    QQuickItem( parent )
{
    setFlag( QQuickItem::ItemHasContents, true );
}

QString RootedLoader::location() const
{
    return _location;
}

void RootedLoader::set_location( const QString&amp; location )
{
    _location = location;

    if( isComponentComplete() )
        load();
}

QString RootedLoader::root() const
{
    return _root;
}

void RootedLoader::set_root( const QString&amp; root )
{
    _root = root;
}

void RootedLoader::componentComplete()
{
    load();
}

void RootedLoader::load()
{
    auto item_width = width();
    auto item_height = height();

    qDebug() &lt;&lt; item_width;
    qDebug() &lt;&lt; item_height;

//    auto url = QUrl( _root + _location );
    auto url_string = QString( "qrc:/law/lawtree/testpage.qml" );
    auto url = QUrl( url_string );

    QQmlComponent* location_component = new QQmlComponent( application_engine::engine(), url );
    QQuickItem* instance = qobject_cast&lt;QQuickItem*&gt;( location_component-&gt;create() );

    if( childItems().count() &gt; 0 )
    {
        QQuickItem* item = this-&gt;childItems().at( 0 );

        item-&gt;setParentItem( NULL );
        instance-&gt;setParentItem( this );
        instance-&gt;setParent( application_engine::engine() );

        item-&gt;setParent( NULL );
        item-&gt;deleteLater();
    }
    else
    {
        instance-&gt;setParent( application_engine::engine() );
        instance-&gt;setParentItem( this );
    }
}
</code></pre>
<p dir="auto"><strong>main.qml:</strong></p>
<pre><code>import QtQuick
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

import CustomComponents 1.0
import "."

ApplicationWindow
{
    title: qsTr( "Law and Order" )
    width: 540
    height: 960
    visible: true

    StackLayout
    {
        id: view_stack
        anchors.fill: parent
        currentIndex: 1

        LawTree
        {
            id: law_tree
        }

        Rectangle
        {
            border.width: 4
            border.color: "blue"

            Column
            {
                anchors.fill: parent

                Text { text: "testing" }

//                TestQuickItem
//                {
//                    anchors.centerIn: parent
//                    width: parent.width/2
//                    height: parent.height/2
//                }

// note that TestQuickItem displays without a problem using updatePaintNode to paint a triangle 

                RootedLoader
                {
                    id: loader
                    root: "qrc:/law/lawtree"
                    location: "/testpage.qml"
                    anchors.fill: parent
                    width: 200
                    height: 400
                }
            }
        }

    }

}
</code></pre>
<p dir="auto"><strong>testpage.qml (loaded from RootedLoader):</strong></p>
<pre><code>Item
{
    id: view

    property string law_description : "law description ****"
    property string order_description : "order description ****"
    property string coordination_description : "coordination description ****"
    property string preawareness_description : "preawareness description ****"

    anchors.fill: parent

    Rectangle
    {
        anchors.fill: parent
        border.color: "red"
        border.width: 4

        Column
        {
            anchors.fill: parent

            Row
            {
                Text { text: "law:" }

                Text {
                    id: law_description_text
                    text: view.law_description
                    color: "blue"
                }
            }

            Row
            {
                Text { text: "order:" }

                Text {
                    id: order_description_text
                    text: view.order_description
                    color: "blue"
                }
            }

            Row
            {
                Text { text: "coordination:" }

                Text {
                    id: coordination_description_text
                    text: view.coordination_description
                    color: "blue"
                }
            }

            Row
            {
                Text { text: "preawareness:" }

                Text {
                    id: preawareness_description_text
                    text: view.preawareness_description
                    color: "blue"
                }
            }
        }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/133983/custom-c-qquickitem-is-not-visible-in-qml-rendering</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 23:16:42 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/133983.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 31 Jan 2022 21:18:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Custom c++ QQuickItem is not visible in QML rendering on Wed, 01 Feb 2023 20:38:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ariyo_walker">@<bdi>Ariyo_Walker</bdi></a>  I have exactly the same problem and I don't know how to figure it out...<br />
I'm trying qquickPaintedItem instead of QQuickItem inherit but now I got an error Element is not Creatable</p>
<p dir="auto">Anyone have I idea how can we proceed ?</p>
<p dir="auto">Thanks you in advance</p>
]]></description><link>https://forum.qt.io/post/745927</link><guid isPermaLink="true">https://forum.qt.io/post/745927</guid><dc:creator><![CDATA[JorisC]]></dc:creator><pubDate>Wed, 01 Feb 2023 20:38:17 GMT</pubDate></item></channel></rss>