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. Calling an object from another class
Forum Updated to NodeBB v4.3 + New Features

Calling an object from another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcamera
13 Posts 4 Posters 7.8k 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.
  • the_T Offline
    the_T Offline
    the_
    wrote on last edited by
    #2

    Hi,

    m_camera is declared in the Camera class. So if you want to access a method or variable from that class you need to create an instance of Camera Class.

    Also you have to declare it public. Private variables and methods can only be accessed by methods inside the class itself.

    -- No support in PM --

    1 Reply Last reply
    0
    • CharbyC Offline
      CharbyC Offline
      Charby
      wrote on last edited by
      #3

      @Suji said: Your problem is that you don't have such m_camera pointer as a member of your Parameters class.
      What you would need is :

      • add a pointer to the QCamera as a private member of Parameters
      • add a getter in Camera to return the QCamera pointer
      • add a setter in Parameters (a method for initialising you QCamera pointer using your Camera getter

      Keep in mind, that this solution is the following up of your current design (where you have a QCamera pointer as member of Camera) but using pointers will force you to take care of the QCamera ownership.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Suji
        wrote on last edited by Suji
        #4

        Hi guys,

        Thank you for your so fast answer !

        I tried to understand your solutions but, I'm sorry I'm novice and I really dont understand what I have to do and moreover where I have to put it : .cpp ? .h ?
        Can you explain me more precisely please... my level is really low, I'm learning basis, vocabulary etc.

        Thanks for your help and your patience

        CharbyC 1 Reply Last reply
        0
        • the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #5

          @Suji
          To explain what @Charby mentioned:

          • provide a getter method in the Camera class:
          //in camera.h
          class Camera {
          //...
          public:
          QCamera* get();
          //...
          }
          
          //in camera.cpp
          QCamera *Camera::get() {
            return m_camera;
          }
          
          • in parameters.cpp
          #include "camera.h"
          //...
          Camera *cam = new Camera;
          QCamera *m_camera = cam->get();
          

          You still have to initialize the pointer in camera.cpp.

          (@Charby correct me if I misinterpreded your post :))

          -- No support in PM --

          1 Reply Last reply
          2
          • S Suji

            Hi guys,

            Thank you for your so fast answer !

            I tried to understand your solutions but, I'm sorry I'm novice and I really dont understand what I have to do and moreover where I have to put it : .cpp ? .h ?
            Can you explain me more precisely please... my level is really low, I'm learning basis, vocabulary etc.

            Thanks for your help and your patience

            CharbyC Offline
            CharbyC Offline
            Charby
            wrote on last edited by
            #6

            It is hard to answer without knowing how your Camera and Parameters are created and where the QCamera member of Camera is initialized. This is one way :

            Modify you Camera class adding a getter

            class Camera : public QMainWindow
            {
            
                public:
                Camera();
                ~Camera();
                QCamera* getCamera(){ return m_camera;}
                public slots:
            
                private:
                QCamera *m_camera;
            }
            

            Modify your Parameters class so that it can be constructed using a QCamera*

            class Parameters : public QMainWindow
            {
            public:
                Parameters(QCamera* cam){
                    imageProcessing = cam->m_camera->imageProcessing();
                }
                ~Parameters();
            
            private:
                QCameraImageProcessing *imageProcessing;
            };
            

            and here I consider that your Camera and Parameters instances can be created on the stack and that the QCamera is initialized in the Camera constructor :

            Camera* cam = new Camera(); 
            Parameters* param = new Parameters( cam->getCamera() );
            ...
            delete param,
            delete cam;
            
            1 Reply Last reply
            2
            • S Offline
              S Offline
              Suji
              wrote on last edited by
              #7

              Thanks again @Charby and @the_ .

              Why it is so complicated ? :D

              I had the "camera.h" in my Parameters.cpp, so why he just dont finds "m_camera" in Parameters.cpp ?

              //Parameters.cpp
              imageProcessing = m_camera->imageProcessing();
              

              like he finds it in Camera.cpp when I initialize a new QCamera ?

              QCamera *m_camera
              

              Moreover, compiler dont finds "m_camera" here :

              class Parameters : public QMainWindow
              {
              public:
                  Parameters(QCamera* cam){
                      imageProcessing = cam->m_camera->imageProcessing();
                  }
                  ~Parameters();
              
              private:
                  QCameraImageProcessing *imageProcessing;
              };
              

              even if i followed all your steps guys...

              I know it is hard for you to help me, but my code is not in English, then if i post it all, it will not help you, in my opinion.
              Maybe it can ?

              I don't like to annoy you with my so bad level, so i think I will try to find alone... Your answer are clear, but I don't understand why it is so complicated to do something simple ! :D

              the_T 1 Reply Last reply
              0
              • S Suji

                Thanks again @Charby and @the_ .

                Why it is so complicated ? :D

                I had the "camera.h" in my Parameters.cpp, so why he just dont finds "m_camera" in Parameters.cpp ?

                //Parameters.cpp
                imageProcessing = m_camera->imageProcessing();
                

                like he finds it in Camera.cpp when I initialize a new QCamera ?

                QCamera *m_camera
                

                Moreover, compiler dont finds "m_camera" here :

                class Parameters : public QMainWindow
                {
                public:
                    Parameters(QCamera* cam){
                        imageProcessing = cam->m_camera->imageProcessing();
                    }
                    ~Parameters();
                
                private:
                    QCameraImageProcessing *imageProcessing;
                };
                

                even if i followed all your steps guys...

                I know it is hard for you to help me, but my code is not in English, then if i post it all, it will not help you, in my opinion.
                Maybe it can ?

                I don't like to annoy you with my so bad level, so i think I will try to find alone... Your answer are clear, but I don't understand why it is so complicated to do something simple ! :D

                the_T Offline
                the_T Offline
                the_
                wrote on last edited by
                #8

                I dont know your programming skills, but maybe you should start with a quick reading about Classes in C++ :) Try to understand how to access public, private and protected methods and variables.
                I hope this link will help a little bit
                C++ Classes

                -- No support in PM --

                1 Reply Last reply
                2
                • S Offline
                  S Offline
                  Suji
                  wrote on last edited by
                  #9

                  Hi @the_

                  Yes, you're right, my problem is I learnt alone, so I have a big vocabulary problem, I know how to do things, but not how they're called, I understand the logic, but not the syntax I have to use. :) So thank you for this link, I like this summary !

                  Actually, my code is able to launch a windows, to obtain the availables cameras in a checklist, when you ckeck one, it is selected and you can open its stream. But now, I created a new windows called "parameters", and i want to pilot my camera's settings from this new window "parameters".

                  I will work with your solutions guys, thank you.

                  Have a nice day !

                  G 1 Reply Last reply
                  0
                  • S Suji

                    Hi @the_

                    Yes, you're right, my problem is I learnt alone, so I have a big vocabulary problem, I know how to do things, but not how they're called, I understand the logic, but not the syntax I have to use. :) So thank you for this link, I like this summary !

                    Actually, my code is able to launch a windows, to obtain the availables cameras in a checklist, when you ckeck one, it is selected and you can open its stream. But now, I created a new windows called "parameters", and i want to pilot my camera's settings from this new window "parameters".

                    I will work with your solutions guys, thank you.

                    Have a nice day !

                    G Offline
                    G Offline
                    gyll
                    wrote on last edited by gyll
                    #10

                    @Suji
                    You don't have a vocabulary problem, you have a problem with understanding the very basics of object-oriented programming and C++ classes.
                    C++ is a very complex programming language, you can't learn it just by examples. Go to the source: http://mazonka.com/shared/Straustrup4th.pdf
                    If you come from the world of C (i.e. if you already know C and have programmed in C) then you can go directly to "Part III " in the book, "Abstraction Mechanisms"

                    S 1 Reply Last reply
                    1
                    • G gyll

                      @Suji
                      You don't have a vocabulary problem, you have a problem with understanding the very basics of object-oriented programming and C++ classes.
                      C++ is a very complex programming language, you can't learn it just by examples. Go to the source: http://mazonka.com/shared/Straustrup4th.pdf
                      If you come from the world of C (i.e. if you already know C and have programmed in C) then you can go directly to "Part III " in the book, "Abstraction Mechanisms"

                      S Offline
                      S Offline
                      Suji
                      wrote on last edited by
                      #11

                      Hi @gyll,

                      You're right, the problem is I started programming for 2 months. I knew the if, for loop etc from Matlab, but didn't know the "real" programming 2 months ago. I did C++ open classroom courses and bought a book about C.

                      And as you said :
                      "you can't learn it just by examples."

                      I'm actually learning from examples from here and there but it's a bad way for understanding the basis.

                      Thanks for this source, I will study this.

                      Have a nice day !

                      G 1 Reply Last reply
                      0
                      • S Suji

                        Hi @gyll,

                        You're right, the problem is I started programming for 2 months. I knew the if, for loop etc from Matlab, but didn't know the "real" programming 2 months ago. I did C++ open classroom courses and bought a book about C.

                        And as you said :
                        "you can't learn it just by examples."

                        I'm actually learning from examples from here and there but it's a bad way for understanding the basis.

                        Thanks for this source, I will study this.

                        Have a nice day !

                        G Offline
                        G Offline
                        gyll
                        wrote on last edited by
                        #12

                        @Suji
                        You're welcome.
                        So i understand that you also have no experience with C either. In this case, just read the whole book except "exceptions", "pointers to methods", and "templates" for now; that will give you a good understanding to start programming.

                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          Suji
                          wrote on last edited by
                          #13

                          @gyll

                          OK thank you for all these good advices !

                          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