0
votes

this is the problem given to me

  1. create a project called datamanager and its base class should be QWidget
  2. add a new class called controller inherited from QObject
  3. and 2 slots called sensordatarecived and startdatacollection in controller
  4. add another class called commonreader class inherited from QObject
  5. define 2 signals called readingStarted() and readCompleted() in commonreader class
  6. add a slot called sendData()
  7. declare a virtual function called monitor() in the commonreader class
  8. add 5 new sensor classes which inherit from the commonreader class
  9. in all of the above classes reimplement the common Monitor() function
  10. using QTimer object implement emit readingStarted() from the monitor() function of each of the 5 classes defined
  11. implement the sendData() slot
  12. emit signal called readcompleted inside the send dataslot()
  13. create the object of each of the above sensor classes in the constructor of the controller
  14. call monitor() function of the method sensor objectfrom startDataCollection()
  15. connect readComplete() signal of each object to sensordatarecieved() of the controller.

these are the steps i have to follow for a project.i am stuck in the 14 th step and i need help.

//controller.h
class controler : public QObject
{
    Q_OBJECT
public:
    explicit controler(QObject *parent = nullptr);

signals:

public slots:
    void sensorDataRecived();
    void startDataCollection();

};

//controller.cpp

#include "controler.h"
#include <QDebug>
#include "heart_1_sensor.h"
#include "eye_2_sensor.h"
#include "brain_3_sensor.h"
#include "ear_5_sensor.h"
#include "head_4_sensor.h"
#include "commonreaderclass.h"

controler::controler(QObject *parent) : QObject(parent)
{
    commonReaderClass *h1=new heart_1_Sensor;
    commonReaderClass *e2=new eye_2_Sensor;
    commonReaderClass *b3=new brain_3_sensor;
    commonReaderClass *e5=new ear_5_sensor;
    commonReaderClass *h4=new head_4_sensor;



}

void controler::sensorDataRecived()
{
    qDebug()<<Q_FUNC_INFO<<endl;

}

void controler::startDataCollection()
{
}
//commonreaderclass.h

#ifndef COMMONREADERCLASS_H
#define COMMONREADERCLASS_H

#include <QObject>

class commonReaderClass : public QObject
{
    Q_OBJECT
public:
    explicit commonReaderClass(QObject *parent = nullptr);
    virtual void monitor();

signals:
    void readingStarted();
    void readCompleted();

public slots:
    void sendData();
};

#endif // COMMONREADERCLASS_H

//commonreaderclass.cpp
#include "commonreaderclass.h"
#include <QDebug>
#include <QTimer>

commonReaderClass::commonReaderClass(QObject *parent) : QObject(parent)
{

}

void commonReaderClass::sendData()
{
    qDebug()<<"sending data has started"<<endl;
    emit readCompleted();

}
//sensor1.h
#ifndef HEART_1_SENSOR_H
#define HEART_1_SENSOR_H
#include "commonreaderclass.h"


class heart_1_Sensor:public commonReaderClass
{
public:
    heart_1_Sensor();
    virtual void monitor();
};

#endif // HEART_1_SENSOR_H
//sensor 1.cpp
#include "heart_1_sensor.h"
#include <QDebug>
#include <QTimer>

heart_1_Sensor::heart_1_Sensor()
{

}

void heart_1_Sensor::monitor()
{
    qDebug()<<"monitoring the heart"<<endl;
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
        timer->start(2000);
        emit readingStarted();
}

//and another 4 sensors of the same implementation
1
It looks like you need to read about member variables in your favourite C++ book.molbdnilo

1 Answers

0
votes

I agree with @molbdnilo that you should make h1, e2, ... members of the class, instead of local variables in the constructor. But in this case, there is one more consideration: the lifetime of QObject instances is special, because the parent/children relationship between them so the children can be automatically destroyed when the parent is destroyed. I recommend you this book (paper printed versions also available). Specially chapter 2 about classes and chapter 8 about QObject and other important Qt classes. This book is a curriculum, you should follow it from start to end, and also read other books.

controller.h

class Controller : public QObject
{
    Q_OBJECT
public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller(); // the destructor
    // ... more public members
signals:
    // ... 
public slots:
    // ... 
private:
    commonReaderClass *m_h1;
    commonReaderClass *m_e2;
    // ...
};

I've renamed variables h1 to m_h1 and e2 to m_e2, following a common convention for member variable names, and the Controller class name starting with uppercase is another common naming convention.

controller.cpp (the classic C++ way)

Controller::Controller(QObject *parent) : QObject(parent)
{
    m_h1 = new heart_1_Sensor;
    m_e2 = new eye_2_Sensor;
    // ...
}

Controller::~Controller()
{
    delete m_h1;
    delete m_e2;
    // ...
}

controller.cpp (the Qt way)

Controller::Controller(QObject *parent) : QObject(parent)
{
    m_h1 = new heart_1_Sensor(this);
    m_e2 = new eye_2_Sensor(this);
    // ...
}

Controller::~Controller()
{ }

The second version of controller.cpp is generally preferred when writing Qt based programs. You should remember that in C++ every pointer initialized with a new operation should have a corresponding delete operation. There is not automatic "garbage collection" in C++, but QObject provides a fairly comfortable mechanism to automatically delete children objects, so the destructor in this second version may be empty or you can omit entirely.