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. Passing a Vector of Classes to a dialog.
Forum Updated to NodeBB v4.3 + New Features

Passing a Vector of Classes to a dialog.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.1k Views 3 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.
  • Dan3460D Offline
    Dan3460D Offline
    Dan3460
    wrote on last edited by
    #1

    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;
    myMachines.push_back(mMchn);

    //From the main window when the menu was triggered:
    Dialog myDialog;
    myDialog.setMachineClass(&myMachines);


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

    Public:
    void setMachineClass(QVector<Machine> *mMachines)

    //from the implementation of the function
    void MachineParameters::setMachineClass(QVector<Machine> *mMachines)
    {
    myMachines=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.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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:

      myMachine[I].getMachineName()
      

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

      Instead, dereference the pointer first, like:

      (*myMachine)[I].getMachineName()
      

      Cheers.

      kshegunovK 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Dan3460D Offline
          Dan3460D Offline
          Dan3460
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            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:

            myMachine[I].getMachineName()
            

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

            Instead, dereference the pointer first, like:

            (*myMachine)[I].getMachineName()
            

            Cheers.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @Paul-Colby said:

            Instead, dereference the pointer first, like:

            (*myMachine)[I].getMachineName()

            Alternatively, the operator can be invoked explicitly:

            myMachine->operator[](i).getMachineName()
            

            @Dan3460

            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; 
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • Dan3460D Offline
              Dan3460D Offline
              Dan3460
              wrote on last edited by
              #6

              Thanks, it worked.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - 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