Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    2
    3
    2962
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y
      yaseminyilmaz last edited by

      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,

      1 Reply Last reply Reply Quote 0
      • R
        Rems last edited by

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

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

        1 Reply Last reply Reply Quote 0
        • Y
          yaseminyilmaz last edited by

          Thanks so much to you.

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post