QQmlListProperty<T> : Why does the following getter function work?

609 Views Asked by At

i am a newbie in qml and have difficulties to understand example code for the use of QQmlListProperty:

I do not understand this getter function in the chartitem.cpp file (without any reference to the private m_bars in the chartitem.h file) :

QQmlListProperty<BarItem> ChartItem::bars()
{
    return QQmlListProperty<BarItem>(this, 0,
                &ChartItem::append_bar,0, 0, 0);
    // where is the reference to m_bars ?
}

Which data will be returned ? There is no reference to the private Qlist<BarItem*> m_bars which should contain the returned data.

Here are the important code snippets of the header and implementation files ...

/*---------- chartitem.h file : -----------*/
    class ChartItem : public QQuickPaintedItem
    {
        Q_OBJECT
        Q_PROPERTY(QQmlListProperty<BarItem> bars READ bars NOTIFY barsChanged)
    public:
        ChartItem(QQuickItem *parent = 0);
        void paint(QPainter *painter);
        QQmlListProperty<BarItem> bars();
        ...

        Q_SIGNALS:
        void barsChanged();
    private:
        static void append_bar(QQmlListProperty<BarItem> *list, BarItem *bar);
        QList<BarItem*> m_bars;
        ...
    }
    /*-----------------------------------------*/


/*------------- chartitem.cpp file --------*/
...
QQmlListProperty<BarItem> ChartItem::bars()
{
    return QQmlListProperty<BarItem>(this, 0,
                &ChartItem::append_bar,0, 0, 0);
    // where is the reference to m_bars ?
}

void ChartItem::append_bar(QQmlListProperty<BarItem> *list, BarItem *bar)
{
ChartItem *chart = qobject_cast<ChartItem *>(list->object);
if (chart) {
    bar->setParent(chart);
    chart->m_bars.append(bar);
    chart->barsChanged();
}
...
/*-----------------------------------------*/

Can someone explain my error in reasoning ? Thank you in advance.

1

There are 1 best solutions below

0
On

Look at the public members of QQmlListProperty. The only thing that would allow you to observe the data is operator==. So it doesn't matter that the data isn't present, because no-one can observe it's absence.

Presumably the only thing you can do with bars is add elements to the underlying ChartItem (by some QML magic), because that is the only operation provided. In some sense it is thus a write-only property