0
votes

I have such program: in my Widget I have qpushbuttons, qslider and qgraphicsview. I place qgraphicsscene in qgraphicsview. The problem is that when I click in QGraphicsscene and want to draw a Point, that was clicked, there is a displacement. When I click in center of QGraphicsScene there is not any problems.

if (mouseEvent->button() == Qt::LeftButton)
{
    QPointF pos = mouseEvent->scenePos();
    addEllipse(pos.x(), pos.y(), 5, 5, QPen(QColor(Qt::green)), QBrush(QColor(Qt::red)));
}

There is a screen too. So, the question is how to avoid this moving of scene.Screen

In the screen I have clicked in right bottom, but point was drawn in the center

2

2 Answers

1
votes

Try this. In my computer it works without displacement

.*h

#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H

#include <QGraphicsScene>
#include <QMouseEvent>
class GraphicsScene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit GraphicsScene(QObject *parent = 0);

signals:

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
public slots:

};

#endif // GRAPHICSSCENE_H

*.cpp

#include "graphicsscene.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>

GraphicsScene::GraphicsScene(QObject *parent) :
    QGraphicsScene(parent)
{
}

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    qDebug() << "in";
    if (mouseEvent->button() == Qt::LeftButton)
    {
        QPointF pos = mouseEvent->scenePos();
        addEllipse(pos.x(), pos.y(), 5, 5, QPen(QColor(Qt::green)), QBrush(QColor(Qt::red)));

    }
}

Using

GraphicsScene *scene = new GraphicsScene(this);
ui->graphicsView->setSceneRect(50,50,50,50);//for example
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
0
votes

view->setSceneRect(0, 0, 640, 480); has helped me