Results 1 to 7 of 7

Thread: Increase margin for detecting tooltip events of QGraphicsLineItem?

  1. #1
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    34
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Increase margin for detecting tooltip events of QGraphicsLineItem?

    Hi,

    I have a QGraphicsView/QGraphicsScene setup with several QGraphicsLineItem:s. Now I would like to increase the margin for detecting tooltip events (QEvent::ToolTip) of these line items.

    I was able to affect the margin for detecting mousePressEvent:s by returning a "wider line" from the shape(), like this:
    Qt Code:
    1. QPainterPath MyLineGraphicsItem::shape() const
    2. {
    3. stroker.setWidth(qMax(4, pen().width() + 3));
    4. path.moveTo(line().p1());
    5. path.lineTo(line().p2());
    6. return stroker.createStroke(path);
    7. }
    To copy to clipboard, switch view to plain text mode 
    However, this does not seem to affect the detection of tooltip events. Anyone knows what determines that event if shape() does not?

    Many thanks in advance.
    /U

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    Reimplement boundingRect() and return rectangle as big as you want the tooltip to trigger.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    That's probably not going to give very good results if the line goes diagonally from one corner to another. That would give a bounds as big as the whole window, which means the tooltip would be triggered no matter where the user clicked.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    Try returning your reimplemented shape() as the boundingRect of your item.
    Qt Code:
    1. QRectF MyLineGraphicsItem::boundingRect() const{
    2. return shape().boundingRect();
    3. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    I think that will end up doing the same thing as Spitfire's suggestion when the line is a diagonal. The bounding rect for a line shape that goes from corner to corner will still be the entire window.

    The good news is that it is QGraphicsItem::shape() that is called when the mouse hovers over the item (in response to a QGraphicsScene::helpEvent() - that's what will eventually invoke the tooltip). So, you can implement shape() for your line to return a path containing a "fat" line (a QPolygon probably would work) that is as wide as it needs to be to give a good margin for the tooltip detection.

    Be careful, though - this will have side effects if you also are relying on collision detection. The more "fat" you make the shape. the bigger it becomes for collision detection. It will collide with another item as soon as the other item overlaps with the fat shape, even though it isn't actually touching the line itself.

    If you don't care about collisions, then you can do some interesting things. For example, if you only want the tooltip to appear when the mouse is at the ends of the line, then I'm guessing you could define the QPainterPath returned by shape() to simply be two ellipses of appropriate dimensions centered on the two endpoints.
    Last edited by d_stranz; 1st June 2012 at 06:33.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    Quote Originally Posted by d_stranz View Post
    . . . So, you can implement shape() for your line to return a path containing a "fat" line (a QPolygon probably would work) that is as wide as it needs to be to give a good margin for the tooltip detection.
    If I'm not mistaken that's what the OP is saying doesn't work. Returning shape() as the bounding rect seems to work:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class LineItem : public QGraphicsLineItem
    4. {
    5. public:
    6. LineItem(qreal x, qreal y, qreal w, qreal h): QGraphicsLineItem(x,y,w,h){}
    7. QPainterPath shape() const{
    8. path.moveTo(line().p1());
    9. path.lineTo(line().p2());
    10. stroker.setWidth(50);
    11. return stroker.createStroke(path);
    12. }
    13. QRectF boundingRect() const{
    14. return shape().boundingRect();
    15. }
    16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    17. painter->fillPath(shape(),QBrush(QColor(0,0,0,10)));
    18. QGraphicsLineItem::paint(painter, option, widget);
    19. }
    20. };
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QApplication a(argc, argv);
    25. LineItem horizontalLine(0,0,200,0);
    26. horizontalLine.setToolTip("horizontal line tool tip");
    27. scene.addItem(&horizontalLine);
    28. LineItem diagonalLine(0,100,200,300);
    29. diagonalLine.setToolTip("diagonal line tool tip");
    30. diagonalLine.setFlag(QGraphicsItem::ItemIsMovable);
    31. scene.addItem(&diagonalLine);
    32. view.setScene(&scene);
    33. view.setMinimumSize(300,400);
    34. view.show();
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    Note: I just threw the paint function in there to show the area where the tool tip will be shown. It is not necessary.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Increase margin for detecting tooltip events of QGraphicsLineItem?

    Returning shape() as the bounding rect seems to work
    I guess my point is that because the bounding rect is always aligned with the horizontal and vertical, the further you tilt the line away from horizontal or vertical, the bigger the bounding rect becomes. If a line goes diagonally from topLeft to bottomRight of a window, then the bounding rect covers the entire window, which means the tooltip will be triggered from anywhere in the whole window, not just near the line.

    I am not sure why the stroker didn't work in the OP. Perhaps the stroker doesn't have any "real" width (i.e. doesn't actually describe an enclosed area in terms of a path). That's why I suggested using a QPolygon instead, or maybe a path described by two half-circles at each end of the line, connected by two parallel lines to create a closed area.

Similar Threads

  1. USB Win Events messages Detecting in QT
    By AlphaWolfXV in forum Qt Programming
    Replies: 3
    Last Post: 16th August 2013, 15:33
  2. How do i get QGraphicsLineItem coordinates?
    By chaltier in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2012, 06:41
  3. Replies: 1
    Last Post: 10th February 2011, 00:06
  4. QGraphicsLineItem + setAcceptHoverEvents
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 13:13
  5. Detecting KeyPad Events from my Keypad
    By Svaths in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th August 2006, 08:28

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.