PDA

View Full Version : How to manage multi QGraphicsItem



Marcofe
16th October 2013, 10:59
Hello,
I've a class Scene extending QGraphicsSceneand to catch click mouse i'm using the function mousePressEvent ( QGraphicsSceneMouseEvent* event ). This function is coded like:


void Scene::mousePressEvent ( QGraphicsSceneMouseEvent* event ) {
// set local variables and check if existing station clicked
qreal x = event->scenePos().x();
qreal y = event->scenePos().y();

GraphItem _item = dynamic_cast<GraphItem*> ( itemAt ( x, y ) );

if ( GraphItem == 0 && event->button() == Qt::LeftButton ) {
m_undoStack->push ( new CommandGraphItemAdd ( this, x, y ) );
emit message ( QString ( "GraphItem add at %1,%2" ).arg ( x ).arg ( y ) );
}

QGraphicsScene::mousePressEvent ( event );
this->update();
}


My problem is how to create various QGraphicItem into the mousePressEvent function ? For example i click a button into menu option and a point is selecting, or a circle or other.
My solution is using a switch that reads a value (for example an int value, it set when a button is clicked), but for me isn't optimal solution. What do you think?Exist any different solution?
tnx so much

JackHammer
16th October 2013, 12:31
May be you could use factory pattern for creating the graphics items so that you could keep the changes minimal later.

Marcofe
17th October 2013, 09:12
JackHammer, I don't know this pattern and this is a way to learn a new thing about programming...so I’m looking for it.
Tnx so much for your answer. I'll inform you for news. Bye ;)