PDA

View Full Version : QGraphicsItem Mouse-handling



roband915
20th February 2011, 16:14
In my subclassed QGraphicsItem I'd like to draw some points with my mouse. One click with the mouse will result in one point showing up at the screen.When I doubleclick the item will be added to the scene. Is this possible or must the item be added to the scene and then updated? Can anybody help me understand how I can get the mousepressed-event to my Item?

MyScene.cpp


void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsScene::mousePressEvent(mouseEvent);
QPointF curPos= mouseEvent->lastScenePos();

//Create new item
if(Mouse.iClicks == 0)
{
qDebug("Creating new spline.");
newSpline = new itemSpline();
}

newSpline->setVertices(curPos);

Mouse.iClicks++;
}

void MyScene::mouseDoubleClickEvent(QGraphicsSceneMouse Event *mouseEvent)
{
//TODO: Remove last point because dblclick takes one position.

qDebug("MyScene: DoubleClickEvent");
if(newSpline->size() > 1)
{
qDebug("Adding item");
this->addItem(newSpline);
this->addRect(newSpline->boundingRect());
}
Mouse.iClicks = 0;
}


MyItem.cpp


void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::lightGray);
//Draw line.
}

void item::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
//Catch mouse pos.
QMessageBox m;
m.setText("Test");
m.exec();
}

void item::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QMessageBox m;
m.setText("Test");
m.exec();
}

void item::mouseMoveEvent(QGraphicsSceneEvent *event)
{
//Draw line from start pos to current pos.
}

GreenScape
21st February 2011, 06:01
#
//Create new item
#
if(Mouse.iClicks == 0)
#
{
#
qDebug("Creating new spline.");
#
----> newSpline = new itemSpline();
#
}

newSpline wasn't added to the scene, why do u belive scene should draw it?

You need to understand what QGraphicsScene actually is. It is visualizer of the QGraphicsItems, that's mean u need thing u want to visualize to be QGraphicsItem and to be added to this scene.
But since Qt is flexible, u always can reimplement QGraphicScene's paint() event. Good luck.