QGroupBox: always disable children in a checkable, unchecked groupbox

The childEvent handler sets the enabled property of children as they are
added to the groupbox, but applications might later enable children and
check/uncheck the groupbox's checkbox in undefined order. In that case,
we would end up with enabled children inside a conceptually disabled
groupbox (the groupbox's checkbox represents the logical "disabled"
state), which breaks documented QWidget::enabled rules.

To make sure that all children are disabled as per the state of the
groupbox, we need to run that logic once the UI has been set up, and
before it becomes visible. This is what polishing is for, so listen
for that event in addition and handle it the same way as adding (which
duplicates things, but keeps existing code that might depend on things
being updated as they are added working).

Adds the case to the existing enabledChildPropagation test case.

[ChangeLog][QWidget][QGroupBox] Always disable children of a checkable,
unchecked group box before showing.

Change-Id: I978bd27b6f1a3f54ec745faeea529a98d0d93619
Fixes: QTBUG-25938
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Volker Hilsheimer 2019-10-07 16:09:37 +02:00
parent 81a7909693
commit 1748dc3e2d
2 changed files with 12 additions and 2 deletions

View File

@ -389,9 +389,13 @@ bool QGroupBox::event(QEvent *e)
void QGroupBox::childEvent(QChildEvent *c)
{
Q_D(QGroupBox);
if (c->type() != QEvent::ChildAdded || !c->child()->isWidgetType())
/*
Children might have been enabled after being added to the group box, in which case
the childEvent handler ran too early, and we need to disabled children again.
*/
if (!(c->added() || c->polished()) || !c->child()->isWidgetType())
return;
QWidget *w = (QWidget*)c->child();
QWidget *w = static_cast<QWidget*>(c->child());
if (w->isWindow())
return;
if (d->checkable) {

View File

@ -295,6 +295,12 @@ void tst_QGroupBox::enabledChildPropagation()
QVERIFY(!childWidget->isEnabled());
dialog = new QDialog(&testWidget);
QVERIFY(dialog->isEnabled());
// children that are enabled after adding should still be disabled before
// they are shown
childWidget->setEnabled(true);
testWidget.show();
QVERIFY(!childWidget->isEnabled());
}
void tst_QGroupBox::sizeHint()