0

I am trying to make a QML Object that I can add an arbitrary number of children to and then manipulate them in C++. I have code that works, but I think that I have the wrong approach, especially since it involves a text search.

void Foo::componentComplete()
{
    QObjectList children = this->children();

    for (int i = 0; i < children.length(); i++)
    {
        if(QString(children[i]->metaObject()->className()).contains("MyClass"))
        {
            this->myChildList.append(static_cast<MyClass*>(children[i]));
        }
    }
}

One example of this can be found in the QML charts example.

Here is one excerpt from the code. Even though the PiceSlice child elements are not assigned to properties of the PieSeries element, they are discovered and displayed by the chart. How does this work under the hood? Is it just using this->children() or is there a better method?

ChartView {
    id: chart
    title: "Top-5 car brand shares in Finland"
    anchors.fill: parent
    legend.alignment: Qt.AlignBottom
    antialiasing: true

    PieSeries {
        id: pieSeries
        PieSlice { label: "Volkswagen"; value: 13.5 }
        PieSlice { label: "Toyota"; value: 10.9 }
        PieSlice { label: "Ford"; value: 8.6 }
        PieSlice { label: "Skoda"; value: 8.2 }
        PieSlice { label: "Volvo"; value: 6.8 }
    }
}

Here is the complete source at Github

Source

user8908459
  • 537
  • 5
  • 24
  • Where is Foo in the qml? – eyllanesc Dec 14 '17 at 17:00
  • You must provide a [mcve] – eyllanesc Dec 14 '17 at 17:02
  • Take a look at my universal object model class here https://stackoverflow.com/questions/35160909/how-to-create-a-generic-object-model-for-use-in-qml - it can be populated declaratively in QML or imperatively from C++ or QML, it is directly usable as a model, it is type agnostic and can be nested to create trees. – dtech Dec 14 '17 at 17:16
  • @eyllanesc I have added the complete source code – user8908459 Dec 14 '17 at 17:23
  • What does your code have to do with PieSeries and PieSlice? – eyllanesc Dec 14 '17 at 17:33
  • I see that it works correctly, why do you say it does not work? – eyllanesc Dec 14 '17 at 17:37
  • This doesn't answer your question, but the path I always use is for QML to read from a C++ model's `Q_PROPERTY` to get some sort of QJSonObject or QJsonArray that the QML iterates over using nested [`Repeater`'s](http://doc.qt.io/qt-5/qml-qtquick-repeater.html) . – Ross Rogers Dec 14 '17 at 17:38

0 Answers0