vendredi 20 septembre 2019

to extend the lifetime of a QT object outside of the constructor

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

Aucun commentaire:

Enregistrer un commentaire