Results 1 to 7 of 7

Thread: Increase margin for detecting tooltip events of QGraphicsLineItem?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 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, 14:33
  2. How do i get QGraphicsLineItem coordinates?
    By chaltier in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2012, 05:41
  3. Replies: 1
    Last Post: 9th February 2011, 23:06
  4. QGraphicsLineItem + setAcceptHoverEvents
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 12:13
  5. Detecting KeyPad Events from my Keypad
    By Svaths in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th August 2006, 07: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
  •  
Qt is a trademark of The Qt Company.