Not able to use QList<Class> ComponentA (solved)
-
My code is in pastebin, http://pastebin.com/JwKjfW0b
Like to ask how to read out the data in the class ( CComponent ) -
@houmingc You will need to write getter methods as well which will return the values stored in those variables.
-
Code below is not working although no compiler error
QList<CComponent> list; QListIterator<CComponent> i(list); while(i.hasNext()){ qDebug()<<"xxxx is "<<ComponentTemplate.ComponentX; }
-
@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. -
@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(); }
-
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()); -
qDebug() << ComponentList[0].GetComponentHeight(); // why accessing ComponentList array return error?
-
@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 ? -
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 QLinkedListQLinkedList<CComponent>::iterator iterator; for(iterator=ComponentList.begin();iterator != ComponentList.end(); iterator ++) { qDebug()<<iterator->GetComponentAspectRatio()<<endl; }
-
@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