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>
{
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
#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
To copy to clipboard, switch view to plain text mode
#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
) {
...
}
#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)
{
...
}
To copy to clipboard, switch view to plain text mode
#ifndef WHITESPACEVIEW_H
#define WHITESPACEVIEW_H
#include <QGraphicsView>
{
Q_OBJECT
public:
};
#endif // WHITESPACEVIEW_H
#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
To copy to clipboard, switch view to plain text mode
#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);
}
#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::BoundingRectViewportUpdate);
setBackgroundBrush(QColor(255, 255, 255));
setMouseTracking(true);
setCursor(Qt::CrossCursor);
}
To copy to clipboard, switch view to plain text mode
In mainwindow.cpp I have an event filter which wasn't modified:
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
And finally:
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks