I have added a grid layout to a widget in Qt Creator. The hierarchy is:
As long as the widgets in the layout are enabled, the columns are as wide as determined by the widgets. However, I disable all widgets upon startup. Then, the columns shrink and crop the widgets. How can I force the layout to respect also disabled widgets? (Or is there a way of preventing the use of the widgets without disabling them?)
The problem is clearly caused by disabling the widgets. The crucial lines are simply:
for widget in self.tabScrollAreaContents.children():
widget.setEnabled(False)
No further code here but two images:
Correct layout with enabled widgets:
Shrinked columns with disabled widgets:
Supplement:
Minimal code to download: https://www.dropbox.com/sh/i19geisqw6nxuky/AADIPHL1xVKzLCJdoLEsTFDna?dl=0
Looks like this (left is wrong, right is fine):
The only difference between left and right is these lines:
for widget in self.TabScrollAreaContentsA.children():
widget.setEnabled(False)
If you check who the children of TabScrollAreaContentsA using the following code you get the following:
for widget in self.TabScrollAreaContentsA.children():
print(widget)
Output:
<PyQt4.QtGui.QGridLayout object at 0x7f9a19fc3f78>
<PyQt4.QtGui.QPushButton object at 0x7f9a1a0c7048>
<PyQt4.QtGui.QLineEdit object at 0x7f9a1a0c70d8>
<PyQt4.QtGui.QLabel object at 0x7f9a1a0c71f8>
<PyQt4.QtGui.QLineEdit object at 0x7f9a1a0c73a8>
<PyQt4.QtGui.QLabel object at 0x7f9a1a0c7438>
<PyQt4.QtGui.QLineEdit object at 0x7f9a1a0c74c8>
<PyQt4.QtGui.QLabel object at 0x7f9a1a0c7558>
<PyQt4.QtGui.QPushButton object at 0x7f9a1a0c75e8>
<PyQt4.QtGui.QLineEdit object at 0x7f9a1a0c7678>
it is observed that it appears as a child a layout and according to the docs:
QLayout.setEnabled (self, bool)
Enables this layout if enable is true, otherwise disables it.
An enabled layout adjusts dynamically to changes; a disabled layout acts as if it did not exist.
By default all layouts are enabled.
See also isEnabled().
That is to say that if you disable some layout will be the same as if it did not exist.
And the cause of this behavior, the correct thing is only to disable the widgets for it is used the following code:
for obj in self.TabScrollAreaContentsA.children():
if isinstance(obj, QtGui.QWidget):
obj.setEnabled(False)