Solved Class Promotion ?
-
Why do your shapes of class A and B have to be related at all? When ever you have an object of A on your scene and you want to "turn it into" an object of B, just create a new object of B, copy the relevant data from A to B and destroy the original A object.
-
@Wieland
By doing that , i lose all the key and mouse events that happen to class A.
-
You mean events that have been sent to A but haven't been "delivered" to it yet?
-
Yes , how can i assign all of them to the new class B ?
-
Call QCoreApplication::processEvents before destroying A.
-
Maybe i should do this instead :
I start by creating a class B that uses the paintEvent of class A , then when i reach the certain state , it calls the paintEvent of class B .
I have no problem doing it now , since the difference is only in the painting , but later when the new classes have new variables , that'll be a problem .
-
@Wieland
Maybe a small piece of code will help me understand better , and thank you :)
-
I'm sure the answer i'm looking for lays somewhere between :
- dynamic_cast
- virtual
- operator=
-
I dont understand why it ever need to change "type".
I think you need to rethink the design.
Normally u use base class pointers and childs for polymorph operations but
i cannot understand your use case.Its like circle that must sudden draw as a rect?
-
@mrjj
@WielandI'll try to minimize the code , to help clarify my situation :
Class myScene : public QGraphicsScene { //... void MousePressEvent(QGraphicsSceneMouseEvent *event) //... private: A *a; //... }
"myScene.cpp"
myScene::myScene { a = new A; } void myScene::MousePressEvent(QGraphicsSceneMouseEvent *event) { a->doesSmth(); if(a->reachesSomePoint()) a = new B; // Compiles good but i lose the events and the whole paint of a; }
Class A : public QObject , public QGraphicsItem { //... A(); QRectF boundingRect() const Q_DECL_OVERRIDE; QPainterPath shape() const Q_DECL_OVERRIDE; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE; // This is the first painting //... protected: qreal myValue1; int myValue2; //... }
Class B : public Class A { void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE; // This is the second painting //Future Variables private: qreal futureValue1; //... }
-
hi
You only need to go from base to child?
You can use a cast for that.
http://www.bogotobogo.com/cplusplus/upcasting_downcasting.phpand you dont lose A paint as you are free to call the A::paintEvent from
B::paintEvent. -
Thanks for your help , glad you stayed with me till the bottom ;)
Imma read the content of your link :) -
Hmm , very informative , and i can get the slicing problem now :)
But , it won't be of any harm till late versions of my program :)
Thank you very much :DTopic->setAsSloved(true);
-
@Walux
Np.
Its not always you run into slicing but its good to know it can happen.