Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2007
points are moving in graphics view


Message 1 in thread

Hi all, Greetings. I'm new on Qt and I want to use the GraphicsView to
display points created by the user, clicking with the mouse. For that, I
specialize
the mousePressEvent() (derived from QGraphicsView) to create a QPoint in
the position event->pos() and update the view. But when a point is created,
the scene is moved to centre the points. This is not a good behaviour for
my application. The points have to be under the mouse pointer. How can
achieve this?
Thank you in advance.
Here is the relevant code (I'm using Qt4.2.2 on a Linux system):

#include "myview.h"

myView::myView(QGraphicsScene *scene, QWidget *parent = 0) :
QGraphicsView(scene, parent) {
        myscene = scene;
        setBackgroundBrush(Qt::CrossPattern);
}

void myView::mousePressEvent(QMouseEvent *event) {
        if(event->button() == Qt::LeftButton) {
                QTextStream out(stdout);
                out << "(" << event->pos().x() << " | " << event->pos().y()
<< ")\n";
                lastPos = mapToScene(event->pos());
                myscene->addEllipse(QRectF(lastPos, QSizeF(1, 1)),
Qt::SolidLine,
Qt::NoBrush);
                out << "(" << lastPos.x() << " | " << lastPos.y() << ")\n";
               
update(QRect(event->pos()-QPoint(2,2),event->pos()+QPoint(2,2)));
        }
}

-- 
 [ signature omitted ] 

Message 2 in thread

Luiz Henirique Pereira wrote:
> Hi all, Greetings. I'm new on Qt and I want to use the GraphicsView to
> display points created by the user, clicking with the mouse. For that, I
> specialize
> the mousePressEvent() (derived from QGraphicsView) to create a QPoint in
> the position event->pos() and update the view. But when a point is
> created, the scene is moved to centre the points. This is not a good
> behaviour for my application. The points have to be under the mouse
> pointer. How can achieve this?

Have you set QGraphicsScene::sceneRect? You might be seeing the scene
getting auto-centered as the scene contents' bounding rect is growing.

Andreas

-- 
 [ signature omitted ]