[Solved] QList de custom object, QString membre introuvable
Bonjour a tous,
J'ai une class Translations avec un QString en tant que membre appelle "note".
Cette class est utilisee a partir de la class Term.
Comme vous pouvez voir dans le Term.cpp, je fais des tests pour recuperer et juste afficher le member "note", mais malheureusement, je n'arrive pas a mettre la main dessus.
Lorsque je cree l'objet currentTranslations, il n'y a pas de soucis, mais par contre, si je l'ajoute a la QList<Translations> et ensuite je veux l'afficher, impossible...
J'ai regarde, et la QList fait une copie memoire, donc pourquoi quand j'itere ma QList, je ne peux plus afficher mon "note" de mes objets?
Si quelqu'un comprend ou est mon probleme ?
Merci,
Je ne sais pas si j'ai été clair, mais vous pouvez me poser n'importe quelle question ou autre : )
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#ifndef TERM_HPP
#define TERM_HPP
#include <QDebug>
#include <QObject>
#include <QVariant>
#include <QString>
#include <QList>
#include <QMap>
#include <iostream>
#include "Translations.hpp"
class Term
{
private:
QList<Translations> principalTranslations;
QList<Translations> additionalTranslations;
public:
Term () {}
Term (const Term&) {}
//from QVariant
Term (const QVariant& variant) throw (int);
virtual ~Term () {}
//getters
const QList<Translations>
getPrincipalTranslations() const
{ return this->principalTranslations;}
const QList<Translations>
getAdditionalTranslations() const
{ return this->additionalTranslations;}
friend ostream& operator<<(ostream& os, const Term& term)
{
QList<Translations> listTranslations;
QList<Translations>::iterator itTranslations;
listTranslations = term.getPrincipalTranslations();
os << "Principal translations:" << endl;
for(itTranslations = listTranslations.begin(); itTranslations != listTranslations.end(); itTranslations++)
os << (*itTranslations) << endl;
listTranslations = term.getAdditionalTranslations();
os << "Additional translations:" << endl;
for(itTranslations = listTranslations.begin(); itTranslations != listTranslations.end(); itTranslations++)
os << (*itTranslations) << endl;
return os;
}
};
#endif |
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 36 37 38 39 40 41 42 43 44 45
|
#include "Term.hpp"
Term::Term(const QVariant& variant) throw (int)
{
QMap<QString,QVariant> mapVariant = (variant.canConvert(QVariant::Map)) ? variant.toMap() : throw 1;
QMap<QString,QVariant> mapTranslations;
QMap<QString,QVariant>::iterator itMap, itMap2;
Translations currentTranslations;
this->principalTranslations = QList<Translations>();
this->additionalTranslations = QList<Translations>();
for (itMap = mapVariant.begin(); itMap != mapVariant.end(); itMap++)
{
if(itMap.key() == "PrincipalTranslations")
{
mapTranslations = (itMap.value().canConvert(QVariant::Map)) ? itMap.value().toMap() : throw 1;
for (itMap2 = mapTranslations.begin(); itMap2 != mapTranslations.end(); itMap2++)
{
currentTranslations = Translations(itMap2.value());
this->principalTranslations.append(currentTranslations);
cout << "TEST " << endl << currentTranslations << endl;
//if I create an object here I get the "note" member QString pretty easily
cout << "Test : " << Translations(itMap2.value()).getNote().toAscii().constData() << endl;
QList<Translations>::iterator itTranslations;
//if I iterate on the this->.appen which should had been added previously, I don't get anything
for (itTranslations = this->principalTranslations.begin(); itTranslations != this->principalTranslations.end(); itTranslations++)
cout << "TESTTESTES : " << itTranslations->getNote().toAscii().constData() << endl;
Translations testTranslations = Translations(itMap2.value());
cout << "TEST" << endl << testTranslations << endl;
}
}
else if (itMap.key() == "AdditionalTranslations")
{
mapTranslations = (itMap.value().canConvert(QVariant::Map)) ? itMap.value().toMap() : throw 1;
for (itMap2 = mapTranslations.begin(); itMap2 != mapTranslations.end(); itMap2++)
this->additionalTranslations.append(Translations(itMap2.value()));
}
else
{
//other
}
}
} |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#ifndef TRANSLATIONS_HPP
#define TRANSLATIONS_HPP
#include <QDebug>
#include <QObject>
#include <QVariant>
#include <QString>
#include <QList>
#include <QMap>
#include <iostream>
#include "OriginalTerm.hpp"
#include "Translation.hpp"
using namespace std;
class Translations
{
private:
OriginalTerm originalTerm;
QList<Translation> translations;
QString note;
public:
Translations () {}
Translations (const Translations&) {}
//from QVariant
Translations (const QVariant& variant) throw (int);
virtual ~Translations () {}
const OriginalTerm
getOriginalTerm() const
{ return this->originalTerm; }
const QList<Translation>
getTranslations() const
{ return this->translations; }
const QString
getNote() const
{ return this->note;}
friend ostream& operator<<(ostream& os, const Translations& translations)
{
os << "[Original Term]" << endl;
os << "\t" << translations.getOriginalTerm() << endl;
QList<Translation> listTranslation = translations.getTranslations();
QList<Translation>::iterator itTranslation;
for(itTranslation = listTranslation.begin(); itTranslation != listTranslation.end(); itTranslation++)
os << (*itTranslation) << endl;
os << "[Note]" << endl << translations.getNote().toAscii().constData() << endl;
return os;
}
};
#endif |
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
|
#include "Translations.hpp"
//from QVariant
Translations::Translations (const QVariant& variant) throw (int)
{
QMap<QString,QVariant> mapVariant = (variant.canConvert(QVariant::Map)) ? variant.toMap() : throw 1;
QMap<QString,QVariant>::iterator itMap;
this->translations = QList<Translation>();
this->note = "";
for (itMap = mapVariant.begin(); itMap != mapVariant.end(); itMap++)
{
if (itMap.key() == "Note")
{
//we have the note
this->note = itMap.value().toString();
}
else if (itMap.key() == "OriginalTerm")
{
this->originalTerm = OriginalTerm(itMap.value());
}
else
{
//we have to add a translation
this->translations.append(Translation(itMap.value()));
}
}
} |
[Solved] QList de custom object, QString membre introuvable
Avec un exemple plus simple :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
OriginalTerm ot = OriginalTerm();
ot.term = "ot term";
ot.POS = "ot POS";
ot.sense = "ot sense";
ot.usage = "";
QList<OriginalTerm> listOriginal = QList<OriginalTerm>();
QList<OriginalTerm>::iterator itOriginalTerm;
listOriginal.append(ot);
// M'affiche bien tous les champs de OriginalTerm avec ot.term, ot.POS etc.
cout << ot << endl;
for (itOriginalTerm = listOriginal.begin(); itOriginalTerm != listOriginal.end(); itOriginalTerm++)
cout << (*itOriginalTerm) << endl;
// N'affiche plus rien du tout quand j'itere sur ma list |
Up Solved:
Cela est du au constructeur par copie qui est vide.