这似乎是确实有效的,或至少是一个工作方法,尽管我的问题有些宽泛,最后总会有人找到有用的答案:
/*
* QList<QGraphicsPathItem *> mList;
* bool mErase;
* QPainterPath * mCurrentPath;
* QPainterPathStroker mStroker;
*/
void ScribbleArea::mousePressEvent (QGraphicsSceneMouseEvent* event)
{
if(!mErase)
{
mCurrentPath = new QPainterPath();
mCurrentPath->moveTo(event->lastScenePos());
mList.append(addPath(mStroker.createStroke(*mCurrentPath), QPen(Qt::red), QBrush(Qt::red)));
}
QGraphicsScene::mousePressEvent(event);
}
void ScribbleArea::mouseMoveEvent (QGraphicsSceneMouseEvent* event)
{
if(!mErase)
{
mCurrentPath->lineTo(event->lastScenePos());
mList[mList.count()-1]->setPath(mStroker.createStroke(*mCurrentPath));
}
else
{
for(int i=0; i < mList.count(); i++)
{
if(mList[i]->isUnderMouse())
{
removeItem(mList[i]);
delete mList[i];
mList.removeAt(i);
}
}
}
QGraphicsScene::mouseMoveEvent(event);
}
这些是两个关键的功能,在mousePressEvent上我开始一个新的路径并将其移动到当前的鼠标位置,接下来,我添加QGraphicsScene的路径以获取指向QGraphicsPathItem的指针,我将在mouseMoveEvent函数中使用它。
在mouseMoveEvent函数中,我首先检查我们是否正在擦除或绘图。在绘制的情况下,我从最后一个路径添加一行到当前的鼠标位置,并再次将路径添加到QGraphicsScene,否则它不会显示这些新行(也许有更好的方法,我愿意改进),这就是绘画。 如果我们正在删除,我会检查所有路径的列表,并检查是否有任何路径当前位于鼠标下方,如果是,请将其删除。
这是目前只是一个草案,更多的改进来。
绘图/涂鸦的一些真棒例如:
很抱歉,但你的问题过于宽泛,不适合这么久。 –