PDA

View Full Version : QGraphicsPathItem selected by mouse click



johnson604
30th August 2013, 14:00
As the title, I subclass a QGraphicsPathItem to draw a polyline and make it selectable. When I select it by mouse click, it seems to be selected by its boundingRect(). Just like picture bellow. But I want to select it by its shape, how to do it?
9504

Santosh Reddy
30th August 2013, 14:09
There is no option to change the default selection outline. Only way out is to create a custom item and re-implement paint() and while painting draw the selection outline as you like.

For more information have a look at implementation of QGraphicsPathItem::paint(). This is how it looks like


void QGraphicsPathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_D(QGraphicsPathItem);
Q_UNUSED(widget);
painter->setPen(d->pen);
painter->setBrush(d->brush);
painter->drawPath(d->path);

if (option->state & QStyle::State_Selected)
qt_graphicsItem_highlightSelected(this, painter, option); // Replace this with your own outline painting in custom item
}

johnson604
2nd September 2013, 17:04
Thank you very much, Santosh.
But repaint the selection outline is not my purpose. I want to select the path when I click in my own polygon, not in the default rectangle. I don't care how to draw the selection outline. Any good idea ?

Santosh Reddy
3rd September 2013, 07:32
But repaint the selection outline is not my purpose. I want to select the path when I click in my own polygon, not in the default rectangle. I don't care how to draw the selection outline. Any good idea ?
I already said the only way it to draw your own selection outline (instead of Qt dedault i.e. rectangle).

johnson604
3rd September 2013, 08:11
To draw my selection outline in paint() function? Or in Boundingrect(), or shape() function ?

Santosh Reddy
3rd September 2013, 08:19
You have to do it in paint().

Onanymous
16th December 2013, 18:43
I just came over this thread searching for same problem solution and I believe you did not get question at all. The guy wants to select by clicking the path line, not clicking someshere in the rectangle containing the path. He does not care how selection area is painted. And the solution I found is to reimplement contains(), however it might be tricky for arbitrary path, as you do not want this function to be expensive.

wysota
17th December 2013, 09:03
The solution is to actually reimplement shape() which deals with selections.