QGraphicsView - show mouse cursor position in status bar
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:
Code:
#ifndef WHITESPACESCENE_H
#define WHITESPACESCENE_H
#include <QGraphicsScene>
{
Q_OBJECT
public:
WhiteSpaceScene
(qreal x, qreal y, qreal width, qreal height,
QObject * parent
= 0);
protected:
private:
void printPoint
(const QPoint &clickedPoint
);
};
#endif // WHITESPACESCENE_H
Code:
#include "whitespacescene.h"
WhiteSpaceScene
::WhiteSpaceScene (qreal x, qreal y, qreal width, qreal height,
QObject * parent
) :{
}
void WhiteSpaceScene
::mousePressEvent(QMouseEvent *event
) {
if (event->button() == Qt::LeftButton)
{
printPoint(event->pos());
}
}
void WhiteSpaceScene
::printPoint(const QPoint &clickedPoint
) {
...
}
Code:
#ifndef WHITESPACEVIEW_H
#define WHITESPACEVIEW_H
#include <QGraphicsView>
{
Q_OBJECT
public:
};
#endif // WHITESPACEVIEW_H
Code:
#include "whitespaceview.h"
{
setAttribute(Qt::WA_StaticContents); // won't change contents on widget resizing
setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
setBackgroundBrush
(QColor(255,
255,
255));
setMouseTracking(true);
setCursor(Qt::CrossCursor);
}
In mainwindow.cpp I have an event filter which wasn't modified:
Code:
{
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:
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "whitespacescene.h"
#include "whitespaceview.h"
int main(int argc, char *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.
Re: QGraphicsView - show mouse cursor position in status bar
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.
Re: QGraphicsView - show mouse cursor position in status bar
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).
Re: QGraphicsView - show mouse cursor position in status bar
Try the following in main.cpp:
Code:
whitespaceview->viewport()->installEventFilter(window);
Re: QGraphicsView - show mouse cursor position in status bar
You're the man, worked flawlessly. Could you explain what's the deal with it?
Re: QGraphicsView - show mouse cursor position in status bar
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:
Code:
qDebug() << window->centralWidget()->children();
qDebug() << whitespaceview->viewport();
With the above in mind, reread the QGraphicsView docs (here) and things should make sense.