Transformation of co-ordinates on the graphics scene



  • I have QGraphicsView as central widget of QMainWindow
    I am binding scene and the view as follows,

    /* Constructor of mainWindow */
    scene = new Canvas(&strContextMenu, this);
    ui->graphicsView->setScene(scene);

    /* Setting the view as the central widget */
    setCentralWidget(ui->graphicsView);

    /* Setting alignment of view to the top left */
    ui->graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    Problem Statement:
    When I paint a graphics Item on the scene(Position is the click of the mice), only the first Item is drawn on the top left of the view. Subsequent graphic Items of the same type do not experience this and are drawn where intended(position of the click of the mice)

    Requirement is to draw the items wherever mice is clicked which is achieved by using scene->setSceneRect(QRectF(0, 0, 1000, 700)) which
    I don't want to do as I want the size of the scene to be hardcoded.

    I have tried using the following but it doesn't seem to work.
    ui->graphicsView->setTransformationAnchor(QGraphicsView::NoAnchor);
    ui->graphicsView->resetTransform();

    I believe it is some transform issue between the view and the scene but I am not getting it.

    Please help!


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what OS ?
    Can you show the current code you are using ?



  • Hi, I am on Qt 5.9. Operating System MacOS Mojave. The code I shared above is the constructor of the mainWindow and where I see the problem.

    Is there any specific snippet you'd like to see ?

    Thanks.


  • Lifetime Qt Champion

    What would be nice is to have a minimal compilable example that shows that behaviour.