0

I have a question again: I have a class PBV t(inherits from Tab) hat has a class Geometry. Geometry is the parent of Geo_1. From Geo_1 I want to have access to methods of PBV ( e.g. printContent . How do I do that? I am able to create Signal-Slots but since I have to use methods of PBV often that would make lots of Signal-Slots. Here is my code:

PBV.h:

#include "../Geometry/Geo_1.h"

 class PBV : public Tab
 {
     Q_OBJECT    
 public:
     explicit PBV (QWidget *parent = 0);
     ~ PBV ();
     virtual void printContent( QStringList *const qsl);

private:    
    Geo_1 *GEO_1;
    Geometry *GEO;
 }

PBV.cpp:

 …
 Geo_1 *GEO_1;
 GEO_1 = new Geo_1(this);
 GEO_1->set_LNE_default();
 …

.

Geo_1.h:
#ifndef GEO_1_H
#define GEO_1_H 
#include "Geometry.h"
#include "../Tabs/PBV.h"
class Geo_1: public Geometry
{
   Q_OBJECT
public:
    Geo_1 (QObject *_parent = 0);
    virtual void set_LNE_default();
};  
#endif // GEO_1_H

.

Geo_1.cpp:
#include "Geometry.h"
#include <QDebug>
#include "Geo_1.h"
#include "../Tabs/PBV.h"

Geo_1::Geo_1(QObject*_parent)
: Geometry(_parent) {}

void Geo_1::set_LNE_default()
{
    // here I want to use printContent  
}

.

Geometry.h:

 #ifndef GEOMETRY_H
 #define GEOMETRY_H
 #include <QObject>

 class Geometry : public QObject
 {
     Q_OBJECT
 public:
     Geometry(QObject *_parent=0);
     ~Geometry();
     virtual void set_LNE_default();
 };
 #endif // GEOMETRY_H

.

Geometry.cpp:

 #include "Geometry.h"
 #include <QDebug>

 Geometry::Geometry(QObject *_parent)
     : QObject(_parent)     {}

 Geometry::~Geometry(){}
 void Geometry::set_LNE_default() { }
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • `dynamic_cast`/`static_cast`? – LogicStuff Jan 16 '17 at 11:54
  • During the constructor of Geo_1 you have a reference to the parent through a pointer. That parent is the PBV class since you are passing the "this" pointer(its an assumption since i saw that those lines were inside the PBV.cpp file). So if you are going to use a dynamic_cast on that pointer like this dynamic_cast(_parent); you will be able to access the printContent of the PBV class. – Stamatis Liatsos Jan 16 '17 at 11:59
  • sorry, I am not used to dynamic/static cast. What do you mean? – user3443063 Jan 16 '17 at 12:00
  • You need to have a `PBV` instance to call `printContent` on. You have the option of storing a pointer to a `PBV` object in the `Geo_1` object (maybe its parent `QObject` and just call `printContent` on that instance. – Mike Jan 16 '17 at 12:00
  • If you would like to have `Geo_1` and `PBV` implementations decoupled. You can emit a signal from `Geo_1::set_LNE_default()` (since `Geo_1` is a `QObject`) and have that signal connected to a slot that calls `printContent()` (or maybe declare `printContent` as a slot). – Mike Jan 16 '17 at 12:02
  • Hi How would I code that? What is dynamic cast/ static cast ? (I don`t want to use Signal-Slot. ) – user3443063 Jan 16 '17 at 12:55

1 Answers1

0

One approach, as referred to by comments, is to keep track of the parent class in the constructor of the child:

In Geometry.h, add a private member variable:

private: 
  PBV* m_pParentPBV;

Then, in the constructor in Geometry.cpp, set this to the parent you are already passing:

Geometry::Geometry(QObject *_parent)
     : QObject(_parent)     
{
    m_pParentPBV = dynamic_cast<PBV*>(_parent);
}

Now you can call methods on the parent using m_pParentPBV->printContent() etc.

Benp44
  • 1,548
  • 1
  • 10
  • 14