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.
}
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.
}