Skip to content


Qt signals & slots mechanism

Class Defination, must inherit QObject

 #include <QObject>
 
 class Counter : public QObject
 {
     Q_OBJECT
 
 public:
     Counter() { m_value = 0; }
 
     int value() const { return m_value; }
 
 public slots:
     void setValue(int value);
 
 signals:
     void valueChanged(int newValue);
 
 private:
     int m_value;
 };

Send Signals

 void Counter::setValue(int value)
 {
     if (value != m_value) {
         m_value = value;
         emit valueChanged(value);
     }
 }

Connect signals with slots

     Counter a, b;
     QObject::connect(&a, SIGNAL(valueChanged(int)),
                      &b, SLOT(setValue(int)));
 
     a.setValue(12);     // a.value() == 12, b.value() == 12
     b.setValue(48);     // a.value() == 12, b.value() == 48

Posted in 1g1g Linux Desktop App. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.