2
votes

My QTabWidget has 2 tabs on launch.

  • Tab 1 is the one that's opened upon launch.
  • Tab 2 contains some widgets of which I take the QSize to later draw similar elements in newly opened tabs.

The problem is that currently the size of the elements is 640x480 (apparently the default value), if tab 2 was not shown yet. Once tab 2 is selected, the elements are "drawn" and get their sizes depending on resolutio, window size etc.
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? repaint and update do not do that.

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
Does tab 2 widget has a layout?Kao
I think you might want to call sizeHint() rather than size()Jeremy Friesner
@JeremyFriesner thanks, that worked, if you want to make that an answer I'll accept that one,Turtle10000

2 Answers

2
votes

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.

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.

1
votes

according to Document resizeEvent was never called when the widget is not visible and also 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.