Important: Please read the Qt Code of Conduct -

Passing a Vector of Classes to a dialog.

  • First a disclaimer, I program in C++ once every year for a specific project, so I'm a old fart novice.
    From the menu of a main window I'm calling a dialog that should fill out parameters for series of similar objects, in my case a different machines types. In the main window I have created a Qvector for this machines "QVector<Machines> myMachines". The class Machines have a series of functions to set different parameters. I used vectors extensible with Visual C++ and I think I was able to do what I needed but don't remember as it was a long ago.
    I'm trying to pass the vector to a Dialog, so the user can modify parameters of the different machines. Could not add the vector to the default constructor of the dialog as it was giving me an error, maybe someone here has an idea. By playing with just classes I added a "SetClass" function to the Dialog to pass the address as in this:

    //In the main Window header:
    QVector<Machine> myMachines;

    //From the initialization of the main window:
    Machine mMchn;

    //From the main window when the menu was triggered:
    Dialog myDialog;

    //In the dialog header
    QVector<Machine> *myMachines;

    void setMachineClass(QVector<Machine> *mMachines)

    //from the implementation of the function
    void MachineParameters::setMachineClass(QVector<Machine> *mMachines)

    I get no errors up to here compiling and displaying the main window and dialog. I can get the size of the vector as in "myMachines.size()" but I cannot access individual items as in "myMachine[I].getMachineName()".
    I tried to compile thinking that maybe the editor did not see the machines functions but I got the error that getMachineName() is not a function of myMachine.

    Thanks and sorry for the long post.

  • Hi @Dan3460,

    I'm not sure I understand completely what you are trying to do, but one quick thing: Given that MachineParameters::myMachines is a pointer, then when you do:


    the square brackets are trying to perform a C-style array pointer manipulation, not invoking QVector::operator[].

    Instead, dereference the pointer first, like:



  • Lifetime Qt Champion


    To add to @paul-colby, do you really need to pass that QVector around ?

    It seems that your Dialog should only need one machine object, no ?

  • Thanks for the answers, I'll try your solution Paul this evening.
    @SGaist the program can have many machines, from this new dialog i'm planning to add, modify and delete machines. I'm trying to pass a reference to the vector of objects to the dialog. The dialog has a dropdown list where you can select the machine you want to modify.

  • Moderators

    @Paul-Colby said:

    Instead, dereference the pointer first, like:


    Alternatively, the operator can be invoked explicitly:



    I'm trying to pass a reference to the vector of objects to the dialog.

    Then pass a reference. :)

    QVector<Machine> machines;
    // This is similar to what you do with pointers, but it guarantees there's a valid object behind the reference
    QVector<Machine> & machinesRef = machines; 

  • Thanks, it worked.

  • Lifetime Qt Champion

    Out of curiosity, how much are you going to pass that vector around in your application ?

Log in to reply