Getting the size of a QGraphicsView

谁都会走 提交于 2019-12-10 20:29:38

问题


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?


回答1:


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.




回答2:


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




回答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.




回答4:


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.




回答5:


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!



来源:https://stackoverflow.com/questions/5103613/getting-the-size-of-a-qgraphicsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!