PDA

View Full Version : QGraphicsView - show mouse cursor position in status bar



mwgobetti
19th November 2011, 07:13
First, thank you all for your questions and your answers, I've been learning a lot just by searching and finding perfect information in these forums.

Now, my problem which I couldn't find the solution: I'm not successfully getting mouse cursor position in main window's status bar, based on its movement around a QGraphicsView. It was working perfect when I was using a QWidget instead of QGraphicsView, but I had to change it due to some project new requirements. Also, I should print to an external file the points that were clicked so far. That's not working anymore, too.

The current symptom is: the mouse current coordinates are shown in the status bar, but only in the extremes, i.e., x<=2, y<=2, x>=right-2 or y>=bottom-2. Odd, isn't it?

I've reimplemented QGraphicsScene and QGraphicsView this way:


#ifndef WHITESPACESCENE_H
#define WHITESPACESCENE_H

#include <QGraphicsScene>

class WhiteSpaceScene : public QGraphicsScene
{
Q_OBJECT
public:
WhiteSpaceScene (qreal x, qreal y, qreal width, qreal height, QObject * parent = 0);

protected:
void mousePressEvent(QMouseEvent *event);

private:
void printPoint(const QPoint &clickedPoint);
};

#endif // WHITESPACESCENE_H

#include "whitespacescene.h"

WhiteSpaceScene::WhiteSpaceScene (qreal x, qreal y, qreal width, qreal height, QObject * parent) :
QGraphicsScene(x, y, width, height, parent)
{

}

void WhiteSpaceScene::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
printPoint(event->pos());
}
}

void WhiteSpaceScene::printPoint(const QPoint &clickedPoint)
{
...
}


#ifndef WHITESPACEVIEW_H
#define WHITESPACEVIEW_H

#include <QGraphicsView>

class WhiteSpaceView : public QGraphicsView
{
Q_OBJECT
public:
explicit WhiteSpaceView(QGraphicsScene * scene, QWidget * parent = 0);
};

#endif // WHITESPACEVIEW_H


#include "whitespaceview.h"

WhiteSpaceView::WhiteSpaceView(QGraphicsScene * scene, QWidget * parent) :
QGraphicsView(scene, parent)
{
setAttribute(Qt::WA_StaticContents); // won't change contents on widget resizing
setRenderHint(QPainter::Antialiasing);
setViewportUpdateMode(QGraphicsView::BoundingRectV iewportUpdate);
setBackgroundBrush(QColor(255, 255, 255));
setMouseTracking(true);
setCursor(Qt::CrossCursor);
}
In mainwindow.cpp I have an event filter which wasn't modified:

bool MainWindow::eventFilter(QObject*, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseCursor = static_cast<QMouseEvent*>(event);
statusBar()->showMessage(QString("(x,y) coordinates: (%1,%2)").arg(mouseCursor->x()).arg(mouseCursor->y()));
}
else if (event->type() == QEvent::Leave)
{
statusBar()->showMessage("");
}
return false;
}
And finally:

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "whitespacescene.h"
#include "whitespaceview.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *window = new MainWindow;

WhiteSpaceScene *whitespace = new WhiteSpaceScene(0, 0, 350, 350);
WhiteSpaceView *whitespaceview = new WhiteSpaceView(whitespace);
whitespaceview->installEventFilter(window);
window->setCentralWidget(whitespaceview);
window->show();
return a.exec();
}

In the old main, I had a call to my subclassed QWidget (instead of two calls: QGraphicsScene and QGraphicsView) and the event filter was installed on it (subwidget->installEventFilter(window)).

I've also tried to declare using standard QGraphicsView and QGraphicsScene to see if my subclasses were the problem, but the exact same symptom occurs.

Any suggestions?
Thanks a lot in advance.

totem
19th November 2011, 16:32
this is weird.. i have the same behaviour with the code you provided.
I thought the event filter was called before the event handler (mouseMoveEvent in that case), but apparently this is not the case. If you overload this event handler, and ignore the event, then the event filter will receive what you want.

mwgobetti
19th November 2011, 17:48
Hi totem, thank you for reply.
I should probably ask this in the newbie forum, but to stay on topic, how would I do what you suggested? Could you please share some code?

Before I created the topic, I tried to overload mouseMoveEvent instead of using the filter, but then nothing happens at all (the same behaviour occured with QWidget, I had to use the event filter otherwise it didn't work at all).

norobro
19th November 2011, 18:12
Try the following in main.cpp:
whitespaceview->viewport()->installEventFilter(window);

mwgobetti
19th November 2011, 18:15
You're the man, worked flawlessly. Could you explain what's the deal with it?

norobro
19th November 2011, 19:34
The QGraphicsView::viewport() is the widget that is actually displayed as the centralWidget in your MainWindow not the QGraphicsView itself. You can check this by adding the following to main.cpp:
qDebug() << window->centralWidget()->children();
qDebug() << whitespaceview->viewport();


With the above in mind, reread the QGraphicsView docs (here (http://doc.qt.nokia.com/latest/qgraphicsview.html#details)) and things should make sense.