Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Lost of the pointers of the array when I the new objects calls ui->setupUi(this);
QtWS25 Last Chance

Lost of the pointers of the array when I the new objects calls ui->setupUi(this);

Scheduled Pinned Locked Moved General and Desktop
ui-setupuithislost pointers
3 Posts 3 Posters 931 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Genzo
    wrote on last edited by
    #1

    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);
    }//for

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

    YYYgetTest: secondo
    YYYgetNameIngredient():Coffee
    YYYgetTest: secondo
    YYgetNameIngredient():
    YYYgetTest: secondo
    YYY getNameIngredient():

    Instead, if I comment
    ui->setupUi(this);
    everything works well

    1 Reply Last reply
    0
    • Arty.McLabinA Offline
      Arty.McLabinA Offline
      Arty.McLabin
      wrote on last edited by
      #2

      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 :)

      Static linking is cool. Really.

      jsulmJ 1 Reply Last reply
      0
      • Arty.McLabinA Arty.McLabin

        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 :)

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved