Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. List of Object
Qt 6.11 is out! See what's new in the release blog

List of Object

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vetryaspa
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @
        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.

        (Z(:^

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vetryaspa
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved