PDA

View Full Version : Cant access to superclass ...



tonnot
23rd November 2011, 10:47
I have a G_item_base class, it has some properties I want to use at G_item_rect (a QGraphicsRect subclassed ) and others...
In example, I have this method:


A_GItem_base::A_GItem_base(qint64 internal_pointer, WW::Graph_type my_type) {
w_internal_type= QGraphicsItem::UserType + my_type; }

void A_GItem_base::w_set_pen(QPen my_pen ) {
((QAbstractGraphicsShapeItem*)this->metaObject()->superClass())->setPen(my_pen); }

And the G_item_rect is declared as:

A_GItem_rect::A_GItem_rect (qint64 internal_pointer QGraphicsScene *scene, QGraphicsItem *parent ) : QGraphicsRectItem (parent,scene) , A_GItem_base(internal_pointer, WW::Graph_rectangle)


Ok, I can't use Q_object macro. I have
" Class contains Q_OBJECT macro but does not inherit from QObject"

Any help ? Thanks

Ginsengelf
23rd November 2011, 11:12
Hi, you don't need the Q_OBJECT macro in the class if it doesn't use signals and slots.

Ginsengelf

tonnot
23rd November 2011, 12:08
Ok, ... What casting can i do .... I have tested all...

((QAbstractGraphicsShapeItem *) this)->setPen(my_pen);
QAbstractGraphicsShapeItem * r = reinterpret_cast<QAbstractGraphicsShapeItem*>(this) ;

I have crash for both ...

Thanks.

FelixB
23rd November 2011, 12:25
please post full class declaration

tonnot
23rd November 2011, 13:01
Here you are :


class A_GItem_base {
public:
A_GItem_base(WW::Graph_type my_type);
void w_set_pen(QPen my_pen );
};

A_GItem_base::A_GItem_base( WW::Graph_type my_type) { }

void A_GItem_base::w_set_pen(QPen my_pen ) {
// or
QAbstractGraphicsShapeItem * r = reinterpret_cast<QAbstractGraphicsShapeItem*>(this) ;
r->setPen(my_pen);
// or
((QAbstractGraphicsShapeItem *) this)->setPen(my_pen);
// both crashes
}

class A_GItem_rect : public QGraphicsRectItem , public A_GItem_base {
public:
A_GItem_rect(QGraphicsScene *scene = 0, QGraphicsItem *parent = 0 );
void w_set_rect( QRect rectf) ;
};

A_GItem_rect::A_GItem_rect (QGraphicsScene *scene, QGraphicsItem *parent ) : QGraphicsRectItem (parent,scene) , A_GItem_base(WW::Graph_rectangle) { }
void A_GItem_rect::w_set_rect( QRect rectf) { this->setRect(rectf); }

I have :
A_GItem_rect * my_rect = new A_GItem_rect(this->scene()) ;
my_rect->w_set_pen(a_pen);


I'm trying to do this strange casting because I dont know how to access to the superclass of A_GItem_Base ...


Thanks

Ginsengelf
24th November 2011, 08:07
Hi, do you really need the w_set_pen() method in A_GItem_base? Because if you remove it and call setPen() on your instance of A_GItem_Rect, the method of the base class should be called.

Ginsengelf

ChrisW67
24th November 2011, 08:50
Ok, I can't use Q_object macro. I have
" Class contains Q_OBJECT macro but does not inherit from QObject"

Any help ? Thanks

The error message tells you exactly what the problem is. The Q_OBJECT macro installs some support code for the Qt meta-object system. A_GItem_base is not derived from QObject and therefore does not support anything that needs the Qt meta-object extensions... including signals and slots.

tonnot
24th November 2011, 09:05
Ginsengelf:
Yes I need to have control on this. (I know I can use setpen()... ) .

Chris: Yes, I understand. I wanted to use metaobject to discover the superclass.

By now, I have w_set_pen for every class....

Class A
Class B : extending A and Z
Class C : extending A and Z
...
Class M : extending A and Z

I need to set or use a Z property/method from A. And I need to check it and other things.
So.... How can I, inside A , have access to Z inherited methods of my superclass (B, C, or any ).
Thanks for your patience

stampede
24th November 2011, 19:46
Why not trying with composition instead of inheritance:


class A_GItem_base {
public:
A_GItem_base(WW::Graph_type my_type) : _itemPtr(NULL){
}
void w_set_pen(QPen my_pen ){
if( _itemPtr ) _itemPtr->setPen(my_pen);
}
// here you can implement all methods that will use the QGraphicsItem class methods
protected:
QGraphicsItem * _itemPtr;
};

class A_GItem_rect : public A_GItem_base {
public:
A_GItem_rect(QGraphicsScene *scene = 0, QGraphicsItem *parent = 0 ) : A_GItem_base(WW::Graph_rectangle){
this->_itemPtr = new QGraphicsRectItem(parent,scene);
}
void w_set_rect( QRect rectf){
if( QGraphicsRectItem * r = dynamic_cast<QGraphicsRectItem*>(_itemPtr) )
r->setRect(rectf);
}
};


If somehow in class "base" you need to know some data or methods specific to A_GItem_rect, then you should reconsider your design, and use virtual methods properly.

tonnot
24th November 2011, 20:13
Ok, this point of view is really what I need.
Thank you very much.