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);
Forum Updated to NodeBB v4.3 + New Features

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 944 Views 1 Watching
  • 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 16 Oct 2015, 11:53 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
    • A Offline
      A Offline
      Arty.McLabin
      wrote on 16 Oct 2015, 21:42 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.

      J 1 Reply Last reply 19 Oct 2015, 05:05
      0
      • A Arty.McLabin
        16 Oct 2015, 21:42

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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 19 Oct 2015, 05:05 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

        3/3

        19 Oct 2015, 05:05

        • Login

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