I have a QDialog containing a QTabWidget, whose tabs vary in size. Following these instructions, which are similar to another stack overflow answer, I have the following code (self.tabs is a dictionary of the tab widgets):
class Panel(NXDialog):
def __init__(self, panel, parent=None):
super(Panel, self).__init__(parent)
self.tabwidget = QtWidgets.QTabWidget()
self.tabwidget.currentChanged.connect(self.update)
...
@property
def tab(self):
return self.tabwidget.currentWidget()
def update(self):
if self.tabwidget.count() == 0:
self.setVisible(False)
else:
for tab in [tab for tab in self.tabs if tab is not self.tab]:
try:
self.tabs[tab].setSizePolicy(QtWidgets.QSizePolicy.Ignored,
QtWidgets.QSizePolicy.Ignored)
self.tabs[tab].update()
except Exception:
pass
self.tab.setSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
self.tab.update()
self.adjustSize()
When I add a new tab, this works very well. The tab does resize as I want. However, subsequent changes in the tab selection have no effect, even though my debugger confirms that this update function is called.
My application has an embedded shell that shares a namespace with the GUI so I can address all the PyQt widgets. If I call tab.adjustSize()
in the shell, where tab points to the selected tab, the tab does resize! So my question is why the adjustSize
function is ignored when I make the selection and trigger the currentChanged
signal using the GUI, but is not ignored when I call the same slot function a little later from a shell. I've tried adding a sleep time and recursively cycling through the parent widgets calling adjustSize
for each of them, but it has no effect. I've also tried adding self.repaint()
as well as programmatically changing focus and back again, but nothing works except typing it from the shell.
I'm running PyQt5 v5.12.5 on Python 3.8 but I get the same behavior with PyQt v5.9.