获取 QGraphicsView 的大小

发布于 2024-10-18 22:03:09 字数 189 浏览 6 评论 0原文

我想知道某个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

李不 2024-10-25 22:03:09

我的 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.

你的呼吸 2024-10-25 22:03:09

如果你想知道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();

骄兵必败 2024-10-25 22:03:09

在MainWindow的构造函数中检索宽度/高度

这就是问题所在!该小部件尚未绘制,您正在询问它的大小。使用 eventshowEventpaintEvent 等其他事件在小部件的初始化过程中获取正确的大小。

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.

小…楫夜泊 2024-10-25 22:03:09

回答:
调用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.

淡淡绿茶香 2024-10-25 22:03:09

我和你有同样的问题。 QGraphicsView.size() 始终返回(100,30)。我已经在我的项目中解决了这个问题。

检查下面的代码是这样的,

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

就像 Bigbell Mercy 回答的那样,您显示确保 QGraphicsView 显示!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文