how to position a tooltip in qt graphicsview - qt

I want to show a tooltip when dragging the endpoint of a line to a QGraphicsItem with the mouse pointer (no hovering).
So i have the mouse event and i want to call QToolTip::showText(QPoint pt, String str) and i want to show the tooltip at a normal position relative to the mouse pointer.
But whatever i try (widget.mapToGlobal, item.mapToScene) on event.pos() the tooltip appears realtive to the topleft of my screen or the graphicsview, not the mouse.
Maybe i am missing something simple, but i would really appreciate some help with this.
widget = item.scene().views()[0] #there is only one view
p = event.pos()
pt = widget.mapToGlobal(QtCore.QPoint(p.x(), p.y()))
QtGui.QToolTip.showText(pt, text)

QGraphicsSceneMouseEvent has another useful method - screenPos() which returns the mouse cursor position in scene coordinates. Try to use it. For example:
QtGui.QToolTip.showText(event.screenPos(), text)

Related

How do I find the position in the document that is visible in a QTextBrowser?

When the user scrolls in a QTextBrowser in my application, I want to retrieve the position in the document that they've scrolled to (offset in the document, not the GUI position.)
If I can make the cursor jump to that location, I can get QTextCursor.position(). But I don't see a way to make the cursor jump to the visible location in the browser. The cursor stays where it is when I scroll.
I do not completely understand the description of your problem, but maybe you can determine the cursor (i.e. the position in text document) of the beginning and the end of the visible area by calling https://doc.qt.io/qt-5/qtextedit.html#cursorForPosition
QRect rect = textBrowser->rect();
QTextCursor firstVisible = textBrowser->cursorForPosition(rect.topLeft());
QTextCursor lastVisible = textBrowser->cursorForPosition(rect.bottomRight());
I have not tested it, but I think you get the idea. Maybe you will need to use the rect of textBrowser->viewport() instead of the rect of textBrowser. You need to experiment a bit with this to find what works for you.
Based on V.K.'s answer, here is my solution in Python:
browserRect = self.mainText.rect()
newCursor = self.mainText.cursorForPosition(browserRect.topLeft())
self.mainText.setTextCursor(newCursor)
textPos = mainText.textCursor().position()
Actually, just using QPoint(0,0) probably would work just as well, since the upper left of the browser's rectangle is pretty close to (0,0).

What is the simplest way to get notified whenever the user scrolls a QScrollArea?

Just as a QPushButton provides a default clicked() signal, I expected QScrollArea to have a sliderChanged() or similar signal. Interestingly, the QScrollBar does have such a signal.
All I would like to do is to know what part of the huge widget inside the scroll area is visible, whenever the user scrolls it.
There are many solutions, none of which seem elegant to me:
subclass QScrollArea
subclass the widget inside the scroll area, and re-implement its paint event.
create a custom veiwport, using QScrollBar
periodically poll the position of the widget inside the scroll area. This seems to be the worst solution.
Is there a way without subclassing?
There is QAbstractSlider::valueChanged() signal that is emitted when the slider value has changed, with the new slider value as argument. This will notify you as soon as you scroll your view.
WRT the second problem, neither of mentioned points necessary. You need to:
1) Get the position of inner widget (if any) related to the scroll area:
QPoint p = scrollArea->widget()->pos();
It use to be a negative coordinates if you scrolled your view down/right or null without scrolling.
2) Get the size of the visible area
QSize s = scrollArea->viewport()->size();
With these two values you can construct a QRect that will represent the visible area of your inner widget.

Mouse position and a QDialog

I am trying to figure out if there is a way to make a qdialog to input the co-ordinates of a line such that the co-ordinates can be given by mouse position or a qdialog (via QLineEdit items). Is there a way to have the mouse position be displayed in the QLinedEdit items in the qdialog when moving the mouse of the canvas or move the line points when typing in the dialog?
Thanks.

QGraphicsItem - Follow the mouse cursor

I'm stuck on how to approach this. I have a QGraphicsItem within a scene, and I'm passing a hover event from the scene to this child. While the move event occurs (I'm just using mouseMoveEvent with mouse tracking on), I want another QGraphicsItem to follow the cursor.
I don't need any collision detection, drag and drop, etc. Just an item that follows the cursor. The only two ways I can think of are...
While the mouse moves, draw a new QGraphicsItem at the mouse position. I would need to clear the scene, redraw everything, and draw the new position on top.
Somehow use the animation framework and whenever the mouse moves, animate the QGraphicsItem to move to the new mouse position in 1 millisecond.
I'm probably either overthinking this or unaware of another way to do this... any suggestions?
I did it like this
Create the GraphicsItem cursor which will be moved with mouse-cursor, and store its pointer somewhere (in scene subclass for instance. I have a toolset, so for me it's in one of these tools)
Set its Z-Value (QGraphicsItem::setZValue) so that the cursor will be painted above all other items in your scene
Track QGraphicsScene::mouseMoveEvent events, forward these events down to the cursor pointer, and update item's position
that's it.
I guess it corresponds to your solution 1, except you don't have to clear the scene thanks to z-value feature.

QScrollArea's elements aren't redrawn

I'm looking for a way to fix a problem I have with some widgets disappearing while I zoom in.
The structure of my program is: Window with a QscrollArea as a child. The QscrollArea has a class that inherits from QWidget as a child and this class has a vector of a drawclass that inherits from qwidget. I resize the class in the scrollArea to zoom in or out and get the scrollbars while updating the coordinates in the drawclass.
The problem I have is that when I zoom in and then out, some of the instances of the drawclass aren't redrawn (I get the background instead of the square I should have) and doesn't respond to the enterevent. Scrolling in the direction of the square does fix the problem so I think I'm not correctly updating the qScrollArea with
nativeParentWidget()->update(); when the zoom slot is called.
The output should be:
normal http://img26.imageshack.us/img26/492/38361041.jpg
after zooming and dezooming I get:
problem http://img214.imageshack.us/img214/2642/78940605.jpg
Thanks for your help.
Not sure, but try to update QAbstractScrollArea::viewport().

Resources