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. Newbie Question, Working with multiple CPP files and Headers
Forum Updated to NodeBB v4.3 + New Features

Newbie Question, Working with multiple CPP files and Headers

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 2 Posters 2.2k 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.
  • S Offline
    S Offline
    Stevendragoes
    wrote on 10 Feb 2020, 07:09 last edited by
    #1

    Hello, I am trying to make my code a little bit more modular and reusable by having multiple CPP files and Headers in my QT project.
    Are there any simple example for me to get started with? explaining like how can I make the cpp files to communicate or access it's data members?

    Maybe Parent and Child relationships in QT?

    J 1 Reply Last reply 10 Feb 2020, 07:12
    0
    • S Stevendragoes
      10 Feb 2020, 08:47

      @jsulm said in Newbie Question, Working with multiple CPP files and Headers:

      just connect the clicked() signals from the buttons to the slots in conlinecam.

      May I know exactly how? Because I have created the slots. The Signals, Comes from the ToolButtons I have created in my UI form. Do you mean via the Signal/Slot Editor in the UI?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 Feb 2020, 08:49 last edited by
      #16

      @Stevendragoes

      connect(ui->some_tool_button, SIGNAL(clicked()), conlinecamInstance, SLOT(start()));
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • S Stevendragoes
        10 Feb 2020, 07:09

        Hello, I am trying to make my code a little bit more modular and reusable by having multiple CPP files and Headers in my QT project.
        Are there any simple example for me to get started with? explaining like how can I make the cpp files to communicate or access it's data members?

        Maybe Parent and Child relationships in QT?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 10 Feb 2020, 07:12 last edited by
        #2

        @Stevendragoes said in Newbie Question, Working with multiple CPP files and Headers:

        Maybe Parent and Child relationships in QT?

        No, signals/slots: https://doc.qt.io/qt-5/signalsandslots.html And of cource what C++ provides.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • S Offline
          S Offline
          Stevendragoes
          wrote on 10 Feb 2020, 07:15 last edited by
          #3

          @jsulm Do you have a simple example how I can connect two objects together or pass in data members such as QString or QNumber between those two objects?

          J 1 Reply Last reply 10 Feb 2020, 07:16
          0
          • S Stevendragoes
            10 Feb 2020, 07:15

            @jsulm Do you have a simple example how I can connect two objects together or pass in data members such as QString or QNumber between those two objects?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 10 Feb 2020, 07:16 last edited by
            #4

            @Stevendragoes Please check the link I provided - there are simple examples.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Stevendragoes
              wrote on 10 Feb 2020, 07:56 last edited by
              #5

              Hello @jsulm As you remember I did a illegal method of Passing in parent into the child to access and play. you have recommended me to have initialise the Timer in another class. However, Now I want to bind the Play, Pause and Stop button. would it be possible because I initialised my timer in another class?

              J 1 Reply Last reply 10 Feb 2020, 08:09
              0
              • S Stevendragoes
                10 Feb 2020, 07:56

                Hello @jsulm As you remember I did a illegal method of Passing in parent into the child to access and play. you have recommended me to have initialise the Timer in another class. However, Now I want to bind the Play, Pause and Stop button. would it be possible because I initialised my timer in another class?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 10 Feb 2020, 08:09 last edited by
                #6

                @Stevendragoes said in Newbie Question, Working with multiple CPP files and Headers:

                Now I want to bind the Play, Pause and Stop button. would it be possible because I initialised my timer in another class?

                Yes, in your main window add play(), pause() and stop() signals. When you create the child class instance connect these signals to slots in that child class.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Stevendragoes
                  wrote on 10 Feb 2020, 08:12 last edited by
                  #7
                  conlinecam* cam = new conlinecam();
                  connect(cam, SIGNAL(update()), this, SLOT(update_screen()));
                  

                  I have this.

                  update() is from another cpp file which initialise the Timer and sends the update signal to my update_screen slot.
                  So, I have already created QToolButton "Stop" what can I do to Stop the Frames? Is there a specific "Disconnect a Slot" function?

                  J 1 Reply Last reply 10 Feb 2020, 08:15
                  0
                  • S Stevendragoes
                    10 Feb 2020, 08:12
                    conlinecam* cam = new conlinecam();
                    connect(cam, SIGNAL(update()), this, SLOT(update_screen()));
                    

                    I have this.

                    update() is from another cpp file which initialise the Timer and sends the update signal to my update_screen slot.
                    So, I have already created QToolButton "Stop" what can I do to Stop the Frames? Is there a specific "Disconnect a Slot" function?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 10 Feb 2020, 08:15 last edited by
                    #8

                    @Stevendragoes said in Newbie Question, Working with multiple CPP files and Headers:

                    Is there a specific "Disconnect a Slot" function?

                    Yes, but there is no need for that in your case. Just stop the timer.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Stevendragoes
                      wrote on 10 Feb 2020, 08:20 last edited by Stevendragoes 2 Oct 2020, 08:26
                      #9

                      Okay I think I get it. I need to initialise

                      conlinecam* cam = new conlinecam();
                      

                      in my Main Code when I initialise the UI.

                      And I have to make my timer public already because I am initialising the timer when I press a Button to get the WebCam Feed

                      EDIT: I get then error about __inline because I initialise in the header file

                      J 1 Reply Last reply 10 Feb 2020, 08:27
                      0
                      • S Offline
                        S Offline
                        Stevendragoes
                        wrote on 10 Feb 2020, 08:21 last edited by
                        #10

                        Then I have a question. why would they say _inline?
                        Because I tried to initialise the Timer QObject in the Header FIle. But It simply doesn't allow me @jsulm

                        1 Reply Last reply
                        0
                        • S Stevendragoes
                          10 Feb 2020, 08:20

                          Okay I think I get it. I need to initialise

                          conlinecam* cam = new conlinecam();
                          

                          in my Main Code when I initialise the UI.

                          And I have to make my timer public already because I am initialising the timer when I press a Button to get the WebCam Feed

                          EDIT: I get then error about __inline because I initialise in the header file

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 10 Feb 2020, 08:27 last edited by
                          #11

                          @Stevendragoes said in Newbie Question, Working with multiple CPP files and Headers:

                          And I have to make my timer public

                          NO!
                          Why do you think so?
                          The timer is private implementation detail in conlinecam, the outside world does not have to know it even exists.
                          Initialise the timer inside conlinecam.

                          "why would they say _inline?" who?
                          "Because I tried to initialise the Timer QObject in the Header FIle. But It simply doesn't allow me" - please show your code.
                          It is actually as simple as

                          class conlinecam
                          {
                          private:
                              QTimer timer;
                          }
                          

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • S Offline
                            S Offline
                            Stevendragoes
                            wrote on 10 Feb 2020, 08:32 last edited by
                            #12

                            To simplify the situation, @jsulm I have the following files
                            483bb332-8bca-404a-b603-7d0aa9737752-image.png

                            conlinecam.h has the following code

                            #ifndef CONLINECAM_H
                            #define CONLINECAM_H
                            
                            #include <mainwindow.h>
                            #include <QMainWindow>
                            
                            class conlinecam : public QObject
                            {
                                Q_OBJECT
                            public:
                                conlinecam();
                                ~conlinecam();
                            private:
                                QTimer* camtimer;
                                
                            signals:
                                void update();
                            };
                            
                            #endif // CONLINECAM_H
                            
                            

                            conlinecam.cpp has the following code

                            #include "conlinecam.h"
                            #include "ui_mainwindow.h"
                            #include "mainwindow.h"
                            
                            conlinecam::conlinecam() : QObject()
                            {
                                 camtimer = new QTimer(this);
                                 connect(camtimer,SIGNAL(timeout()),this,SIGNAL(update()));
                                 camtimer->start(100);
                            }
                            
                            conlinecam::~conlinecam()
                            {
                                camtimer->stop();
                            }
                            
                            

                            in my mainwindow.cpp , I have the following code to connect to the conlinecam

                            void MainWindow::on_online_clicked()
                            {
                                conlinecam* cam = new conlinecam();
                                connect(cam, SIGNAL(update()), this, SLOT(update_screen()));
                                onlinecam.open(0);                                 //------------------------CAMERA INPUT---------------------------------//
                                int framewidth = onlinecam.get(cv::CAP_PROP_FRAME_WIDTH);
                                int frameheight = onlinecam.get(cv::CAP_PROP_FRAME_HEIGHT);
                                QString fwidth= QString::number(framewidth);
                                QString fheight = QString::number(frameheight);
                                ui->textEdit->append(fwidth);
                                ui->textEdit->append(fheight);
                            }
                            

                            Now I just want to include Pause and STOP button to pause and stop the timer. But can't because of the following.

                            1. Timer is private in conlinecam.cpp
                            2. conlinecam is only initialised when I pressed the Online PushButton.
                              Hence, I cannot access it's data members
                            J 2 Replies Last reply 10 Feb 2020, 08:36
                            0
                            • S Stevendragoes
                              10 Feb 2020, 08:32

                              To simplify the situation, @jsulm I have the following files
                              483bb332-8bca-404a-b603-7d0aa9737752-image.png

                              conlinecam.h has the following code

                              #ifndef CONLINECAM_H
                              #define CONLINECAM_H
                              
                              #include <mainwindow.h>
                              #include <QMainWindow>
                              
                              class conlinecam : public QObject
                              {
                                  Q_OBJECT
                              public:
                                  conlinecam();
                                  ~conlinecam();
                              private:
                                  QTimer* camtimer;
                                  
                              signals:
                                  void update();
                              };
                              
                              #endif // CONLINECAM_H
                              
                              

                              conlinecam.cpp has the following code

                              #include "conlinecam.h"
                              #include "ui_mainwindow.h"
                              #include "mainwindow.h"
                              
                              conlinecam::conlinecam() : QObject()
                              {
                                   camtimer = new QTimer(this);
                                   connect(camtimer,SIGNAL(timeout()),this,SIGNAL(update()));
                                   camtimer->start(100);
                              }
                              
                              conlinecam::~conlinecam()
                              {
                                  camtimer->stop();
                              }
                              
                              

                              in my mainwindow.cpp , I have the following code to connect to the conlinecam

                              void MainWindow::on_online_clicked()
                              {
                                  conlinecam* cam = new conlinecam();
                                  connect(cam, SIGNAL(update()), this, SLOT(update_screen()));
                                  onlinecam.open(0);                                 //------------------------CAMERA INPUT---------------------------------//
                                  int framewidth = onlinecam.get(cv::CAP_PROP_FRAME_WIDTH);
                                  int frameheight = onlinecam.get(cv::CAP_PROP_FRAME_HEIGHT);
                                  QString fwidth= QString::number(framewidth);
                                  QString fheight = QString::number(frameheight);
                                  ui->textEdit->append(fwidth);
                                  ui->textEdit->append(fheight);
                              }
                              

                              Now I just want to include Pause and STOP button to pause and stop the timer. But can't because of the following.

                              1. Timer is private in conlinecam.cpp
                              2. conlinecam is only initialised when I pressed the Online PushButton.
                                Hence, I cannot access it's data members
                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 10 Feb 2020, 08:36 last edited by
                              #13

                              @Stevendragoes

                              1. As I said add slots to conlinecam where you have full access to the timer
                              2. You don't have to access its members - this is whole point of encapsulation.
                              class conlinecam
                              {
                              public slots:
                                  void start();
                                  void stop();
                                  void pause();
                              private:
                                  QTimer timer;
                              }
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • S Stevendragoes
                                10 Feb 2020, 08:32

                                To simplify the situation, @jsulm I have the following files
                                483bb332-8bca-404a-b603-7d0aa9737752-image.png

                                conlinecam.h has the following code

                                #ifndef CONLINECAM_H
                                #define CONLINECAM_H
                                
                                #include <mainwindow.h>
                                #include <QMainWindow>
                                
                                class conlinecam : public QObject
                                {
                                    Q_OBJECT
                                public:
                                    conlinecam();
                                    ~conlinecam();
                                private:
                                    QTimer* camtimer;
                                    
                                signals:
                                    void update();
                                };
                                
                                #endif // CONLINECAM_H
                                
                                

                                conlinecam.cpp has the following code

                                #include "conlinecam.h"
                                #include "ui_mainwindow.h"
                                #include "mainwindow.h"
                                
                                conlinecam::conlinecam() : QObject()
                                {
                                     camtimer = new QTimer(this);
                                     connect(camtimer,SIGNAL(timeout()),this,SIGNAL(update()));
                                     camtimer->start(100);
                                }
                                
                                conlinecam::~conlinecam()
                                {
                                    camtimer->stop();
                                }
                                
                                

                                in my mainwindow.cpp , I have the following code to connect to the conlinecam

                                void MainWindow::on_online_clicked()
                                {
                                    conlinecam* cam = new conlinecam();
                                    connect(cam, SIGNAL(update()), this, SLOT(update_screen()));
                                    onlinecam.open(0);                                 //------------------------CAMERA INPUT---------------------------------//
                                    int framewidth = onlinecam.get(cv::CAP_PROP_FRAME_WIDTH);
                                    int frameheight = onlinecam.get(cv::CAP_PROP_FRAME_HEIGHT);
                                    QString fwidth= QString::number(framewidth);
                                    QString fheight = QString::number(frameheight);
                                    ui->textEdit->append(fwidth);
                                    ui->textEdit->append(fheight);
                                }
                                

                                Now I just want to include Pause and STOP button to pause and stop the timer. But can't because of the following.

                                1. Timer is private in conlinecam.cpp
                                2. conlinecam is only initialised when I pressed the Online PushButton.
                                  Hence, I cannot access it's data members
                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 10 Feb 2020, 08:42 last edited by
                                #14

                                @Stevendragoes One correction to one of my previous posts: "Yes, in your main window add play(), pause() and stop() signals. When you create the child class instance connect these signals to slots in that child class." - there is no need for to define these signals in main window, just connect the clicked() signals from the buttons to the slots in conlinecam.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                S 1 Reply Last reply 10 Feb 2020, 08:47
                                0
                                • J jsulm
                                  10 Feb 2020, 08:42

                                  @Stevendragoes One correction to one of my previous posts: "Yes, in your main window add play(), pause() and stop() signals. When you create the child class instance connect these signals to slots in that child class." - there is no need for to define these signals in main window, just connect the clicked() signals from the buttons to the slots in conlinecam.

                                  S Offline
                                  S Offline
                                  Stevendragoes
                                  wrote on 10 Feb 2020, 08:47 last edited by
                                  #15

                                  @jsulm said in Newbie Question, Working with multiple CPP files and Headers:

                                  just connect the clicked() signals from the buttons to the slots in conlinecam.

                                  May I know exactly how? Because I have created the slots. The Signals, Comes from the ToolButtons I have created in my UI form. Do you mean via the Signal/Slot Editor in the UI?

                                  J 1 Reply Last reply 10 Feb 2020, 08:49
                                  0
                                  • S Stevendragoes
                                    10 Feb 2020, 08:47

                                    @jsulm said in Newbie Question, Working with multiple CPP files and Headers:

                                    just connect the clicked() signals from the buttons to the slots in conlinecam.

                                    May I know exactly how? Because I have created the slots. The Signals, Comes from the ToolButtons I have created in my UI form. Do you mean via the Signal/Slot Editor in the UI?

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 10 Feb 2020, 08:49 last edited by
                                    #16

                                    @Stevendragoes

                                    connect(ui->some_tool_button, SIGNAL(clicked()), conlinecamInstance, SLOT(start()));
                                    

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    3
                                    • S Offline
                                      S Offline
                                      Stevendragoes
                                      wrote on 10 Feb 2020, 08:51 last edited by
                                      #17

                                      @jsulm
                                      Wow. you are amazing. Or maybe I am too inexperienced 4 months into coding..

                                      J 1 Reply Last reply 10 Feb 2020, 08:51
                                      1
                                      • S Stevendragoes
                                        10 Feb 2020, 08:51

                                        @jsulm
                                        Wow. you are amazing. Or maybe I am too inexperienced 4 months into coding..

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 10 Feb 2020, 08:51 last edited by
                                        #18

                                        @Stevendragoes No problem. It takes some time to learn new concepts. Keep learning :-)

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        2
                                        • S Offline
                                          S Offline
                                          Stevendragoes
                                          wrote on 10 Feb 2020, 08:54 last edited by
                                          #19

                                          @jsulm Just a question Do you experienced guys have like Discord Chat Groups with the moderators and professionals?

                                          J 1 Reply Last reply 11 Feb 2020, 07:24
                                          0
                                          • S Stevendragoes
                                            10 Feb 2020, 08:54

                                            @jsulm Just a question Do you experienced guys have like Discord Chat Groups with the moderators and professionals?

                                            J Offline
                                            J Offline
                                            jsulm
                                            Lifetime Qt Champion
                                            wrote on 11 Feb 2020, 07:24 last edited by
                                            #20

                                            @Stevendragoes You can chat here.
                                            For professional support from Qt Company you will need a commercial license.

                                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                                            1 Reply Last reply
                                            0

                                            1/20

                                            10 Feb 2020, 07:09

                                            • Login

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