One possibility is that this is result of way in which Qt only instantiates the tabs on demand. So when tab 1 is selected tab at startup, tab 2 doesn't yet exist. If tab 2 is selected at startup, I don't know if tab 1 already exists (because it was the default tab). IF it does then Qt has size info from both tabs when tab 2 is selection at startup, but it only has size info for tab 1 if tab 1 is selection at startup.
You could try instantiating all tabs, this other pyqt post QObject::findChild() returns None without obvious reason talks about related issue and fix.
Update:
I tested your code and confirmed that even in PyQt5 same behavior. The sizes just after construction are wrong for tabs that have not yet been selected. However, as soon as a tab is selected, it is layed out, and its size is then correct. To see this, comment out the line self.tabWidget.setCurrentIndex(0)
and change the 3 lines
print self.graphicsView.size()
print self.graphicsView_2.size()
print self.graphicsView_3.size()
to this (indentation is important: i moved the 3 print statements to a new printViewSizes
method, and added a tab-change observer):
self.tabWidget.currentChanged.connect(self.onTabChange)
self.printViewSizes()
and add the following two methods:
def printViewSizes(self):
print(self.graphicsView.size())
print(self.graphicsView_2.size())
print(self.graphicsView_3.size())
def onTabChange(self):
print('Tab changed')
self.printViewSizes()
Now after window visible, click tab 2, then 3. The output is:
PyQt5.QtCore.QSize(640, 480)
PyQt5.QtCore.QSize(640, 480)
PyQt5.QtCore.QSize(640, 480)
PyQt5.QtCore.QSize(622, 462)
PyQt5.QtCore.QSize(100, 30)
PyQt5.QtCore.QSize(100, 30)
Tab changed
PyQt5.QtCore.QSize(758, 518)
PyQt5.QtCore.QSize(758, 518)
PyQt5.QtCore.QSize(100, 30)
Tab changed
PyQt5.QtCore.QSize(758, 518)
PyQt5.QtCore.QSize(758, 518)
PyQt5.QtCore.QSize(758, 518)
showing that tab 2 size is correct as soon as visible, then tab 3 size correct as soon as it becomes visible. So if you are facing a layout issue in your tab, this is not the problem, it is likely in the resize handler of a widget in your graphics view.