[Qt-interest] [Solved] Screen coordinates inQGraphicsItem::hoverEnterEvent() ?
bd.anon at free.fr
bd.anon at free.fr
Sat Jun 4 14:03:12 CEST 2011
On Mon, 30 May 2011 14:11:13 +1000, "Ross Bencina"
<rossb-lists at audiomulch.com> said :
>The solution I use only works when there is a single view.
Ross, based on your suggestion, this seems to work for me in a
multi-view environment :
void myQGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent*
pEvent)
{
QString text("tooltip text");
// We need a way to turn QGraphicsItem's bounding rectangle into
screen coordinates, even though
// we do not know which of several potential views generated the
QGraphicsSceneHoverEvent
// 1) Determine which view generated the event
QWidget* pViewport = QApplication::widgetAt(pEvent->screenPos());
QWidget* pView = pViewport->parentWidget();
// 2) As this is a hack, check that we indeed got a QGraphicsView
QGraphicsView* pGraphicsView =
qobject_cast<QGraphicsView*>(pView);
// If not, show transient tooltip
if (pGraphicsView == 0)
{
QToolTip::showText(pEvent->screenPos(), text,
pEvent->widget());
return;
}
// 3) Here we indeed have a QGraphicsView
// Use it to map the QGraphicsItem's bounding rectangle into
screen coordinates and enjoy a semi permanent tooltip
QRectF itemRect = boundingRect();
QRect screenRect(
pGraphicsView->mapToGlobal(pGraphicsView->mapFromScene(mapToScene(itemRect.topLeft()))),
pGraphicsView->mapToGlobal(pGraphicsView->mapFromScene(mapToScene(itemRect.bottomRight())))
);
QToolTip::showText(pEvent->screenPos(), text, pEvent->widget(),
screenRect);
}
Bertrand Anon
More information about the Qt-interest-old
mailing list