PDA

View Full Version : How to display mouse coordinates in QGraphicsView



edujar
29th May 2011, 06:44
Hello,

How can I display the mouse coordinate position (XY) on my derived QGraphicsView class.

I get the coordinates on mouseMoveEvent and even I can make a string with them, but I do not how to display the string and get it changed when the mouse moves.

Thanks
ejar

Santosh Reddy
29th May 2011, 08:36
QGraphicsView is used to view the items which are in a QGraphicsScene. If you need to see the cursor position updating in the view, you need to something like this.

Add a QGraphicsTextItem in your custom QGraphicsScene, and then update the the text of the item in QGraphicsScene::mouseMoveEvent()



MyGraphicsScene::MyGraphicsScene(QObject* parent)
: QGraphicsScene(parent)
{
cursor = new QGraphicsTextItem("0, 0", 0, this); //Fixed at 0, 0
}

void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMous eEvent* event)
{
QString string = QString("%1, %2")
.arg(event->scenePos().x())
.arg(event->scenePos().y()); //Update the cursor potion text

cursor->setPlainText(string);
}


I am not sure, if this what you are trying to do, looks like your are creating a custom QGraphicsView.

edujar
29th May 2011, 09:24
Hi Santosh and thanks for your fast response.

Yes, I think I am creating a custom QGraphicsView. I have got an intermediate solution to my question with the code you have sent, but the ideal would be to draw the text directly on my derived class from QGraphicsView.
Let´s say that I want to display the mouse XY position always on the right bottom corner of the QGraphicsView. Doing on that way, it doesn´t matter about any scroll or zooming factor. Otherway I have to recalculate the position where to display the coordinates for each change on the view.

My application is a GIS one, where I need to know the Latitude and Longitude of the cursor on the scene and display it at the right bottom corner of the view.

Thanks
ejar

Santosh Reddy
29th May 2011, 09:53
As far as I understand you can only add items to a scene not to view, so you need to get the current view's view port and then add the cursor there, this needs to be in your scene, something like this



void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMous eEvent* event)
{
// Get the list of the views attached to this scene
QList<QGraphicsView*> scene_views = views();

for(int i = 0; i < scene_views.count(); i++)
{
// get the visible view port
QWidget* viewport_widget = scene_views.at(i)->viewport();

// ensure it is visible
if(viewport_widget->isVisible())
{
// Update cursor position (text to be displayed)
QString string = QString("%1, %2")
.arg(event->scenePos().x())
.arg(event->scenePos().y());

cursor->setPlainText(string);

// Set the new position of the cursor
cursor->setPos(viewport_widget->width() - cursor->boundingRect().width()
,viewport_widget->height() - cursor->boundingRect().height());
}
}
}

edujar
29th May 2011, 18:49
Thanks again. The code above has the limitation of working with QGraphicsScene. Scene is fine becasue I have some graphics to display and it provides a very useful way, but it doesn´t for my mouse coordinates.
Going back to the coordinate, is there a way to use something like QPainter::drawText and use it on the view. Is should be able to call the function each time the mouse moves.

Ejar

WE-B-QT-N
3rd March 2016, 16:42
Hello I realize this is a old thread. Very interesting, I have questions on the cord system also...i would like to try this code but dont know quite how to do it. If you are still a member could you elaborate more?