PDA

View Full Version : Using boundingRegion instead of boundingRect



lynchkp
19th December 2010, 15:16
Hello,

I'm trying to implement some hovering commands on a quiver plot I've made using a QGraphicsItem. I would like the arrows to be highlighted when the mouse is over them, but I'm running into issues because a rectangular boundingRect() causes overlap sometimes. Because of this I'd like to use boundingRegion() with a high granularity, however I am unsure how to implement this in my class. I've looked all around for examples of its use but I havent found too many. How is boundingRegion() used instead of boundingRect? Thanks for all the help!!

Here is a snippet of the code I have for the items:



class SimpleItem : public QGraphicsItem
{
public:

qreal dx, dy;
qreal subtract_x, subtract_y;
qreal scale_factor;
bool test;
bool highlight_flag;
qint32 vector_color_type;
qreal snr;
qreal scale_max;
QPen textPen;
QColor color;
qreal dmag;

SimpleItem(qreal x_i, qreal y_i, qreal dx_i, qreal dy_i,
qreal subtract_x_i, qreal subtract_y_i,
qreal snr_i, bool test_i,
qreal scale_factor_i, qint32 vector_color_type_i,
qreal scale_max_i,
bool highlight_flag_i) {

this->setPos(x_i,y_i);
this->setAcceptHoverEvents(true);
this->setBoundingRegionGranularity(1);

dx = dx_i;
dy = dy_i;

subtract_x = subtract_x_i;
subtract_y = subtract_y_i;

snr = snr_i;
test = test_i;

highlight_flag = highlight_flag_i;

scale_factor = scale_factor_i;
vector_color_type = vector_color_type_i;

scale_max = scale_max_i;

dmag = sqrt(pow(dx-subtract_x,2) + pow(dy-subtract_y,2));
findColor();

}

QRectF boundingRect() const
{
return QRectF(-10,-10,20,20);
}

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{

textPen = QPen(color);

qreal tangent = atan2 ((dy-subtract_y),(dx-subtract_x));
qreal x2 = (dx-subtract_x)*scale_factor;
qreal y2 = (dy-subtract_y)*scale_factor;
qreal pa_x = x2 - (scale_factor*dmag/6) * cos (tangent + M_PI / 7);
qreal pa_y = y2 - (scale_factor*dmag/6) * sin (tangent + M_PI / 7);
qreal pb_x = x2 - (scale_factor*dmag/6) * cos (tangent - M_PI / 7);
qreal pb_y = y2 - (scale_factor*dmag/6) * sin (tangent - M_PI / 7);

QVector<QLineF> points(3);
points[0] = QLineF(0,0,x2,y2);
points[1] = QLineF(pa_x,pa_y,x2,y2);
points[2] = QLineF(pb_x,pb_y,x2,y2);

textPen.setWidth(2);
textPen.setCapStyle(Qt::RoundCap);
painter->setPen(textPen);
painter->drawLines(points);

}


};

wysota
19th December 2010, 16:47
Collisions are checked against shape() and not boundingRect(). Whatever you do with the granularity, it will not change anything until you reimplement shape() for your item.