PDA

View Full Version : Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip



philw
25th November 2010, 20:35
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

franz
26th November 2010, 20:45
QGraphicsItem::setToolTip() isn't a virtual function (http://en.wikipedia.org/wiki/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.

philw
27th November 2010, 18:37
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.

wysota
27th November 2010, 18:51
Please provide a minimal compilable example reproducing the problem.

philw
29th November 2010, 18:32
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:


// File: testGraphicItems.cpp

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <ostream>

int main(int argc, char** argv)
{
QApplication app (argc, argv);
QGraphicsScene myScene;

// *******************************************
// *** Create Outer Parent QGraphicsItem ***
// *******************************************

QGraphicsRectItem* outerParentItem =
new QGraphicsRectItem (-180.0, -70.0, 360.0, 140.0);

outerParentItem->setToolTip ("Parent Item ToolTip");
outerParentItem->setFlags (QGraphicsItem::ItemIsMovable |
QGraphicsItem::ItemIsSelectable);

myScene.addItem (outerParentItem);

// *******************************************
// *** Create Inner Parent QGraphicsItem ***
// *******************************************

QGraphicsRectItem* innerParentItem =
new QGraphicsRectItem (-140.0, -45.0, 280.0, 90.0, outerParentItem);

// ************************************
// *** Create Child QGraphicsItem ***
// ************************************

QGraphicsTextItem* childItem =
new QGraphicsTextItem ("Child QGraphicsTextItem", innerParentItem);

const QRectF childRect = childItem->boundingRect();
childItem->setPos (-0.5 * childRect.width(), -0.5 * childRect.height());

childItem->setToolTip ("Child Item ToolTip");

// ******************************
// *** Create QGraphicsView ***
// ******************************

QGraphicsView myView (&myScene);
myView.setCaption ("Test Nested QGraphicsItems ToolTips");
myView.setAttribute (Qt::WA_AlwaysShowToolTips);
myView.resize (420, 200);
myView.show();

return app.exec();
}

//--- (end testGraphicItems.cpp) ---

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

https://cadswes2.colorado.edu/~philw/2010/Qt4AcctView/ToolTipProb/BaseOK.gif

5542

Thank you.

wysota
29th November 2010, 18:40
An example of working code is very unlikely to help us see the problem in the code that doesn't work.

philw
29th November 2010, 18:48
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.

franz
29th November 2010, 19:20
Good to see you've solved the issue.