PDA

View Full Version : QGraphicsLineItem how to make it easier to select/move the line?



chaltier
4th April 2012, 07:39
I have sub classed QGraphicsLineItem, set flag movable and selectable, and draw them to a graphic scene..

problem is the bounding rect is the line itself and it is hard to click/select/move them..

what can i do to make the line easier to be selected?

I've tried reimplementing the boundingRect() with QRectF(0,0,100,100) but yields no results..

also increasing the pen width doesnt help either..

thanks alot..

wayfaerer
4th April 2012, 08:08
I would reimplement shape() rather than boundingRect(). shape() is used to detect mouse clicks. The default implementation just calls boundingRect(), though. You could reimplement shape() as something like this:



const int adjustment = 5;
QPainterPath path;
return path.addRect(boundingRect().adjusted(-adjustment, -adjustment, adjustment, adjustment);

Not tested, but I think it should work!

chaltier
4th April 2012, 08:43
Hi thanks for the reply..

When my line is drawn vertically or horizontally, the adjustment does not make any differences..

When my line is drawn diagonally, it creates a bounding rect with my line as the hypotenuse

i only wan my line to be selected when i click on the line..

thanks againn..

chaltier
4th April 2012, 12:53
QPainterPath myLine::shape() const
{
QPainterPath path;
QPainterPathStroker stroker;
stroker.setWidth(30);
path.moveTo(line().p1());
path.lineTo(line().p2());
return stroker.createStroke(path);
}

this is what i did now..i was able to create an invisible handler for me to click it..

anyone knows how do i set color to it so it wont be invisible?

thanks

jeevan_ravula
19th March 2014, 09:12
@Chaltier - Even I m facing the same issue. I reimplemented the QGraphicsLineItem and created my own custom line item. The custom line item is hard to select, press and move. Currently I am using the boundingRect() and shape() methods of QGraphicsLineItem itself. I haven't implemented my own custom methods for these. What shoud I do to fix these issues?