Results 1 to 8 of 8

Thread: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    My QGraphicsItem subclass "parent" item contains two nested levels of children QGraphicsItem [in Qt 4.6.3]. I want the lowest level child QGraphicsItem (a QGraphicsRectItem) to have a tooltip which OVERRIDES the parent's tooltip. But only the parent tooltip is showing up.

    The child QGraphicsItem is calling this method in its constructor:
    setAcceptHoverEvents (true);

    The containing QMainWindow subclass constructor -- and also the QGraphicsView subclass constructor -- are calling this:
    setAttribute (Qt::WA_AlwaysShowToolTips);

    Why isn't the nested child QGraphicsItem's "setToolTip()" having an effect?

    Thank you in advance,
    Phil Weinstein, CADSWES
    Boulder CO USA
    Last edited by philw; 25th November 2010 at 21:42.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    QGraphicsItem::setToolTip() isn't a virtual function, nor is QGraphicsItem::toolTip(). Only virtual functions can be overridden.

    Right now, your subclass is obscuring or hiding the superclass function rather than overriding it. This means the behavior of the object becomes somewhat non-deterministic -- which function is actually called depends on which class is used to fetch the toolTip() rather than on the vtable entries (virual function references). QGraphicsView always talks to QGraphicsItems, so your QGraphicsView will always return the tool tip set using QGraphicsItem::setToolTip() function, rather than the one on your class.

    Long story short: Your tool tip will not show up unless you use the superclass function to set your tool tip.
    Last edited by franz; 26th November 2010 at 21:50. Reason: reformatted to look better
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    Thank you Franz. I did use the work "override" -- but I didn't mean that in the sense of C++ virtual methods. I'm not attempting to redefine QGraphicsItem::setToolTip() in any of my classes.

    I just want the child QGraphicsItem's "setToolTip" call to PREVAIL over the containing (parent's) "setToolTip" call when the mouse pointer is over the child QGraphicsItem.

    I did also make sure that the child QGraphicsItem had a higher "Z" value than the parent QGraphicsItem -- that didn't help. Only the tooltip text set by the parent's setToolTip call ever shows up.

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

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    Please provide a minimal compilable example reproducing the problem.
    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.


  5. #5
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    Well, QToolTips ARE working as desired in a simple example of nested QGraphicsItems. (See also screenshot of this example, below). I don't yet know why this isn't working in the context of our large application. THIS DOES WORK:

    Qt Code:
    1. // File: testGraphicItems.cpp
    2.  
    3. #include <QApplication>
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6. #include <QGraphicsRectItem>
    7. #include <QGraphicsTextItem>
    8. #include <ostream>
    9.  
    10. int main(int argc, char** argv)
    11. {
    12. QApplication app (argc, argv);
    13. QGraphicsScene myScene;
    14.  
    15. // *******************************************
    16. // *** Create Outer Parent QGraphicsItem ***
    17. // *******************************************
    18.  
    19. QGraphicsRectItem* outerParentItem =
    20. new QGraphicsRectItem (-180.0, -70.0, 360.0, 140.0);
    21.  
    22. outerParentItem->setToolTip ("Parent Item ToolTip");
    23. outerParentItem->setFlags (QGraphicsItem::ItemIsMovable |
    24. QGraphicsItem::ItemIsSelectable);
    25.  
    26. myScene.addItem (outerParentItem);
    27.  
    28. // *******************************************
    29. // *** Create Inner Parent QGraphicsItem ***
    30. // *******************************************
    31.  
    32. QGraphicsRectItem* innerParentItem =
    33. new QGraphicsRectItem (-140.0, -45.0, 280.0, 90.0, outerParentItem);
    34.  
    35. // ************************************
    36. // *** Create Child QGraphicsItem ***
    37. // ************************************
    38.  
    39. QGraphicsTextItem* childItem =
    40. new QGraphicsTextItem ("Child QGraphicsTextItem", innerParentItem);
    41.  
    42. const QRectF childRect = childItem->boundingRect();
    43. childItem->setPos (-0.5 * childRect.width(), -0.5 * childRect.height());
    44.  
    45. childItem->setToolTip ("Child Item ToolTip");
    46.  
    47. // ******************************
    48. // *** Create QGraphicsView ***
    49. // ******************************
    50.  
    51. QGraphicsView myView (&myScene);
    52. myView.setCaption ("Test Nested QGraphicsItems ToolTips");
    53. myView.setAttribute (Qt::WA_AlwaysShowToolTips);
    54. myView.resize (420, 200);
    55. myView.show();
    56.  
    57. return app.exec();
    58. }
    59.  
    60. //--- (end testGraphicItems.cpp) ---
    To copy to clipboard, switch view to plain text mode 

    Here is a screenshot -- without showing the QToolTip. (That's a little tricky to capture using our X11 setup):



    BaseOK.gif

    Thank you.
    Last edited by philw; 29th November 2010 at 19:36.

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

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    An example of working code is very unlikely to help us see the problem in the code that doesn't work.
    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.


  7. The following user says thank you to wysota for this useful post:

    philw (29th November 2010)

  8. #7
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    Quote Originally Posted by wysota View Post
    An example of working code is very unlikely to help us see the problem in the code that doesn't work.
    Yes, of course. I just wanted to share this confirmation that Qt 4.6.3 IS working as I had hoped, in this particular respect. I'll take it from here. Thank you.

  9. #8
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip

    Good to see you've solved the issue.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  10. The following user says thank you to franz for this useful post:

    philw (29th November 2010)

Similar Threads

  1. QGraphicsItem::setToolTip does not work
    By Nadia in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2009, 20:54
  2. Qgraphicsitem parent/child paint problem.
    By repka3 in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 23:03
  3. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 16:37
  4. Replies: 9
    Last Post: 22nd June 2008, 23:26
  5. QGraphicsItem tooltip behavior
    By darksaga in forum Qt Programming
    Replies: 6
    Last Post: 22nd August 2007, 18:50

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.