Lost of the pointers of the array when I the new objects calls ui->setupUi(this);
-
Hi,
my array of pointers loose them when I create a new class and it calls "ui->setupUi(this);"
These are the relevant parts of the code.detail.cpp
std::vector<Ingredient * > vIngredients;
loadIngredientsXml("xml/ingredients.xml",vIngredients);
...
std::cout<<"\nXXXgetTest(): "<<vIngredients[0]->getvTest();
std::cout << "\nXXXgetNameIngredient():" << vIngredients[0]->getNameIngredient();
for(int i = 0; i < vIngredients.size(); i++)
{
std::cout<<"\nYYYgetTest(): "<<vIngredients[i]->getvTest(); //these will not be loose
std::cout << "\nYYYgetNameIngredient():" << vIngredients[i]->getNameIngredient(); //these pointer will be loose when TestForm() will invoke ui->setupUi(this);
TestForm * tf;
tf = new TestForm(); //this is an empty widget form
}testform.cpp
TestForm::TestForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::TestForm)
{
ui->setupUi(this); //if this line is commented, I do not lose the pointer
}ingredient.h
class Ingredient
{
public:
Ingredient();
Ingredient(std::vector<char const * > _vTest,std::vector<char const * > _vName):
vTest(_vTest),
vName(_vName){}
char const * getvTest(){return vTest[0];}
char const * getNameIngredient(){return vName[0];}
private:
std::vector<char const * > vName;
std::vector<char const * > vTest;
...
Ingredient.cpp
void loadIngredientsXml(const char * filename, std::vector<Ingredient * >& vIngredient){
Ingredient * pIngredient;
...
//open the xml document
tinyxml2::XMLDocument doc;
if(doc.LoadFile(filename) == tinyxml2::XML_NO_ERROR){
...
//INGREDIENT
tinyxml2::XMLElement* listIngredients;
int i = 0;
for( listIngredients = pRoot->FirstChildElement("ingredient"); listIngredients; listIngredients = listIngredients->NextSiblingElement("ingredient") )
{
std::vector<Propriety * > vPropriety;
std::vector<char const * > vName;
std::vector<char const * > vTest;
vTest.push_back("primo");
vTest.push_back("secondo");
//INGREDIENT //name
for(tinyxml2::XMLElement* name = listIngredients->FirstChildElement("name")->FirstChildElement(); name != NULL; name = name->NextSiblingElement())
{
vName.push_back(namePropriety);
}
...
pIngredient = new Ingredient(vTest,vName);
vIngredient.push_back(pIngredient);
}//forI have this output:
XXXvTest from detail: secondo
XXXvTest from detail: CoffeeYYYgetTest: secondo
YYYgetNameIngredient():Coffee
YYYgetTest: secondo
YYgetNameIngredient():
YYYgetTest: secondo
YYY getNameIngredient():Instead, if I comment
ui->setupUi(this);
everything works well -
please, use QVector rather than std::vector
i know it may be a matter of habit, but Qt has it's alternatives for most std:: features, which work better with Qt classes and framework overall (and they are awesome besides it, you will love them, i promise.)also mind i ask why are you using std::cout? do your project have a graphical interface?
if(has){
cout is binded to the program software, which will not be available (probably) to the user, but still will consume resources for work. not effective. if you use it for debug, there is "QDebug.h" with "qDebug() << "debug code" which will work the same for you, but will not be compiled in a release version (fun).
}
else
you don't need ui->setupUi(this); because guess what, ui stands for "user interface"good luck :)
-
@Arty.McLabin The application seems to have a UI, so calling ui->setupUi(this); is perfectly valid. What is strange: in detail.cpp it is called in a for loop (setupUi() should be called only once).
std::cout is always available as it is the standard output, the user just needs to start the app from a command line (same for qDebug()). But you're right it is better to use qDebug() in Qt applications for debug output.