How can I include pen width to QPainterPath (QGraphicsItem Selection Problem)?



  • Hi all,

    I've defined a new class inherited from QGraphicsItem. It's bounding rect has an arrow and curve. It can be easily selected by clicking it's arrow. But it's curve can't. I suppose the reason is corresponding with pen width. Please look at the following code. If I use mBoundingPath in the function checkPointSet, this problem occurs. If I use mRectF, it also has been selected when I click near of the graphic. How can I include pen width to mBoundingPath?

    @
    bool Flow::checkPointSet(QPointF point)
    {
    bool rc = false;

    if (mBoundingPath.contains(point))
    //if (mRectF.contains(point))
    {
        rc = true;
    }
    
    return rc;
    

    }
    void Flow::updateBoundingRect()
    {
    mBoundingPath = QPainterPath();

    addArrow();
    addCurve();
    
    prepareGeometryChange();
    mRectF = mBoundingPath.boundingRect();
    update();
    

    }
    void Flow::addArrow()
    {
    ...
    mArrowHead.clear();
    mArrowHead.append(qLine.p2());
    mArrowHead.append(arrowP1);
    mArrowHead.append(arrowP2);
    mBoundingPath.addPolygon(mArrowHead);
    }
    void Flow::addCurve()
    {
    ...
    mCurvePath = QPainterPath();
    mCurvePath.moveTo(mNodeList[0]->coord());
    mCurvePath.lineTo(mNodeList[1]->coord());

    mBoundingPath.addPath(mCurvePath);
    

    }

    QPainterPath Flow::shape() const
    {
    QPainterPath path;
    path.addRect(mRectF);

    return path;
    }

    QRectF Flow::boundingRect() const
    {
    return mRectF;
    }@

    Thanks in advance for your helps,



  • To include the pen width in your bounding shape, you can use QPainterPathStroker:

    @QPainterPathStroker stroker;
    stroker.setWidth(w);
    mBoundingPath = stroker.createStroke(mCurvePath);@



  • Thanks so much to you.

    It has worked with your help. Thanks a lot again.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.