Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] QQmlListProperty viewing from QML
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QQmlListProperty viewing from QML

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.8k 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.
  • E Offline
    E Offline
    Elegant
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Hi,

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

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Elegant
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Elegant
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              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 ;)

              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