jeudi 7 septembre 2017

Adding QQuickItem pointers to an std::map makes them all "not accessible" pointers or items

This question involves Qt but could be pure C++ problem with my logic.

I am adding QQuickItems to an std::map store info about a list of QQuickItems & their respective parents.

The code:

std::array<std::string, 2> ObjectNamesArray = { "quickitemObj1", "quickitemObj2" };

std::map<QQuickItem*, QQuickItem*> items;

for(const auto& quickitem: ObjectNamesArray) {
    QQuickItem * item = Qmlengine->rootObjects()[0]->findChild<QQuickItem*>("quickitem.c_str()");
    parents.insert(std::make_pair(item, item->parent());
}

Debugging through above loop, the items map shows zero items with the tag not accessible.

Iterating over the map again like this.

std::map<QQuickItem*, QQuickItem*>::iterator it = items.begin();
while (it != items.end()) {

    QQuickItem* item = it->first;
    QQuickItem * itemParent = it->second;  // crashes here as *item is null
    it++;
}

Problem:

But, when I try to iterate through the map, there are no valid pointers to my QQuickItems. In fact looks like there are not items added to the map.

Question:

What is wrong with my logic? How should I add QQuickItems to an std::map so that I can safely retrieve them back.

Aucun commentaire:

Enregistrer un commentaire