Insérer un objet perso dans une QList
Bonjour à tous,
J'ai une classe perso qui se nomme : Annotation.
Dans mon prog principal, je déclare ceci :
Code:
QList<Annotation *> liste_annotations;
J'ai une fonction qui lit un morceau de fichier XML, me charge une combobox et met les objets dans la liste :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| void MainWindow::chargerCombo(QDomElement annotation,QString leDossier){
Annotation *lannot;
QString lenom,source;
int i=0,taille=0;
while(!annotation.isNull()){
source = annotation.attribute("src","");
lenom = annotation.attribute("name","");
//On créer l'objet Annotation
lannot = creerObjetAnnotation(source,leDossier);
//On insérer l'annotation dans la liste liste_annotations
liste_annotations<<lannot;
taille = liste_annotations.length();
//On insère le nom de l'annotation dans la combobox
ui->annotations_cb->insertItems(i,QStringList()<<lenom);
//On passe à la suivante (NULL s'il n'y a pas de suivant)
annotation = annotation.nextSibling().toElement();
//On incrémente le compteur
i++;
}
} |
Ma fonction qui créer l'objet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| //Fonction qui créer l'objet Annotation
Annotation * MainWindow::creerObjetAnnotation(QString source,QString leDossier){
Annotation *mon_annotation = new Annotation();
//Lecture de l'arborescence
QDomDocument doc;
QFile f(leDossier+source);
f.open(QIODevice::ReadOnly);
doc.setContent(&f);
f.close();
QDomElement root=doc.documentElement();
mon_annotation->setId(root.attribute("acquisitionId","-1").toInt());
QDomElement child = root.firstChild().toElement();
/*Plutôt que d'utiliser un constructeur qui sera illisible en cas de maintenance
nous créons l'objet par étapes*/
mon_annotation->setNom(child.attribute("name","Aucun nom"));
mon_annotation->setIdPraticien(child.attribute("authorId","-1").toInt());
QDomElement grandchild = child.firstChild().toElement();
mon_annotation->setType(grandchild.attribute("type","aucun type"));
mon_annotation->setType_x(grandchild.attribute("x","-1").toInt());
mon_annotation->setType_y(grandchild.attribute("y","-1").toInt());
mon_annotation->setType_w(grandchild.attribute("w","-1").toInt());
mon_annotation->setType_h(grandchild.attribute("h","-1").toInt());
mon_annotation->setR(grandchild.attribute("r","-1").toInt());
mon_annotation->setG(grandchild.attribute("g","-1").toInt());
mon_annotation->setB(grandchild.attribute("b","-1").toInt());
mon_annotation->setAlpha(grandchild.attribute("alpha","-1").toInt());
grandchild = grandchild.nextSibling().toElement();
mon_annotation->setOrigine_x(grandchild.attribute("x","-1").toInt());
mon_annotation->setOrigine_y(grandchild.attribute("y","-1").toInt());
mon_annotation->setOrigine_z(grandchild.attribute("z","-1").toInt());
grandchild = grandchild.nextSibling().toElement();
mon_annotation->setCommentaires(grandchild.text());
return mon_annotation;
} |
Le problème est que l'insertion dans la liste_annotations se fait mal : en mode débug, tout est bon (les objets existent et sont cohérents). La première insertion se fait bien, taille = 1 et je peux voir l'objet dans la fenêtre des variables.
Par contre, pour le 2e objet (il n'y a que 2 objets dans mes tests), l'insertion écrase le premier au lieu de le mettre au bout de la QList : je n'ai donc qu'un seul objet à la fin dans ma liste.
J'ai essayé les méthodes : append, push_back et insert(index, objet) -> d'où mon compteur i. Mais rien n'y fait, toujours le même problème :s
Pouvez vous m'aider ?
Merci d'avance xD