0
我还有一个问题: 我有一个类PBV t(从Tab继承)hat有一个类几何。几何是Geo_1的父项。来自Geo_1我想访问PBV的方法(例如,printContent。我该怎么做?我能够创建信号插槽,但是由于我必须经常使用PBV方法来制作大量信号插槽。 这里是我的代码:Qt C++内部类访问外部类
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() { }
'dynamic_cast' /'static_cast'? – LogicStuff
在Geo_1的构造函数中,通过指针可以引用父对象。由于您传递了“this”指针(因为我看到这些行位于PBV.cpp文件中,所以它是一个假设),因此该父级是PBV类。所以如果你打算在这个指针上使用dynamic_cast就像这个dynamic_cast(_parent);您将能够访问PBV类的printContent。 –
对不起,我不习惯动态/静态演员。你什么意思? – user3443063