Results 1 to 5 of 5

Thread: How to enlarge the grap mouse selection

  1. #1
    Join Date
    Jun 2010
    Location
    Italy
    Posts
    10
    Qt products
    Platforms
    Unix/X11 Windows

    Default How to enlarge the grap mouse selection

    Hi All,
    I have a QGraphicsScene in witch I add some items like line etc etc ..

    Each item derived from QGraphicsItem and implements the hoverEnterEvent and hoverLeaveEvent to change the color of the entity.
    Whit this system I can make an highlight when the mouse pass over the item.

    Sometimes to highlight the entity I need to go very close to the entity and in case of thin line it's very difficult to get the entity highlighted and selected.

    so there is any way to enlarge the mouse grap ?
    I try to use the scene.setSelectionArea function passing a bigger rectangle in this way under the scene.mouseMoveEvent

    Qt Code:
    1. path=QtGui.QPainterPath()
    2. path.addRect(scenePos.x()-self.__grapWithd/2, scenePos.y()+self.__grapWithd/2, self.__grapWithd, self.__grapWithd)
    3. self.setSelectionArea(path)
    To copy to clipboard, switch view to plain text mode 

    but this code dose not give me any help ..

    any help ?
    Thanks,
    Matteo

  2. #2
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge the grap mouse selection

    Hi,

    you might want to overload QGraphicsItem::shape(), which is used by the graphics system to determine if the mouse is actually inside an item:
    Qt Code:
    1. class SomeItem : public QGraphicsItem
    2. {
    3. ...
    4. // here is how to enlarge the "grab" area by 5 pixels in either dimension:
    5. QPainterPath shape() {
    6. path.addRect( boundingRect().adjusted( -5, -5, 5, 5 ) );
    7. return path;
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    Tobi

  3. #3
    Join Date
    Jun 2010
    Location
    Italy
    Posts
    10
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge the grap mouse selection

    Thanks for you suggestion but I already overwrite the shape method.
    And if I do as you suggest the bounding rectangle dose not fit well my shape.
    so in case of overlapping bounding box I do not have the correct highlight.

    Overloading the shape method is the only way to enlarge the grap mouse ?

    Thanks,
    Matteo

  4. #4
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge the grap mouse selection

    I was only pointing out the general principle of how this could be done. Of course if you have an arbitrarily shaped item, creating a QPainterPath containing only an enlarged QRectF will be rather crude. As far as I know, the shape() function is the way to go. You might have to be more specific on how you define the PainterPath, to, for example, better fit a single line. Be creative .

  5. #5
    Join Date
    Jun 2010
    Location
    Italy
    Posts
    10
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to enlarge the grap mouse selection

    Thanks for your responce ...

    I' like to be creative and happy too ..

    I found an easy solution :-)


    this is my base class for item:
    Qt Code:
    1. class BaseEntity(QtGui.QGraphicsItem):
    2. ...
    3. ...
    4.  
    5. def shape(self):
    6. """
    7. overloading of the shape method
    8. """
    9. painterStrock=QtGui.QPainterPathStroker()
    10. path=QtGui.QPainterPath()
    11. self.drawShape(path)
    12. painterStrock.setWidth(self.lineWith)
    13. path1=painterStrock.createStroke(path)
    14. return path1
    15.  
    16. ...
    17. ..
    To copy to clipboard, switch view to plain text mode 

    I derive my object from the above one : and for example the this is the arc one :
    Qt Code:
    1. from Interface.Entity.base import *
    2.  
    3. class Arc(BaseEntity):
    4. """
    5. this class define the arcQT object
    6. """
    7.  
    8. .....
    9.  
    10.  
    11. def drawShape(self, painterPath):
    12. """
    13. overloading of the shape method
    14. """
    15. painterPath.arcTo(self.boundingRect(),self.startAngle,self.spanAngle)
    16.  
    17. ......
    To copy to clipboard, switch view to plain text mode 

    I change the
    Qt Code:
    1. painterStrock.setWidth(self.lineWith)
    To copy to clipboard, switch view to plain text mode 

    in to

    Qt Code:
    1. painterStrock.setWidth(self.lineWith*10)
    To copy to clipboard, switch view to plain text mode 

    This enlarge the with of the shape :-)

    Regards,
    Matteo

Similar Threads

  1. Question about how to do a mouse selection box
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2010, 13:01
  2. Replies: 5
    Last Post: 20th April 2010, 17:16
  3. Enlarge Pushbutton?
    By triperzonak in forum Qt Programming
    Replies: 7
    Last Post: 20th June 2008, 14:58
  4. Dragging mouse selection in QTableWidget
    By yartov in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2008, 15:03
  5. QPixmap/QImage How to enlarge a picture
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2006, 06:38

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.