Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

problem with scope of array??



  • Hi,
    i'm new with c++ and Qt both..
    i've created a form then dinamically create and populate some array..

    0_1537604015979_Schermata 2018-09-22 alle 10.10.09.png

    my problem is that when you press on pushbutton "CALCOLA" i've to manage the data loaded dinamically in the array..

    the array was created with this code:

    QLineEdit *array_Fine15[5];
        for (int i = 0; i<5; i++)
        {
            array_Fine15[i] = new QLineEdit;
            ui->gridLayout_2->addWidget(array_Fine15[i]);
            array_Fine15[i]->setFixedWidth(75);
            array_Fine15[i]->setFixedHeight(24);
            array_Fine15[i]->setText("12.00");
        }
    

    this code:

    0_1537604278358_Schermata 2018-09-22 alle 10.17.13.png

    give my this error:

    no member named 'array_PezziProd' in 'QGridLayout'

    i think was a scope problem but i havn't idea how to solve it.

    can anyone help me please?



  • @TheCipo76
    It's difficult to figure from what you have presented as your code:

    • QLineEdit *array_Fine15[5];
      Is that declaration of an array local to a function (in which case it does not exist once the function has exited) or is that a member variable of a class (in which case it exists as long as an instance of the class exists)?

    After executing that loop, regardless of whether the array itself has or has not gone out of scope, you have created 5 QLineEdits which have been added into ui->gridLayout_2. These line edits do not have their own variables associated with them, but they still exist, e.g. if you searched the widgets on ui->gridLayout_2 they would still be there.

    • ->array_PezziProd[i]
      It's red underlined, indicating it simply does not exist. There is no mention elsewhere in your code of this variable, so where is this declared as a member of the QGridLayout instance which is ui->gridLyout_5? If you think there is any relationship between your array_Fine15 and array_PezziProd, there is not.

    Suggestion: first get your code compiling & working with one line edit, no array. Then start moving it over to allow for an array.

    P.S.
    When posting code, please do not paste an image of code as you have done. It makes it very difficult for anyone trying to comment on it. Always paste code as text into your emails. Put 3 backticks (` character above Tab key on my UK keyboard) before the first line and again after the last line around the code.



  • the arrays was created with code on form load

    ui->setupUi(this);
    
    ... create arrays and add it to general layout
    

    when i lauch application and click on pushbutton (slot / event)

    don't find the array (added as you see in the gridLayout)

    and give me the up-posted error..

    can i solve with associated variables? and how i have to do in this case?



  • @TheCipo76
    Probably then make the array variable be a member of your "form", then it will stay in scope.



  • @JonB i have no idea how to do it..

    can you put here an exampe please?


  • Lifetime Qt Champion

    Hi,

    Best to read about them here.



  • @SGaist
    Ok, now i've understand how to do it.
    Thank you very much



  • @SGaist said in problem with scope of array??:

    Hi,

    Best to read about them here.

    How come you give him one dense reference to read and it turns out to be just the right one to explain to his satisfaction?!


  • Lifetime Qt Champion

    @JonB ... it was the right reference ;)



  • Ok i read the link documentation but i can't do this work..

    i think that i have to create a class with QLabelEdit array

    so i can manage data in or out the main with no scope problem.

    It is correct?

    If yes..

    i've view some tutorial in order to learn c++ ( i'm coming from VB6)
    but this is too complicate for me..

    anyone can put a example?


  • Qt Champions 2019

    @TheCipo76 Simply put these arrays as member variables of your form class, there is no need for an extra class just for these arrays.

    class regtemciclo: public...
    {
    ...
    private:
        QLineEdit *array_Fine15[5];
    ...
    }
    


  • @jsulm Eureka! it works..

    thank you very much!


Log in to reply