Disabling a QCheckbox in a tricky way

2019-08-22 02:25发布

问题:

I want to make a QCheckBox named "Show Captions" disable another QCheckBox named "Show captions if no title" when the first is checked, but my problem is that how I can make it disabled immediately when the user checks the first checkbox.

SetupSlideShow::SetupSlideShow(QWidget* parent)
    : QScrollArea(parent), d(new SetupSlideShowPriv)
{
    QWidget* panel = new QWidget(viewport());
    setWidget(panel);
    setWidgetResizable(true);

    QVBoxLayout* layout = new QVBoxLayout(panel);

    d->showComment = new QCheckBox(i18n("Show captions"), panel);
    d->showComment->setWhatsThis( i18n("Show the image caption at the bottom of the screen."));

    d->showTitle = new QGroupBox(i18n("Show title"), panel);
    d->showTitle->setWhatsThis( i18n("Show the image title at the bottom of the screen."));
    d->showTitle->setCheckable(true);

    d->showCapIfNoTitle = new QCheckBox(i18n("Show captions if no title"), panel);
    d->showCapIfNoTitle->setWhatsThis( i18n("Show the image caption at the bottom of the screen if no titles existed."));
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(d->showCapIfNoTitle);
    d->showTitle->setLayout(vbox);
    layout->addWidget(d->showLabels);
    layout->addWidget(d->showComment);
    layout->addWidget(d->showTitle);
}

回答1:

Doesn't this work?

connect(d->showComment, SIGNAL(toggled(bool)), d->showCapIfNoTitle, SLOT(setDisabled(bool)));



回答2:

The call to paintEvent() isn't really doing anything for you regarding immediacy. Nothing will be repainted until control returns to the event loop (after your constructor exits). It is more typical to call update() but even this is unnecessary when changing the properties of built in widgets.

To link the check boxes, define a slot for the stateChanged() signal of showComment, connect the signal to your slot in your constructor above (by calling connect(), and in that slot, call d->showCapIfNoTitle->setCheckState(d->showComment->checkState()).