简体   繁体   English

如何在未选择的选项卡中的 QTabWidget 中绘制 Qt 小部件?

[英]How to draw Qt widgets in a QTabWidget in non-selected tabs?

My QTabWidget has 2 tabs on launch.我的QTabWidget在启动时有 2 个选项卡。

  • Tab 1 is the one that's opened upon launch.选项卡 1 是启动时打开的选项卡。
  • Tab 2 contains some widgets of which I take the QSize to later draw similar elements in newly opened tabs.选项卡 2 包含一些小部件,我使用QSize稍后在新打开的选项卡中绘制类似的元素。

The problem is that currently the size of the elements is 640x480 (apparently the default value), if tab 2 was not shown yet.问题是当前元素的大小是 640x480(显然是默认值),如果选项卡 2 尚未显示。 Once tab 2 is selected, the elements are "drawn" and get their sizes depending on resolutio, window size etc.选择选项卡 2 后,将“绘制”元素并根据分辨率、window 大小等获取它们的大小。
How can I force the drawing of the elements in the background without opening tab 2, so that I can obtain the sizes the elements would have if the tab was opened?如何在不打开选项卡 2 的情况下强制在后台绘制元素,以便获得打开选项卡时元素的大小? repaint and update do not do that. repaintupdate不这样做。

My current workaround is to launch with tab 2 open and switch to tab 1 in main.cpp between myWindow.show() and myApp.exec() but that seems a bit dirty.我目前的解决方法是在打开选项卡 2 的情况下启动并在myWindow.show()myApp.exec()之间切换到main.cpp中的选项卡 1,但这似乎有点脏。

For performance reasons, a QWidget's size() method is not guaranteed to return its expected value until after the QWidget has actually been laid out and shown.出于性能原因,QWidget 的size()方法不能保证在 QWidget 实际布局和显示之前返回其预期值。

If you need to know in advance what a QWidget's size would be , you can call its sizeHint() method instead, and that will force the calculation to be performed right away, so that the correct size can be returned even if it hasn't already been computed.如果您需要提前知道 QWidget 的大小,您可以调用它的sizeHint()方法,这将强制立即执行计算,以便即使没有返回正确的大小已经计算过了。

according to Document resizeEvent was never called when the widget is not visible and also paintEvent.根据文档,当小部件不可见时,resizeEvent 永远不会被调用,并且也是paintEvent。 this is a normal routine for decrease processing effort and reduce the rendering process.这是减少处理工作和减少渲染过程的正常例程。 if you want to know when a tab1 resized, reimplement(override) resizeEvent and say to tab2 must be resized.如果您想知道何时调整 tab1 的大小,必须调整 reimplement(override) resizeEvent 并对 tab2 说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM