3

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
piper2200s
  • 151
  • 7
  • the question is not quite clear. What do you want to do when `parent` is a null pointer? – 463035818_is_not_an_ai Feb 10 '20 at 20:15
  • @idclev463035818 I will update the question's wording a bit, but the issue i am having is that I would like it to NOT be a null pointer. I would expect the parent to be set to the Rectangle opPic as it is the visual parent of the VisibleTag. Instead the constructor receives null – piper2200s Feb 10 '20 at 20:18
  • Please post a [mcve]. How do you create a `VisibleTag` ? Do you pass a `parent` to the constructor? – 463035818_is_not_an_ai Feb 10 '20 at 20:24
  • @idclev463035818 VisibleTag is declared using qmlRegisterType, Rectangle is contained in a qml file that is loaded from main. I can add in a main to explain what is happening if it helps but the example above was supposed to me a MRE – piper2200s Feb 10 '20 at 20:28
  • Please note that you don't need a `QQuickItem` to do what you want. If your `VisibleTag` type doesn't have any visual appearance, you can derive it from `QObject` instead. – GrecKo Feb 11 '20 at 09:34

1 Answers1

4

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());
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241