Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Graphics view scene resize and origin point



  • In my graphics view i have a line from the point (0,0) to the point (100,100) but i was expecting that the line would go from the top left corner of my graphics view to somewhere to the right bottom but instead it shows up like the view had some margins and when i resize the items don't change their size and position, so how i can make them resize and reposition according to the window and how i make the top left corner of my graphics view have the coordinates 0,0?


  • Lifetime Qt Champion

    Hi,

    The graphics view coordinate system is explained here.



  • Now it became more clear but i still have one problem, i think the rect of my graphics view isn't accurate in the dialog constructor.
    I have this function that gets called in my dialog constructor and in the resizeEvent:

    void GameEditor::resizeGraphicsViewScene() {
    QRect crect = ui.graphicsView->contentsRect();
    ui.graphicsView->setSceneRect(0, 0, crect.width(), crect.height());
    QRectF r = ui.graphicsView->sceneRect();
    qDebug() << r;
    line->setLine(QLineF(0, 0, r.width(), r.height()));
    }

    When i run my application i don't get exactly my expected behavior which is the line going from the origin point to the bottom right corner of the graphics view widget.

    And when i maximize my window my resize function it works perfect

    And when i go back to the previous "resolution" it still works fine

    So why the graphics view content rect have some weird values in the start of my application?


  • Lifetime Qt Champion

    @mario1159 said in Graphics view scene resize and origin point:

    So why the graphics view content rect have some weird values in the start of my application?

    I think because you're doing this in the constructor. In the constructor the object is being constructed, so you can't trust that contentsRect() will return anything meaningful.



  • @mario1159

    Try to set up your GraphicsView inside the showEvent of your Widget / window.
    It worked for me some time ago (I tried to set a centered label over the GraphicsView, but 50% of height was always returning wrong values until I used the showEvent)
    As @jsulm already wrote, some values of your GraphicsView may be uninitialized / not set correcty while constructing

    QRectF r = ui.graphicsView->sceneRect(); // This rect could be wrong rect...
    qDebug() << r;
    line->setLine(QLineF(0, 0, r.width(), r.height())); // ...so this wont work as expected
    


  • Thanks that worked


Log in to reply