How to make a Qt widget invisible without changing the position of the other Qt widgets?
layout, qt
Solution
This problem was solved in Qt 5.2. The cute solution is:
QSizePolicy sp_retain = widget->sizePolicy();
sp_retain.setRetainSizeWhenHidden(true);
widget->setSizePolicy(sp_retain);
http://doc.qt.io/qt-5/qsizepolicy.html#setRetainSizeWhenHidden
Problem
I've got a window full of QPushButtons and QLabels and various other fun QWidgets, all layed out dynamically using various `QLayout` objects... and what I'd like to do is occasionally make some of those widgets become invisible. That is, the invisible widgets would still take up their normal space in the window's layout, but they wouldn't be rendered: instead, the user would just see the window's background color in the widget's rectangle/area. `hide()` and/or `setVisible(false)` won't do the trick because they cause the widget to be removed from the layout entirely, allowing other widgets to expand to take up the "newly available" space; an effect that I want to avoid. I suppose I could make a subclass of every `QWidget` type that override `paintEvent()` (and `mousePressEvent()` and etc) to be a no-op (when appropriate), but I'd prefer a solution that doesn't require me to create three dozen different `QWidget` subclasses.