The easy way to create the long cross line cursor (as long as viewport) is create a cross line graphicsItem, when mouse moved, set the item’s pos property.
But this way will be very slow when the scene is complex, because it should update the whole viewport to update the cursor’s pos.
The another easy way is setCursor(QCursor(..)),use a QPixmap to define the long cross line, this way will very fast , but the cursor will exceed the viewport rect.
Is there another way to show a long cross line cursor fastly?
Thanks very much!
If I understand correctly, you want to draw an horizontal line and a vertical line, crossing at the cursor position, and being as large as the viewport is.
A poosible solution would be to reimplement QGraphicsScene::drawForeground() to draw the two lines with the painter.
The problem is that the scene doesn’t know about the mouse position. This means the view will have to track it and inform the scene when the mouse position has changed.
To do that, you’ll have to create your own
GraphicsScene(inheritingQGraphicsScene) and your ownGraphicsView(inheritingQGraphicsView).On your
GraphicsViewconstructor, you’ll have to start mouse tracking. This will make your you receive amouseMoveEventeach time the mouse moves inside the view :As you can see in the code snippet above, the view is emitting a signal (
mousePosChanged) to which the scene will be connected. This signal contains the mouse position, converted to the scene’s coordinates.Now, on the scene side, you have to add a slot which will be called when the mouse position changed, store the new mouse position in a member variable and reimplement QGraphicsScene::drawForeground() :
The last thing to do is connect the GraphicsView’s signal to the GraphicsScene slot.
I’ll let you check if this solution is acceptable performance wise.