#include <QtWidgets>
class EnabledManager;
class EnablableWidget
: public QObject { Q_OBJECT
Q_PROPERTY(bool enabled READ isEnabled NOTIFY enabledChanged)
public:
m_widget = widget;
connect(this, SIGNAL(enabledChanged(bool)), m_widget, SLOT(setEnabled(bool)));
}
QWidget *widget
() const { return m_widget;
} bool isEnabled() const { return !(m_implicitlyDisabled || m_disabled); }
public slots:
void setDisabled(bool val = true) {
if(m_disabled == val) return;
m_disabled = val;
if(!m_implicitlyDisabled) emit enabledChanged(!m_disabled);
}
void setImplicitlyDisabled(bool val = true) {
if(m_implicitlyDisabled == val) return;
m_implicitlyDisabled = val;
if(!m_disabled) emit enabledChanged(!m_implicitlyDisabled);
}
signals:
void enabledChanged(bool);
private:
bool m_disabled = false; //C++11
bool m_implicitlyDisabled = false; //C++11
};
class EnabledManager
: public QObject { Q_OBJECT
public:
EnablableWidget
* addWidget
(QWidget *widget
) { EnablableWidget * ew = entry(widget);
if(!ew) {
ew = new EnablableWidget(widget, this);
m_widgets << ew;
}
return ew;
}
EnablableWidget *addWidget(EnablableWidget *ew) {
if(m_widgets.contains(ew)) return ew;
m_widgets << ew;
return ew;
}
EnablableWidget
* entry
(QWidget *widget
) const { foreach(EnablableWidget *w, m_widgets) if(w->widget() == widget) return w;
return 0;
}
public slots:
void setDisabled(bool val) {
foreach(EnablableWidget *ew, m_widgets) ew->setImplicitlyDisabled(val);
}
void setEnabled(bool val) { setDisabled(!val); }
private:
QList<EnablableWidget*> m_widgets;
};
#include "main.moc"
int main(int argc, char **argv) {
EnabledManager manager;
hl->addWidget(dAll);
for(int i=0;i<5;++i) {
l->addWidget(pb);
EnablableWidget *ew = manager.addWidget(pb);
QObject::connect(cb,
SIGNAL(toggled
(bool)), ew,
SLOT(setDisabled
(bool)));
hl->addWidget(cb);
}
l->addLayout(hl);
QObject::connect(dAll,
SIGNAL(toggled
(bool)),
&manager,
SLOT(setDisabled
(bool)));
w.show();
return app.exec();
}
#include <QtWidgets>
class EnabledManager;
class EnablableWidget : public QObject {
Q_OBJECT
Q_PROPERTY(bool enabled READ isEnabled NOTIFY enabledChanged)
public:
EnablableWidget(QWidget *widget, QObject *parent = 0) : QObject(parent) {
m_widget = widget;
connect(this, SIGNAL(enabledChanged(bool)), m_widget, SLOT(setEnabled(bool)));
}
QWidget *widget() const { return m_widget; }
bool isEnabled() const { return !(m_implicitlyDisabled || m_disabled); }
public slots:
void setDisabled(bool val = true) {
if(m_disabled == val) return;
m_disabled = val;
if(!m_implicitlyDisabled) emit enabledChanged(!m_disabled);
}
void setImplicitlyDisabled(bool val = true) {
if(m_implicitlyDisabled == val) return;
m_implicitlyDisabled = val;
if(!m_disabled) emit enabledChanged(!m_implicitlyDisabled);
}
signals:
void enabledChanged(bool);
private:
QWidget *m_widget = 0; //C++11
bool m_disabled = false; //C++11
bool m_implicitlyDisabled = false; //C++11
};
class EnabledManager : public QObject {
Q_OBJECT
public:
EnabledManager(QObject *parent = 0) : QObject(parent) {}
EnablableWidget* addWidget(QWidget *widget) {
EnablableWidget * ew = entry(widget);
if(!ew) {
ew = new EnablableWidget(widget, this);
m_widgets << ew;
}
return ew;
}
EnablableWidget *addWidget(EnablableWidget *ew) {
if(m_widgets.contains(ew)) return ew;
m_widgets << ew;
return ew;
}
EnablableWidget * entry(QWidget *widget) const {
foreach(EnablableWidget *w, m_widgets) if(w->widget() == widget) return w;
return 0;
}
public slots:
void setDisabled(bool val) {
foreach(EnablableWidget *ew, m_widgets) ew->setImplicitlyDisabled(val);
}
void setEnabled(bool val) { setDisabled(!val); }
private:
QList<EnablableWidget*> m_widgets;
};
#include "main.moc"
int main(int argc, char **argv) {
QApplication app(argc, argv);
EnabledManager manager;
QWidget w;
QVBoxLayout *l = new QVBoxLayout(&w);
QHBoxLayout *hl = new QHBoxLayout;
QCheckBox *dAll = new QCheckBox("DISABLE");
hl->addWidget(dAll);
for(int i=0;i<5;++i) {
QPushButton *pb = new QPushButton("BUTTON");
l->addWidget(pb);
EnablableWidget *ew = manager.addWidget(pb);
QCheckBox *cb = new QCheckBox(QString::number(i+1));
QObject::connect(cb, SIGNAL(toggled(bool)), ew, SLOT(setDisabled(bool)));
hl->addWidget(cb);
}
l->addLayout(hl);
QObject::connect(dAll, SIGNAL(toggled(bool)), &manager, SLOT(setDisabled(bool)));
w.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks