0

In my view there are 2 lines. 1 Polyline, and 1 straight lines.

I am not able to select straight line. If I try to select it , polyline gets selected ( though I have not clicked on it )

Selection of polyline is also not sharp. If I clicked somewhere around polyline ( not on Polyline ) still Polyline gets selected.

So I want to have the bounding rect according to the shape of the item, something like:

enter image description here

While selecting any of the overlapping lines, the one with bounding rect on top is selected. Even making use of setZValue won't work here. I want to implement this by minimizing the bounding rect to the shape of line.

To avoid that I want to override shape() and minimize bounding rect.

Here is my code:

Widget.cpp

void Widget::on_designButoon_clicked()
{
    // for straight line
    QPolygon net0;
    net0 << QPoint(50,180);
    net0 << QPoint(600,180);
    MyPoly* _poly0 = new MyPoly();
    _poly0->DrawPolyline(net0);
    scene->addItem(static_cast<QGraphicsPathItem*>(_poly0));
    // same logic for polyline
}

MyPoly.h

 class MyPoly : public QGraphicsPathItem
    {
    public:
        explicit MyPoly();
        MyPoly(QGraphicsScene*);
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
        void DrawPolyline(QPolygon polygon);
        QGraphicsScene* myScene;
        QPainterPath shape() ;
    
    private:
         QPolygon polygon_;
};

MyPoly.cpp

void MyPoly::DrawPolyline(QPolygon polygon)
{
        polygon_ = polygon;
        QPainterPath pPath;
        pPath.addPolygon(polygon);
        this->setPen(QPen(QColor("red"), 2));
        this->setPath(pPath);
        this->setFlag(QGraphicsItem::ItemIsSelectable);
        //this->setBoundingRegionGranularity(1.0); this has no effect
}

void MyPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   QWidget *widget)
{
    auto copied_option = *option;
    copied_option.state &= ~QStyle::State_Selected;
    auto selected = option->state & QStyle::State_Selected;
    QGraphicsPathItem::paint(painter, &copied_option, widget);
    if (selected) {
        painter->save();
        painter->setBrush(Qt::NoBrush);
        painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
        painter->drawPath(shape());
        painter->restore();
    }
}

QPainterPath MyPoly::shape()
{
   // Here I want to minimize the shape of bounding rect
    for(int i=0;i < polygon_.count()-1; i++) 
   {
        QPoint firstPoint = polygon_.at(i);
        QPoint lastPoint = polygon_.at(i+1);
   }
}
  

But not understanding how to do that.

tushar
  • 313
  • 4
  • 10
  • @TheDarkKnight I used your answer for my above problem. But your answer was for 1 line segments ( for 2 points, start point and end point ) But here I am having polygon. So I am not able to use your answer. Can you help me ? Your answer link : https://stackoverflow.com/questions/26271623/customizing-shape-of-bounding-rect – tushar May 28 '22 at 09:19
  • @Marek R I used your answer for my above problem. But your answer was for 1 line segments ( for 2 points, start point and end point ) But here I am having polygon. So I am not able to use your answer. Can you help me ? Your answer link : https://stackoverflow.com/questions/26271623/customizing-shape-of-bounding-rect – tushar May 28 '22 at 09:20
  • @thuga I used your answer for my above problem. But your answer was for 1 line segments ( for 2 points, start point and end point ) But here I am having polygon. So I am not able to use your answer. Can you help me ? Your answer link : https://stackoverflow.com/questions/26271623/customizing-shape-of-bounding-rect – tushar May 28 '22 at 09:21

0 Answers0