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
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.
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.
Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip
Please provide a minimal compilable example reproducing the problem.
1 Attachment(s)
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:
Code:
// File: testGraphicItems.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <ostream>
int main(int argc, char** argv)
{
// *******************************************
// *** Create Outer Parent QGraphicsItem ***
// *******************************************
outerParentItem->setToolTip ("Parent Item ToolTip");
myScene.addItem (outerParentItem);
// *******************************************
// *** Create Inner Parent QGraphicsItem ***
// *******************************************
// ************************************
// *** Create Child QGraphicsItem ***
// ************************************
const QRectF childRect
= childItem
->boundingRect
();
childItem->setPos (-0.5 * childRect.width(), -0.5 * childRect.height());
childItem->setToolTip ("Child Item ToolTip");
// ******************************
// *** Create QGraphicsView ***
// ******************************
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...rob/BaseOK.gif
Attachment 5542
Thank you.
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.
Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip
Quote:
Originally Posted by
wysota
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.
Re: Child QGraphicsItem setToolTip does not override parent QGraphicsItem's tooltip
Good to see you've solved the issue.