QGraphicsItem Mouse-handling
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
Code:
{
QPointF curPos
= mouseEvent
->lastScenePos
();
//Create new item
if(Mouse.iClicks == 0)
{
qDebug("Creating new spline.");
newSpline = new itemSpline();
}
newSpline->setVertices(curPos);
Mouse.iClicks++;
}
{
//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
Code:
{
painter
->setRenderHint
(QPainter::Antialiasing);
painter->setBrush(Qt::lightGray);
//Draw line.
}
{
//Catch mouse pos.
m.setText("Test");
m.exec();
}
{
m.setText("Test");
m.exec();
}
{
//Draw line from start pos to current pos.
}
Re: QGraphicsItem Mouse-handling
Code:
#
//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.