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?