Getting the size of a QGraphicsView
Asked Answered
A

5

8

I want to know the size of a certain QGraphicsView. Its size isn't fixed because the widget is part of a grid layout. I tried using this->ui->myGraphicsView->width() and its height equivalent but these values aren't accurate.

How can I get the current size of a QGraphicsView?

Artima answered 24/2, 2011 at 10:52 Comment(1)
May you want to know size of QGraphicsView->viewport() ?Harshman
L
8

Constantly received 100x30 as the size of my QGraphicsView as well. It turned out I was asking for the size of the QGraphicsView before it was shown.

After moving my initialization code to showEvent, I got the correct dimensions.

Lay answered 7/4, 2011 at 11:53 Comment(0)
R
7

If you wanna know the actual size of QGraphicsView, QGraphicsView::size(); If you wanna konw only the content size of QGraphicsView, QGraphisView::viewport().size();

Rosalba answered 25/2, 2011 at 3:5 Comment(4)
QGraphicsView::size() (width 100, height 30) and QGraphics::viewport() (width 94, height 24) both seem to report an incorrect size. I'm trying to retrieve the width/height in MainWindow's constructor - does that pose an issue? ui->setupUi(this); is called before I try to retrieve the size.Artima
Try this => ui->graphicsView->geometry().width()Rosalba
I'm still getting width 100, height 30.Artima
I tried the following functions: size() viewport() geometry() sizeHint() sizeIncrement() sceneRect() baseSize() sizeHint() frameSize() rect() normalGeometry() QAbstractScrollArea *area = qobject_cast<QAbstractScrollArea*>(ui->graphicsView); area->size() But the results still don't seem right.Rosalba
M
3

retrieve the width/height in MainWindow's constructor

That is the problem! The widget isn't painted already and you're asking for it's size. Use other events like event, showEvent, paintEvent to get the right size within the initialization process of a widget.

Martsen answered 16/2, 2015 at 20:42 Comment(0)
S
0

Answer: After calling MainWindow::show(), then get the size.

Description: I had the same problem as Pieter. In the widget constructor like MainWindow::MainWindow() you can't get the correct size of the widget like QGraphicsView in Grid Layout because in that constructor the widget's size and location are not determined. Therefore, in MainWindow::MainWindow() you have to call show() and then get the size of the view or other widget.

Sacculate answered 13/1, 2017 at 14:34 Comment(0)
B
0

I have the same problem with you. QGraphicsView.size() is always return (100,30). I have solved this problem in my project.

Check below code is like this

...in a QTabWidget class...
def addANewTab(self):
  view = QGraphicsView()
  ...set view param...
  index = self.addTab(view, 'test')
  view.size() # it will return (100,30)
  self.setCurrentIndex(index)
  view.size() # it will return correct size

so like Bigbell Mercy answered , you show make sure QGraphicsView is show!

Bucharest answered 6/11, 2019 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.