tf520
25th September 2014, 11:56
10636 10637 10638
the color of semicircle changed when my mouse hover in half round area. But I want the color changed when my mouse right on the circle(outline) , not half round area.How to write the code?
tf520
26th September 2014, 03:36
My Code like this, how to edit it ?
#include "item.h"
Item::Item()
{
hover = false;
setAcceptHoverEvents(true);
}
QRectF Item::boundingRect() const
{
qreal extra = (pen().width() + 20) / 2.0;
return QRectF(path().boundingRect()).normalized().adjuste d(-extra, -extra, extra, extra);
}
QPainterPath Item::shape() const
{
QPainterPath path;
QRectF rect(0, 0, 100, 100);
path.arcMoveTo(rect, 0);
path.arcTo(rect, 0, 180);
return path;
}
void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen mypen = pen();
QBrush mybrush;
mybrush.setStyle(Qt::NoBrush);
mypen.setWidth(2);
if(hover)
{
mypen.setColor(Qt::red);
}
else
{
mypen.setColor(Qt::black);
}
painter->setRenderHint(QPainter::Antialiasing,true);
painter->setPen(mypen);
painter->setBrush(mybrush);
QPainterPath path;
QRectF rect(0, 0, 100, 100);
path.arcMoveTo(rect, 0);
path.arcTo(rect, 0, 180);
setPath(path);
painter->drawPath(path);
}
void Item::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
hover = true;
update();
}
void Item::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
hover = false;
update();
}
wysota
28th September 2014, 00:05
shape() is used for detecting collisions. So it is a matter of getting shape() implemented correctly. On the other hand hover events probably use the bounding rect, so you should check for shape collision in your hover events.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.