Search code examples
qtqgraphicsviewcoordinate-systemsqgraphicsscene

How to set (0,0) to the top left corner of QGraphicsScene


Is it possible to set (0,0) to the top-left corner of the QGraphicsScene? It seems to be at the center of the view by default:

class GraphicsScene : public QGraphicsScene
{
protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
    {
        // here you can see that (0,0) is at the center of the view
        qDebug() << "movement " << event->scenePos();
    }
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    GraphicsScene* scene = new GraphicsScene;
    QGraphicsView* view = new QGraphicsView(scene);

    setCentralWidget(view);
}

Solution

  • You have to set the alignment in the QGraphicsView:

    QGraphicsView* view = new QGraphicsView(scene);
    view->setAlignment(Qt::AlignTop | Qt::AlignLeft);