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?
-
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?
-
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?
@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.
-
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?
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 theshowEvent
)
As @jsulm already wrote, some values of your GraphicsView may be uninitialized / not set correcty while constructingQRectF 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