2017-01-17 120 views
0

我仍然卡住。Qt C++使用在创建对象中创建类的方法

我有一个GUI类创建一个对象。从这个新的对象的方法我想要使用创建类的方法

我有一个Gui(PBV)与line_Edits和Combobox。在这个Combobox中,我可以选择不同的几何形状。从Geometry继承的Geo_1,Geo_2 ...。根据组合框中的条目,我想创建不同的对象(Geo_1,Geo_2,...),然后根据Geo_n-Object的需要设置创建类的lineEdits。

我don`want这样做与信号插槽

我问的是不同的问题,但我不能老是进一步得到。

我以某种方式感觉这是一种递归...是否有解决方案堡帽?

这里是我的代码:

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() { } 

回答

3

当PBV分配Geo_1例如,它是已经传递指针本身的构造

GEO_1 = new Geo_1(this); // "this" is your PBV instance 

为什么不保存指针后使用?

Geo_1::Geo_1(PBV*_parent) : Geometry((QObject*)_parent) 
{ 
    this->pbv = _parent; // Save reference to parent 
} 

然后,你可以这样做:

void Geo_1::set_LNE_default() 
{ 
    // here I want to use printContent 
    this->pbv->printContent(...); // this->pbv is your saved ptr to the PBV that created this instance 
} 

编辑

Additonally,你可能会遇到,你将有问题的交叉包括两个头(PBV和Geo_1) ,要解决此问题,请从PBV.h和forward-declare Geo_1中删除Geo_1的包含。就像这样:

class Geo_1; 

在声明您的PBV类之前。

+0

这不起作用。当我在PBV.h中声明一个Geo:1 * GEO_1时,他向我显示了正确的颜色(紫色和红色),表明他知道Geo_1。这是确定的,因为我在PbV上执行了#include“../Geometry/Geo_1.h”。h但是我仍然得到错误:Geo_1没有命名类型,Geo_1 :: Geo_1(PBV *)的原型与类Geo_1中的任何类型都不匹配。为什么???????我有印象这与我的#包含有关 – user3443063