全部,我实现了一个多边形的QGraphicsItem。我使用QGraphicsEllipseItem
作为点(用于拖动功能)来加速开发。但是,我现在在update()
功能方面遇到了困难。我的代码发布在最后,我的问题是:
QGraphicsItem::update()
吗?我称之为其他一些信息:
QObject
。这允许我在场景()(我知道已使用eventFilter
设置)上安装itemChange
。在场景中,我在鼠标移动过程中过滤QGraphicsSceneMouseEvent
并调用QGraphicsItem::update()
,否则我的线条将不会被重绘。InteractivePolygon
时,我的线条不会被删除!我必须调用scene() - > update。我觉得有些不对劲。声明:
class InteractivePolygon : public QGraphicsItem
{
public:
//Only important methods
QRectF boundingRect() const;
void paint(bla bla bla);
bool eventFilter(QObject *, QEvent *);
private:
QList<QGraphicsEllipseItem *> m_points;
void AddPolygonPoint(QPointF);
QGraphicsEllipseItem * MakeNewPoint(QPointF);
}
实现:
QRectF InteractivePolygon::boundingRect() const
{
return childrenBoundingRect();
}
void InteractivePolygon::paint(QPainter painter.. otherstuf)
{
QPen line_pen(QColor(255,0,0));
painter->setPen(line_pen);
if(m_points.count() > 1)
{
for(int i = 1; i < m_points.count(); ++i)
painter->drawLine(m_points[i-1]->pos(), m_points[i]->pos());
}
}
void AddPolygonPoint(QRectF point)
{
QGraphicsEllipseItem * new_item = MakeNewPoint(point);
new_item->setParent(this);
m_points->push_front(new_item);
update();
}
QGraphicsEllipseItem * InteractivePolygon::MakeNewPoint(QPointF & new_point)
{
QGraphicsEllipseItem * result = 0;
result = new QGraphicsEllipseItem();
result->setPos(new_point);
result->setRect(-4, -4, 8, 8);
result->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable)
return result;
}
//Lets pretend this method is correctly setup/exists
bool InteractivePolygon::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::QEvent::GraphicsSceneMouseMove)
{
QGraphicsSceneMouseEvent * mouse_move = (QGraphicsSceneMouseEvent *)event;
//Selected index is set else, let's assume it works
if(selected_index)
{
update(); //If I don't do this, my lines in my paint() are not redrawn.
}
}
}
答案 0 :(得分:0)
每次我更改项目中的任何内容都会更改其边界框时,解决方案为prepareGeometryChange()
。一切都正确地重新绘制并根据需要进行更新。
这允许我删除所有update()
来电。