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. Not able to use QList<Class> ComponentA (solved)
QtWS25 Last Chance

Not able to use QList<Class> ComponentA (solved)

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 3.7k Views
  • 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.
  • A Offline
    A Offline
    alvinNew
    wrote on last edited by p3c0
    #1

    I am able to read XML element into variable.
    I need to read XML element into a container QList, thus created the 2 class.
    and append the read element into the object.

    class CComponent{
    public:
        QString ComponentID;
        QString ComponentType;
        int ComponentX;
        int ComponentY;
        int ComponentWidth;
        int ComponentHeight;
        int ComponentZOrder;
        int ComponentAspectRatio;
    
    };
    class CBackground{
    public:
        int Transparent;
        int Red;
        int Green;
        int Blue;
    };
    
        QList<CComponent> ComponentA;
        QList<CBackground> BackgroundA;
    
        while (xmlGet.findNextAndDescend("Component"))
        {
            QString ComponentID = xmlGet.getAttributeString("ID","unknown");
            QString ComponentType = xmlGet.getAttributeString("Type","unknown");
            int ComponentX = xmlGet.getAttributeInt("X");
            int ComponentY = xmlGet.getAttributeInt("Y");
            int ComponentWidth = xmlGet.getAttributeInt("Width");
            int ComponentHeight = xmlGet.getAttributeInt("Height");
            int ComponentZOrder = xmlGet.getAttributeInt("ZOrder");
            int ComponentAspectRatio = xmlGet.getAttributeInt("AspectRatio");
    
            ComponentA.append(ComponentID);
            ComponentA.append(ComponentType);
            ComponentA.append(ComponentX);
            ComponentA.append(ComponentY);
            ComponentA.append(ComponentWidth);
            ComponentA.append(ComponentHeight);
            ComponentA.append(ComponentZOrder);
            ComponentA.append(ComponentAspectRatio);
    
    p3c0P 1 Reply Last reply
    0
    • A alvinNew

      I am able to read XML element into variable.
      I need to read XML element into a container QList, thus created the 2 class.
      and append the read element into the object.

      class CComponent{
      public:
          QString ComponentID;
          QString ComponentType;
          int ComponentX;
          int ComponentY;
          int ComponentWidth;
          int ComponentHeight;
          int ComponentZOrder;
          int ComponentAspectRatio;
      
      };
      class CBackground{
      public:
          int Transparent;
          int Red;
          int Green;
          int Blue;
      };
      
          QList<CComponent> ComponentA;
          QList<CBackground> BackgroundA;
      
          while (xmlGet.findNextAndDescend("Component"))
          {
              QString ComponentID = xmlGet.getAttributeString("ID","unknown");
              QString ComponentType = xmlGet.getAttributeString("Type","unknown");
              int ComponentX = xmlGet.getAttributeInt("X");
              int ComponentY = xmlGet.getAttributeInt("Y");
              int ComponentWidth = xmlGet.getAttributeInt("Width");
              int ComponentHeight = xmlGet.getAttributeInt("Height");
              int ComponentZOrder = xmlGet.getAttributeInt("ZOrder");
              int ComponentAspectRatio = xmlGet.getAttributeInt("AspectRatio");
      
              ComponentA.append(ComponentID);
              ComponentA.append(ComponentType);
              ComponentA.append(ComponentX);
              ComponentA.append(ComponentY);
              ComponentA.append(ComponentWidth);
              ComponentA.append(ComponentHeight);
              ComponentA.append(ComponentZOrder);
              ComponentA.append(ComponentAspectRatio);
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @alvinNew The List ComponentA is QList of CComponent and you are adding everything other than that. That won't work.
      Store those values you extract from XML in an object of class CComponent and then append that object to ComponentA.

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alvinNew
        wrote on last edited by alvinNew
        #3

        I understand and realize that.
        But i do not want to hard-code the variable into ComponentA.
        How to make Component a QList.

        It's ok, i create a Component Template and move the contain variable into list before grabbing new data

        p3c0P 1 Reply Last reply
        0
        • A alvinNew

          I understand and realize that.
          But i do not want to hard-code the variable into ComponentA.
          How to make Component a QList.

          It's ok, i create a Component Template and move the contain variable into list before grabbing new data

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @alvinNew No need to hard code. You just need to store those values in an object of CComponent and add it to list. Define some setter methods in CComponent for storing values.

          class CComponent{
              public:
              QString ComponentID;
              QString ComponentType;
              ...
          
              void setComponentId(QString id) {
              ComponentID = id;
              }
          
              void setComponentType(QString type) {
              ComponentType = type;
              }
          };
          
          //Then during appending
          
          QList<CComponent> ComponentA;
          while (xmlGet.findNextAndDescend("Component"))
          {
                QString ComponentID = xmlGet.getAttributeString("ID","unknown");
                QString ComponentType = xmlGet.getAttributeString("Type","unknown");
          
                 CComponent comp;
                 comp.setComponentId(ComponentID); //store values
                 comp.setComponentType(ComponentType); //store values
          
                 //add that object to QList
                 ComponentA.append(comp);
          }
          

          157

          A 1 Reply Last reply
          0
          • p3c0P p3c0

            @alvinNew No need to hard code. You just need to store those values in an object of CComponent and add it to list. Define some setter methods in CComponent for storing values.

            class CComponent{
                public:
                QString ComponentID;
                QString ComponentType;
                ...
            
                void setComponentId(QString id) {
                ComponentID = id;
                }
            
                void setComponentType(QString type) {
                ComponentType = type;
                }
            };
            
            //Then during appending
            
            QList<CComponent> ComponentA;
            while (xmlGet.findNextAndDescend("Component"))
            {
                  QString ComponentID = xmlGet.getAttributeString("ID","unknown");
                  QString ComponentType = xmlGet.getAttributeString("Type","unknown");
            
                   CComponent comp;
                   comp.setComponentId(ComponentID); //store values
                   comp.setComponentType(ComponentType); //store values
            
                   //add that object to QList
                   ComponentA.append(comp);
            }
            
            A Offline
            A Offline
            alvinNew
            wrote on last edited by
            #5

            @p3c0

            Many thanks, i can learn alot from your simple coding.

            p3c0P 1 Reply Last reply
            0
            • A alvinNew

              @p3c0

              Many thanks, i can learn alot from your simple coding.

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by p3c0
              #6

              @alvinNew You're Welcome :) Happy Coding ...
              Also use ``` (3 backticks), key just above the tab button on keyboard for posting code blocks.

              157

              1 Reply Last reply
              0
              • H Offline
                H Offline
                houmingc
                wrote on last edited by
                #7

                My code is in pastebin, http://pastebin.com/JwKjfW0b
                Like to ask how to read out the data in the class ( CComponent )

                p3c0P 1 Reply Last reply
                0
                • H houmingc

                  My code is in pastebin, http://pastebin.com/JwKjfW0b
                  Like to ask how to read out the data in the class ( CComponent )

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @houmingc You will need to write getter methods as well which will return the values stored in those variables.

                  157

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    houmingc
                    wrote on last edited by houmingc
                    #9

                    Code below is not working although no compiler error

                    QList<CComponent> list;
                    
                       QListIterator<CComponent> i(list);
                       while(i.hasNext()){
                           qDebug()<<"xxxx is "<<ComponentTemplate.ComponentX;
                       }
                    p3c0P 2 Replies Last reply
                    0
                    • H houmingc

                      Code below is not working although no compiler error

                      QList<CComponent> list;
                      
                         QListIterator<CComponent> i(list);
                         while(i.hasNext()){
                             qDebug()<<"xxxx is "<<ComponentTemplate.ComponentX;
                         }
                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      @houmingc You are doing it wrong. First create some getter methods as said earlier. Then you need to cast the item to CComponent in the loop and access the getter methods to get the values stored in it.

                      157

                      1 Reply Last reply
                      0
                      • H houmingc

                        Code below is not working although no compiler error

                        QList<CComponent> list;
                        
                           QListIterator<CComponent> i(list);
                           while(i.hasNext()){
                               qDebug()<<"xxxx is "<<ComponentTemplate.ComponentX;
                           }
                        p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by p3c0
                        #11

                        @houmingc Something like this

                        //CComponent class
                        
                        void CComponent::setId(QString id)
                        {
                            m_id = id;
                        }
                        
                        QString CComponent::getId()
                        {
                            return m_id;
                        }
                        
                        QString m_id, m_type;
                        

                        then to access after appending to list

                        QList<CComponent *> m_list;
                        ...
                        
                        CComponent *ca = new CComponent;
                        ca->setId("one");
                        ca->setType("big");
                        ...
                        
                        m_list.append(ca);
                        
                        ...
                        
                        QListIterator<CComponent*> i(m_list);
                        while (i.hasNext()) {
                            CComponent *comp = qobject_cast<CComponent*>(i.next()); //qobject_cast - if uses QObject
                            qDebug() << comp->getId() << comp->getType();
                        }
                        
                        

                        157

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          houmingc
                          wrote on last edited by houmingc
                          #12

                          i have an error at cast object
                          error: no matching function for call to 'qobject_cast(CComponent* const&)'
                          CComponent comp = qobject_cast<CComponent>(i.next());

                          p3c0P 1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            houmingc
                            wrote on last edited by
                            #13

                            qDebug() << ComponentList[0].GetComponentHeight(); // why accessing ComponentList array return error?

                            1 Reply Last reply
                            0
                            • H houmingc

                              i have an error at cast object
                              error: no matching function for call to 'qobject_cast(CComponent* const&)'
                              CComponent comp = qobject_cast<CComponent>(i.next());

                              p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on last edited by
                              #14

                              @houmingc said:

                              i have an error at cast object
                              error: no matching function for call to 'qobject_cast(CComponent* const&)'
                              CComponent comp = qobject_cast<CComponent>(i.next());

                              Try it exactly how I have posted earlier in the example.

                              qDebug() << ComponentList[0].GetComponentHeight(); // why accessing ComponentList array return error?

                              What error ? ComponentList is a QList, right ?

                              157

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                houmingc
                                wrote on last edited by houmingc
                                #15

                                I got the each component display. Thanks.
                                No Casting needed.
                                How to get 3rd item of the class? i am now wondering if QList should be used instead of QLinkedList

                                   QLinkedList<CComponent>::iterator iterator;
                                    for(iterator=ComponentList.begin();iterator != ComponentList.end(); iterator ++)
                                    {
                                        qDebug()<<iterator->GetComponentAspectRatio()<<endl;
                                    }
                                p3c0P 1 Reply Last reply
                                0
                                • H houmingc

                                  I got the each component display. Thanks.
                                  No Casting needed.
                                  How to get 3rd item of the class? i am now wondering if QList should be used instead of QLinkedList

                                     QLinkedList<CComponent>::iterator iterator;
                                      for(iterator=ComponentList.begin();iterator != ComponentList.end(); iterator ++)
                                      {
                                          qDebug()<<iterator->GetComponentAspectRatio()<<endl;
                                      }
                                  p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on last edited by p3c0
                                  #16

                                  @houmingc Casting will be needed if you use QListIterator.

                                  QList<CComponent*> m_list;
                                  
                                  CComponent *ca = new CComponent;
                                  ca->setId("one");
                                  ca->setType("big");
                                  
                                  m_list.append(ca);
                                  
                                  QListIterator<CComponent*> i(m_list);
                                  while (i.hasNext()) {
                                      CComponent *comp = qobject_cast<CComponent*>(i.next()); //qobject_cast - if uses QObject
                                      qDebug() << comp->getId() << comp->getType(); //access using QListIterator
                                  }
                                  
                                  /*-------------------------------------------------------------*/
                                  
                                  qDebug() << m_list[0]->getType() << m_list[0]->getId(); //no need to cast
                                  

                                  157

                                  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