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. how to retrieve list element
Forum Updated to NodeBB v4.3 + New Features

how to retrieve list element

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 281 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi all -

    This one's a little complicated; I've simplified the code as much as possible.

    I have a list of zones; each zone has its own list of equipment. I'm trying to retrieve the equipment list using a model, based on the currently selected zone in the zone list, then retrieving each equipment item using a delegate.

    Where I'm stuck is how to code the retrieval of the equipment list element. Here's my code:

    class EquipmentItem
    {
        Q_GADGET
        Q_PROPERTY(QString name MEMBER m_name)
        QString m_name;
    public:
        Q_INVOKABLE QString name() const { return m_name; }
    }
    
    typedef QList<QUuid> ZoneEquipmentList;
    
    class Zone {
        Q_GADGET
        ZoneEquipmentList m_equipmentList;
    public:
        Q_PROPERTY(ZoneEquipmentList equipmentList MEMBER m_equipmentList)
        ZoneEquipmentList equipmentList() const { return m_equipmentList; }
    }
    
    class ZoneModel: public QAbstractListModel
    {
        Q_OBJECT
    private:
        ZoneList *m_list;
    public:
        Q_INVOKABLE Zone getZone(int index) {
            Zone z = Zone();
            if (m_list->size() >= index + 1) {
                z = m_list->getItem(index);
            }
            return z;
        }
    }
    
    GridView {
        id: equipmentView
    
        model: zoneModel.getZone(zoneSelected).equipmentList
        delegate: Text{
            id: card
            text: // ??? what do I do here???
        }
    }
    

    Thanks...this one has me going cross-eyed.

    EDIT: I probably should have mentioned that when I try this:

    delegate: Text {
        text: "Equipmentscreen.qml: " + model.name
    }
    

    then "model.name" comes back as undefined. Could this have something to do with roles? My equipment model has them, but the individual equipment item class does not.

    Thanks...

    mzimmersM 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      This one's a little complicated; I've simplified the code as much as possible.

      I have a list of zones; each zone has its own list of equipment. I'm trying to retrieve the equipment list using a model, based on the currently selected zone in the zone list, then retrieving each equipment item using a delegate.

      Where I'm stuck is how to code the retrieval of the equipment list element. Here's my code:

      class EquipmentItem
      {
          Q_GADGET
          Q_PROPERTY(QString name MEMBER m_name)
          QString m_name;
      public:
          Q_INVOKABLE QString name() const { return m_name; }
      }
      
      typedef QList<QUuid> ZoneEquipmentList;
      
      class Zone {
          Q_GADGET
          ZoneEquipmentList m_equipmentList;
      public:
          Q_PROPERTY(ZoneEquipmentList equipmentList MEMBER m_equipmentList)
          ZoneEquipmentList equipmentList() const { return m_equipmentList; }
      }
      
      class ZoneModel: public QAbstractListModel
      {
          Q_OBJECT
      private:
          ZoneList *m_list;
      public:
          Q_INVOKABLE Zone getZone(int index) {
              Zone z = Zone();
              if (m_list->size() >= index + 1) {
                  z = m_list->getItem(index);
              }
              return z;
          }
      }
      
      GridView {
          id: equipmentView
      
          model: zoneModel.getZone(zoneSelected).equipmentList
          delegate: Text{
              id: card
              text: // ??? what do I do here???
          }
      }
      

      Thanks...this one has me going cross-eyed.

      EDIT: I probably should have mentioned that when I try this:

      delegate: Text {
          text: "Equipmentscreen.qml: " + model.name
      }
      

      then "model.name" comes back as undefined. Could this have something to do with roles? My equipment model has them, but the individual equipment item class does not.

      Thanks...

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      Well, I figured out one way to do it, though I'm not at all sure it's the best way to do it.

      delegate: Text {
          property string equipmentName: equipmentModel.getItem(model.modelData).name()
          text: "Equipmentscreen.qml: " + equipmentName
      }
      

      So, the rather circuitous route is:

      • go to the zone model to get the selected zone
      • from the zone, get its equipment list
      • for each item in the list (via the delegate), use the UUID to look up the index in the equipment list
      • extract its name and store in a QML string

      Similar logic for all other equipment fields I want to report on.

      It works, but before I mark this as solved, I'd be interested in critique.

      Thanks...

      1 Reply Last reply
      0
      • mzimmersM mzimmers has marked this topic as solved on
      • mzimmersM mzimmers has marked this topic as unsolved on
      • mzimmersM mzimmers has marked this topic as solved on

      • Login

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