Results 1 to 3 of 3

Thread: qgraphicspathitem how to get the outline

  1. #1
    Join Date
    Sep 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default qgraphicspathitem how to get the outline

    2014-09-25 17_44_56 _______________.png 1.png 1.png

    the color of semicircle changed when my mouse hover in half round area. But I want the color changed when my mouse right on the circle(outline) , not half round area.How to write the code?
    Last edited by tf520; 25th September 2014 at 11:11.

  2. #2
    Join Date
    Sep 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: qgraphicspathitem how to get the outline

    My Code like this, how to edit it ?
    Qt Code:
    1. #include "item.h"
    2.  
    3. Item::Item()
    4. {
    5. hover = false;
    6. setAcceptHoverEvents(true);
    7. }
    8.  
    9. QRectF Item::boundingRect() const
    10. {
    11. qreal extra = (pen().width() + 20) / 2.0;
    12. return QRectF(path().boundingRect()).normalized().adjusted(-extra, -extra, extra, extra);
    13. }
    14.  
    15. QPainterPath Item::shape() const
    16. {
    17. QRectF rect(0, 0, 100, 100);
    18. path.arcMoveTo(rect, 0);
    19. path.arcTo(rect, 0, 180);
    20. return path;
    21. }
    22.  
    23. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    24. {
    25.  
    26. QPen mypen = pen();
    27. QBrush mybrush;
    28. mybrush.setStyle(Qt::NoBrush);
    29. mypen.setWidth(2);
    30. if(hover)
    31. {
    32. mypen.setColor(Qt::red);
    33. }
    34. else
    35. {
    36. mypen.setColor(Qt::black);
    37. }
    38. painter->setRenderHint(QPainter::Antialiasing,true);
    39. painter->setPen(mypen);
    40. painter->setBrush(mybrush);
    41.  
    42. QRectF rect(0, 0, 100, 100);
    43. path.arcMoveTo(rect, 0);
    44. path.arcTo(rect, 0, 180);
    45.  
    46. setPath(path);
    47. painter->drawPath(path);
    48. }
    49.  
    50. void Item::hoverEnterEvent(QGraphicsSceneHoverEvent *)
    51. {
    52. hover = true;
    53. update();
    54. }
    55.  
    56. void Item::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
    57. {
    58. hover = false;
    59. update();
    60. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qgraphicspathitem how to get the outline

    shape() is used for detecting collisions. So it is a matter of getting shape() implemented correctly. On the other hand hover events probably use the bounding rect, so you should check for shape collision in your hover events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QGraphicsPathItem selected by mouse click
    By johnson604 in forum Qt Programming
    Replies: 7
    Last Post: 17th December 2013, 08:03
  2. QGraphicsPathItem changing state when hovered
    By Vit Stepanek in forum Qt Programming
    Replies: 4
    Last Post: 8th December 2010, 16:44
  3. QGraphicsPathItem
    By tonio in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2009, 14:35
  4. QGraphicsPathItem - slow on Mac, fine on X11
    By aeh in forum Qt Programming
    Replies: 0
    Last Post: 18th May 2009, 12:42
  5. QGraphicsPathItem Doubt
    By arjunasd in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2007, 03:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.