[Qt-interest] Screen coordinates inQGraphicsItem::hoverEnterEvent() ?
Ross Bencina
rossb-lists at audiomulch.com
Mon May 30 06:11:13 CEST 2011
Hello Bertrand
Sorry, coming in a bit late on this. Interesting problem though...
I agree, as far as I know there is no obvious, simple direct way to do this.
bd.anon at free.fr:
> QGraphicsItem certainly knows about the Scene, but this is not a
> widget. True, from the scene, you can access the widget-derived view,
> displaying this scene, but how do I know which view to select when
> there are several of them displaying the same scene ?
I realise it's a bit of a hack, but this should work:
QGraphicsSceneHoverEvent::screenPos() will give you the global screen
position.
QApplication::widgetAt() will give you the widget at that position. This
should be the viewport of your graphics scene view
You could combine this with a search of QGraphicsScene::views() to find the
correct view. Alternatively you could just search the views and see which
one contains QGraphicsSceneHoverEvent::screenPos(). However this might have
z-order problems whereas QApplication::widgetAt() will not.
Another option:
You can use QGraphicsSceneHoverEvent::screenPos() with
QDesktopWidget::screenGeometry() and QDesktopWidget::screen() to find a
screen widget.
> I am certain there has to be a simple solution, but it escapes me. If
> you already solved this problem, a cut and paste of the relevant part
> of your QGraphicsItem::hoverEnterEvent() would be very helpful.
The solution I use only works when there is a single view. On top of that I
have my own ToolTip implementation that takes a global rect to showText()
but you get the idea...
QGraphicsView *view = patcherScene()->views().first();
QRectF anchorRect( toolTipAnchorRect() ); // rect of my graphics item i want
to display a tooltip for
QRect screenRect(
view->mapToGlobal( view->mapFromScene( mapToScene(
anchorRect.topLeft() ) ) ),
view->mapToGlobal( view->mapFromScene( mapToScene(
anchorRect.bottomRight() ) + QPointF( 1.5, 1.5 ) ) ) );
AMToolTip::showText( toolTipText, 0, view, screenRect.adjusted( 0, -1, 0,
2 ),
(direction() == INPUT_PORT) ? AMToolTip::AboveAlignedToLeft :
AMToolTip::BelowAlignedToLeft );
Ross.
More information about the Qt-interest-old
mailing list