Disabling a QCheckbox in a tricky way

别等时光非礼了梦想. 提交于 2019-12-13 07:17:13

问题


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()).



来源:https://stackoverflow.com/questions/8542123/disabling-a-qcheckbox-in-a-tricky-way

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!