Drawing direction and radian were the problem. With these:
#define whats16 16
Slice::Slice(float start, float sweep, QColor color, QGraphicsItem* parent)
: QGraphicsItem(parent), m_start(start), m_sweep(sweep), m_color(color){
m_realColor = color;
m_rect = QRectF(0,0,200,200);
m_path = QPainterPath(QPointF(m_rect.width() / 2, m_rect.height() / 2));
m_path.arcTo(m_rect, -m_start, -m_sweep);
m_path.closeSubpath();
dx = 10 * cos((m_start + m_sweep / 2) * M_PI / 180);
dy = 10 * sin((m_start + m_sweep / 2) * M_PI / 180);
setAcceptHoverEvents(true);
}
QRectF Slice::boundingRect() const { return m_rect; }
QPainterPath Slice::shape() const { return m_path;}
void Slice::hoverEnterEvent(QGraphicsSceneHoverEvent*){
m_color = Qt::gray;
moveBy(dx, dy);
}
void Slice::hoverLeaveEvent(QGraphicsSceneHoverEvent*){
m_color = m_realColor;
moveBy(-dx, -dy);
}
void Slice::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*){
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::white);
painter->setBrush(m_color);
painter->drawPie(m_rect, whats16 * -m_start, whats16 * -m_sweep);
}
Now, slice moves perfectly:
[image: cb199f19-a0d1-4d4c-acfc-8eb4bb342d51.gif]
BUT the whole pie moves for a while (5 times as I start hovering over different slices). Why does it move?