Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

List of Object



  • hi all,

    i have a proble:

    i have an Object call Obj and this object extend QObject, i know that i can't put this Object in a list like this QList<Obj> i need to use the pointer like this but after I have set the pointer in the List the Object will destroied:

    @QList<Oggetto*> InterfacciaDataModel::convertitore(QList<MeteoLocale> lista){

    for(int i=0; i < lista.count(); i++) {
    Oggetto oggetto(lista.at(i).property,lista.at(i).tiplogia);
    listToBeReturn.append(&oggetto);
    }

    cout << "Convertitore" << endl;
    return listToBeReturn;
    }@

    the listToBeReturn is a public list declared in h file like this

    @QList<Oggetto*> listToBeReturn@

    exist a method where i can store this Object???

    regards to all.


  • Moderators

    You should not create QObjects on the stack but use the heap instead. So line 5 should read @ *oggetto = new Ogetto(...);@

    You will then need to delete the object at some point yourself though. One way to make sure that happens is to set a parent object: That will delete all its children once it is destroyed itself.

    PS: You might want to read a book on C++ programming... pointers are covered extensively in all of them. I am sure taking that time will be worth it.


  • Moderators

    @
    Oggetto *oggetto = new Oggetto(lista.at(i).property,lista.at(i).tiplogia);
    listToBeReturn.append(oggetto);
    @

    The object goes out of scope in line 7 of your code, so is destroyed. This is standard c++ behaviour.



  • i know that tha c++ have this behavior...

    But the problem is that in the QML because i have a page whit list, when i click on list it launch another page, but when i'm back and select anothe line to show other content miss the references because the model associated to a ListView remaning the same but the page that contain the list change... is difficult to explain...

    i'm devloping in BB10...


Log in to reply