c++qtqmlqquickitem

QQuickItem parent item null in constructor


I am interested in accessing attributes of a qml parent through a c++ QQuickItem. I have a custom QQuick item called VisibleTag the extends QQuickItem. Any qml item containing this object Tag, I would like set as visible or invisible based off other factors I set in my code that I temporarily removed for the purposes of this question. However, I am having an issue where my parent pointer is null on construction.

//main.cpp
#include <QtQuick/QQuickView>
#include <QGuiApplication>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterType<VisibleTag>("VisibleTag", 1, 0, "VisibleTag");

    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.setSource(QUrl("qrc:///app.qml"));
    view.show();
    return app.exec();
}

//app.aml
Rectangle{
    id: opPic
    height: 100
    width: 100
    color: "red"
    VisibleTag{}
}
//header
class VisibleTag : public QQuickItem
{
    Q_OBJECT
public:
    VisibleTag( QQuickItem* parent = nullptr );

private:
    bool isVisible() { return false; } //this is a dummy function for testing my issue
}
//cpp
VisibleTag::VisibleTag( QQuickItem* parent )
    : QQuickItem( parent )
{
    //qDebug() << parent->objectName(); //This line will break because parent is null
    parent->setVisible( isVisible() );
}

I would expect instead to have the parent pointer to point to the qml's visual parent item. In the example, I would expect parent to point to Rectangle opPic.

Am i misunderstanding how the QQuickItem constructor works? Is is possible to access a qml visual parent?


Solution

  • The construction of an QQuickItem by QML is not:

    T* o = new T(parent);
    

    but

    T* o = new T;
    T->setParentItem(parent);
    

    So you can't get the parent in the constructor but you have to do it in the componentComplete() method (similar to Component.onCompleted in QML):

    #ifndef VISIBLETAG_H
    #define VISIBLETAG_H
    
    #include <QQuickItem>
    class VisibleTag : public QQuickItem
    {
        Q_OBJECT
    public:
        VisibleTag(QQuickItem *parent=nullptr);
    protected:
        void componentComplete();
    private:
        bool dummy() { return false; }
    };
    
    #endif // VISIBLETAG_H
    
    #include "visibletag.h"
    
    VisibleTag::VisibleTag(QQuickItem *parent):QQuickItem(parent)
    {
    }
    void VisibleTag::componentComplete()
    {
        if(parentItem())
            parentItem()->setVisible(dummy());
    }