Results 1 to 8 of 8

Thread: QGraphicsPathItem selected by mouse click

  1. #1
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsPathItem selected by mouse click

    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?
    polyline.png

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsPathItem selected by mouse click

    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
    Qt Code:
    1. void QGraphicsPathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    2. QWidget *widget)
    3. {
    4. Q_UNUSED(widget);
    5. painter->setPen(d->pen);
    6. painter->setBrush(d->brush);
    7. painter->drawPath(d->path);
    8.  
    9. if (option->state & QStyle::State_Selected)
    10. qt_graphicsItem_highlightSelected(this, painter, option); // Replace this with your own outline painting in custom item
    11. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    johnson604 (2nd September 2013)

  4. #3
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPathItem selected by mouse click

    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 ?

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsPathItem selected by mouse click

    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).
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPathItem selected by mouse click

    To draw my selection outline in paint() function? Or in Boundingrect(), or shape() function ?

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsPathItem selected by mouse click

    You have to do it in paint().
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #7
    Join Date
    May 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsPathItem selected by mouse click

    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.

  9. #8
    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 selected by mouse click

    The solution is to actually reimplement shape() which deals with selections.
    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. Replies: 2
    Last Post: 16th July 2012, 12:40
  2. How to get the selected row when click the button in it?
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2011, 10:00
  3. Mouse Right or Left click ???
    By anjanu in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2011, 18:41
  4. Replies: 4
    Last Post: 5th February 2011, 08:33
  5. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 09:24

Tags for this Thread

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.