Newbie Question, Working with multiple CPP files and Headers
-
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?
-
connect(ui->some_tool_button, SIGNAL(clicked()), conlinecamInstance, SLOT(start()));
-
@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.
-
@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?
-
@Stevendragoes Please check the link I provided - there are simple examples.
-
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?
-
@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.
-
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? -
@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.
-
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
-
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 -
@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 asclass conlinecam { private: QTimer timer; }
-
To simplify the situation, @jsulm I have the following files
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.
- Timer is private in conlinecam.cpp
- conlinecam is only initialised when I pressed the Online PushButton.
Hence, I cannot access it's data members
-
- As I said add slots to conlinecam where you have full access to the timer
- 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; }
-
@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.
-
@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?
-
connect(ui->some_tool_button, SIGNAL(clicked()), conlinecamInstance, SLOT(start()));
-
@jsulm
Wow. you are amazing. Or maybe I am too inexperienced 4 months into coding.. -
@Stevendragoes No problem. It takes some time to learn new concepts. Keep learning :-)
-
@jsulm Just a question Do you experienced guys have like Discord Chat Groups with the moderators and professionals?
-
@Stevendragoes You can chat here.
For professional support from Qt Company you will need a commercial license.