[SOLVED] QQmlListProperty viewing from QML



  • To give a brief summary of my project. I am trying to convert my hierarchy:

    PetStage -> PetTeam -> Pet -> PetAbility & PetAura
    -> means consists of

    I want to send my main petStage to the QML engine which I have. I then want to access the QVector<PetTeam*> within it so I used QQmlListProperty<PetTeam> using it's QList<PetTeam*> constructor (by using the the toList() function). The next step in this venture was to make sure QML knew what a PetTeam was, so I used qmlRegisterType<PetTeam>(). Everything played nice!

    Q_PROPERTY(QQmlListProperty<PetTeam> petTeams READ GetTeams)
    qmlRegisterType<PetTeam>();

    Now in QML I am trying to get the script file to print information about objects. Initially I printed the petStage and got it's memory address which was perfect. Next I wanted to get the PetTeam list. So I tried "petStage.petTeams" which gave me [object Object] as an output. I'm not quite sure why it's giving me that but I thought I'd roll with it. The problem is since petTeams should be a list, petStage.petTeams[0] should actually return something (memory address, anything!) but it crashes my program. Given the documentation on QQmlListProperty, my implementation seems correct but I don't seem to know how to access it correctly.

    Ultimately I want to be able to use the line petStage.petTeams[ 0 ] and more. If this idea doesn't really pan out I might just paste all kinds of Q_INVOKABLE functions to get the access I need.



  • Hi,

    Can you post the code where you create the QQmlListProperty<PetTeam>, in other words GetTeams?



  • @QQmlListProperty<PetTeam> PetStage::GetTeams()
    {
    return QQmlListProperty<PetTeam>(this, petTeams.toList());
    }@

    I also tried storing this as a variable to no avail. I also use "this" as the no team can exist without a stage. The variable petTeams the the QVector mentioned earlier, it is a private variable.



  • This is from the documentation:


    Convenience constructor for making a QQmlListProperty value from an existing QList list. The list reference must remain valid for as long as object exists. object must be provided.

    Generally this constructor should not be used in production code, as a writable QList violates QML's memory management rules. However, this constructor can very useful while prototyping.

    You are problem is not this, but the petTeams.toList() I think. petTeams.toList() creates a QList object which is only valid until line 4 of your code:

    @ QQmlListProperty<PetTeam> PetStage::GetTeams()
    {
    return QQmlListProperty<PetTeam>(this, petTeams.toList());
    }@

    Try for testing purposes to make a dummy QList<PetTeam> member variable, and returning that to see if your app still crashes.



  • Wow, I can't believe that was it! I thought it would persist given that it's all pointers and it's source still exists. I am now printing the address of the array at the given index. Thank you so much! I'll be switching to QList in light of this, I think I've lost the purpose of QVector since the team size and abilities cap out at 3.



  • Well your QVector still persists obviously, but the QList that is returned when calling toList() does not.
    Marked the thread as solved when you're done ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.