2017-01-17 3 views
0

私はまだ詰まっています。Qt C++で作成したオブジェクトにクラスを作成するメソッドを使用する

私は、オブジェクトを作成するGUIクラスを持っています。この新しいオブジェクトのメソッドから、クラスの作成メソッドを使いたいです。

私はGui(PBV)にline_EditsとComboboxを持っています。このコンボボックスでは、さまざまなジオメトリの中から選択できます。 Geo_1、Geo_2、...ジオメトリを継承しています。コンボボックスのエントリに応じて、私は異なるオブジェクト(Geo_1、Geo_2、...)を作成して、Geo_n-Objectのニーズに応じて作成するクラスのlineEditsを設定したいと考えています。私は、信号スロット

であることを行うにはdon`want

私はその上の別の質問をしたが、私はさらに取得can`t。

私は何とか再帰的な感じです...解決策はありますか?

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 
} 

EDIT Additonally

、あなたは、クロスなどが2つのヘッダ(PBVとGeo_1)になります問題に遭遇するかもしれませんこの問題を解決するには、PBV.hのGeo_1とGeo_1のforward-declareのインクルードを削除します。ちょうどこのように:

class Geo_1; 

あなたのPBVクラスを宣言する前に。

+0

これは機能しません。 Geo:1 * GEO_1私はPBV.hで宣言すると、Geo_1を知っていることを示す正しい色(紫色と赤色)で表示されます。 PbVの上に "../Geometry/Geo_1.h"を#includeしていたのでこれはOKです。hしかし、まだエラーが表示されます.Geo_1は型を指定せず、Geo_1 :: Geo_1(PBV *)のプロトタイプはGeo_1クラスのいずれとも一致しません。なぜ ???????私は#includesと関係があるという印象を持っています – user3443063

関連する問題