获取 QGraphicsView 的大小
我想知道某个QGraphicsView
的大小。它的大小不固定,因为该小部件是网格布局的一部分。我尝试使用 this->ui->myGraphicsView->width()
及其等效高度,但这些值不准确。
如何获取QGraphicsView
的当前大小?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的 QGraphicsView 的大小也不断收到 100x30 。原来我在显示 QGraphicsView 之前询问了它的大小。
将初始化代码移至 showEvent 后,我得到了正确的尺寸。
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.
如果你想知道QGraphicsView的实际大小,QGraphicsView::size();
如果你只想知道 QGraphicsView 的内容大小, QGraphisView::viewport().size();
If you wanna know the actual size of QGraphicsView, QGraphicsView::size();
If you wanna konw only the content size of QGraphicsView, QGraphisView::viewport().size();
这就是问题所在!该小部件尚未绘制,您正在询问它的大小。使用
event
、showEvent
、paintEvent
等其他事件在小部件的初始化过程中获取正确的大小。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.回答:
调用MainWindow::show()后,然后获取大小。
描述:
我和彼得有同样的问题。在像 MainWindow::MainWindow() 这样的小部件构造函数中,您无法获得像网格布局中的 QGraphicsView 这样的小部件的正确大小,因为在该构造函数中,小部件的大小和位置尚未确定。因此,在 MainWindow::MainWindow() 中,您必须调用 show() ,然后获取视图或其他小部件的大小。
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.
我和你有同样的问题。
QGraphicsView.size()
始终返回(100,30)
。我已经在我的项目中解决了这个问题。检查下面的代码是这样的,
就像 Bigbell Mercy 回答的那样,您显示确保
QGraphicsView
显示!I have the same problem with you.
QGraphicsView.size()
is alwaysreturn (100,30)
. I have solved this problem in my project.Check below code is like this
so like Bigbell Mercy answered , you show make sure
QGraphicsView
is show!