class Controller
: public QObject { Q_OBJECT;
public:
Controller(){
m_instance = this;
m_active = true;
}
static Controller *instance() { return m_instance; }
bool isActive() const { return m_active; }
public slots:
void setActive(bool v){
if(v==m_active) return;
m_active = v;
emit activeChanged(v);
}
signals:
void activeChanged(bool)
private:
bool m_active;
static Controller *m_instance;
};
Controller* Controller::m_instance = 0;
// ...
Q_OBJECT
public:
m_active = Controller::instance()->isActive();
connect(Controller::instance(), SIGNAL(activeChanged(bool)), this, SLOT(setActive(bool)));
connect(this, SIGNAL(activeChanged(bool)), Controller::instance(), SLOT(setActive(bool)));
}
public slots:
void setActive(bool v) {
if(m_active==v) return;
m_active = v;
emit activeChanged(v);
}
signals:
void activeChanged(bool);
private:
bool m_active;
}
#include "main.moc"
int main(int argc, char **argv){
Controller c;
MyObject obj1, obj2;
// effectively these are equivalent:
obj1.setActive(false);
// or
obj2.setActive(false);
// or
c.setActive(false);
// or
QObject::connect(&cb,
SIGNAL(toggled
(bool)), Controller
::instance(),
SLOT(setActive
(bool)));
cb.show();
return app.exec();
}
class Controller : public QObject {
Q_OBJECT;
public:
Controller(){
m_instance = this;
m_active = true;
}
static Controller *instance() { return m_instance; }
bool isActive() const { return m_active; }
public slots:
void setActive(bool v){
if(v==m_active) return;
m_active = v;
emit activeChanged(v);
}
signals:
void activeChanged(bool)
private:
bool m_active;
static Controller *m_instance;
};
Controller* Controller::m_instance = 0;
// ...
class MyObject : public QObject {
Q_OBJECT
public:
MyObject() : QObject(){
m_active = Controller::instance()->isActive();
connect(Controller::instance(), SIGNAL(activeChanged(bool)), this, SLOT(setActive(bool)));
connect(this, SIGNAL(activeChanged(bool)), Controller::instance(), SLOT(setActive(bool)));
}
public slots:
void setActive(bool v) {
if(m_active==v) return;
m_active = v;
emit activeChanged(v);
}
signals:
void activeChanged(bool);
private:
bool m_active;
}
#include "main.moc"
int main(int argc, char **argv){
QApplication app(argc, argv);
Controller c;
MyObject obj1, obj2;
// effectively these are equivalent:
obj1.setActive(false);
// or
obj2.setActive(false);
// or
c.setActive(false);
// or
QCheckBox cb;
QObject::connect(&cb, SIGNAL(toggled(bool)), Controller::instance(), SLOT(setActive(bool)));
cb.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks