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.3k 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 last edited by Stevendragoes
    #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

    jsulmJ 1 Reply Last reply
    0
    • S Offline
      S Offline
      Stevendragoes
      wrote on 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

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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 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
          jsulmJ 2 Replies Last reply
          0
          • S Stevendragoes

            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
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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

              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
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              0
              • jsulmJ jsulm

                @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 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?

                jsulmJ 1 Reply Last reply
                0
                • S Stevendragoes

                  @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?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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 last edited by
                    #17

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

                    jsulmJ 1 Reply Last reply
                    1
                    • S Stevendragoes

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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 last edited by
                        #19

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

                        jsulmJ 1 Reply Last reply
                        0
                        • S Stevendragoes

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 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

                          • Login

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