I'm creating a schematics edit where, simply put, the user can draw lines and rectangles. For this i am using a subclassed QGraphicsView with reimplemented event handlers. Now, when drawing lines, the view shifts in order to put the centerpoint of all drawn lines together in the middle of the application window (i guess?). For this is very annoying in a drawing program, how can i fix this?
MWE:
#include <QApplication>
#include <QMainWindow>
#include <QGraphicsView>
#include <QMouseEvent>
class view : public QGraphicsView
{
public:
view(QGraphicsScene* scene, QWidget* parent = 0) : QGraphicsView::QGraphicsView(scene, parent) { }
void mousePressEvent(QMouseEvent* event)
{
static QPointF p;
static bool active = false;
if(!active)
{
p = mapToScene(event->pos());
active = true;
}
else
{
QPointF p2 = mapToScene(event->pos());
active = false;
draw_line(p, p2);
}
}
void draw_line(QPointF p1, QPointF p2)
{
QPen pen;
pen.setWidth(2);
this->scene()->addLine(p1.x(), p1.y(), p2.x(), p2.y(), pen);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QGraphicsScene* scene = new QGraphicsScene;
view* mview = new view(scene);
w.setCentralWidget(mview);
w.show();
return a.exec();
}