That happens because a QGroupBox
itself is not disabled when the checkbox is off, and thus normal widget enablement propagation doesn't apply to this behavior. This workaround is IMHO sensible. The only possible workaround via public APIs would be to add a viewport child widget in the groupbox, and make the everything a child of that viewport:
class GroupBoxViewport : public QWidget {
Q_OBJECT
void updateGeometry() {
if (parent())
setGeometry(parentWidget()->contentsRect());
}
void newParent() {
if (parent()) {
parent()->installEventFilter(this);
updateGeometry();
}
}
protected:
bool eventFilter(QObject *obj, QEvent *ev) override {
if (obj == parent() && ev->type() == QEvent::Resize)
updateGeometry();
return QWidget::eventFilter(obj, ev);
}
bool event(QEvent *ev) override {
if (ev->type() == QEvent::ParentAboutToChange) {
if (parent())
parent()->uninstallEventFilter(this);
} else if (ev->type() == QEvent::ParentChange)
newParent();
return QWidget::event(ev);
}
public:
QWidget(QWidget *parent = {}) : QWidget(parent) {
newParent();
}
};
Then, set the layout on and add all the children to the viewport:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGroupBox box("The Group");
GroupBoxViewPort viewport(&box);
QVBoxLayout layout(&viewport);
QLabel label("A, uh, member of the group");
layout.addwidget(&label);
box.show();
return app.exec();
}