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.

    std::vector<Ingredient * > 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::TestForm(QWidget *parent) :
    ui(new Ui::TestForm)
    ui->setupUi(this); //if this line is commented, I do not lose the pointer

    class Ingredient
    Ingredient(std::vector<char const * > _vTest,std::vector<char const * > _vName):
    char const * getvTest(){return vTest[0];}
    char const * getNameIngredient(){return vName[0];}
    std::vector<char const * > vName;
    std::vector<char const * > vTest;
    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){
    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;
    //INGREDIENT //name
    for(tinyxml2::XMLElement* name = listIngredients->FirstChildElement("name")->FirstChildElement(); name != NULL; name = name->NextSiblingElement())
    pIngredient = new Ingredient(vTest,vName);

    I have this output:
    XXXvTest from detail: secondo
    XXXvTest from detail: Coffee

    YYYgetTest: secondo
    YYYgetTest: secondo
    YYYgetTest: secondo
    YYY getNameIngredient():

    Instead, if I comment
    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?
    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).
    you don't need ui->setupUi(this); because guess what, ui stands for "user interface"

    good luck :)

  • Moderators

    @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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.