Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt Development beginer



  • Hi i am stuck in a problem as such:-

    there is a class called controller inherited from QObject
    another class called common reader which is also inherited from the QObject
    now we create 5 sensor classes which inherits from common reader class (this sensor class has a method called monitor() which are all virtual functions)
    my task is to create the objects of the 5 sensors in the controllers constructor and the controller has a method called datasensors in this datasensor method i have to call the monitor method of each of the 5 sensor objects, can you please help me to figure out how to extend the life time of an objects created in the constructor.```
    //your code here

    //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

  • Lifetime Qt Champion

    Hi and welcome to the forums

    Since you new the sensors, they have a long lifetime.
    Actually, in this case, they never deleted. (leaking)

    But you also have the issue that the sensor variables are local to the constructor
    so they are hard to reuse from other functions in the same class.

    so i would do the following.

    1: move sensors variables to class as memebers

    class controler : public QObject
    {
    ..
    private:
    commonReaderClass *h1;
    commonReaderClass *e2;
    .. all of them...
    

    and then make sure to change the constructor to only new them, not
    redeclare them.

    2:

    controler::controler(QObject *parent) : QObject(parent)
    {
        h1=new heart_1_Sensor(this); // note the this
        e2=new eye_2_Sensor(this);
        b3=new brain_3_sensor(this);
        e5=new ear_5_sensor(this);
        h4=new head_4_sensor(this);   
    }
    

    To make sure its autoclean up, we use the keyword this for the parent
    this means that the controler class now owns them and will delete them when itself is deleted.
    This auto clean up system is explained here
    https://doc.qt.io/qt-5/objecttrees.html

    Im not 100% sure what you ask help about so if this is not it, please give details about
    what you then mean by life time/ what the actual problem is.


Log in to reply