码迷,mamicode.com
首页 > 其他好文 > 详细

Signal & Slot in QT

时间:2014-12-10 20:59:27      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   on   div   

Try your best to provide an mechanism to implement what you want.

1. All is generated by QT Framework before compiling.

 1 QObject.connect(sender,SIGNAL(signal()), reciver,SLOT(slot())); 

==>

QObject.connect(sender, “2signal( )”,reciver,”1slot()”);

 

 1 class QExample : public QObject
 2 {
 3 
 4        Q_OBJECT
 5 
 6 public:
 7        QTestA (QObject *parent );
 8 
 9        ~QTestA ();
10 signals:
11        void SignalA1 ();
12        void SignalA2 (int i );
13 
14 public slots:
15        void SlotA1 ();
16        void SlotA2 (char *szBuf ,int nSize );
17 private:
18 
19 public:
20 
21 int qt_metacall (QMetaObject ::Call _c , int _id , void **_a )
22 {
23     _id = QObject ::qt_metacall (_c , _id , _a );
24     if (_id < 0)
25         return _id ;
26     if (_c == QMetaObject ::InvokeMetaMethod ) 
27    {
28         switch (_id ) {
29         case 0: SignalA1 (); break ;
30         case 1: SignalA2 ((*reinterpret_cast < int (*)>(_a [1]))); break ;
31         case 2: SlotA1 (); break ;
32         case 3: SlotA2 ((*reinterpret_cast < char *(*)>(_a [1])),(*reinterpret_cast < int (*)>(_a [2]))); break ;
33         default : ;
34          }
35         _id -= 4;
36     }
37     return _id ;
38 }
39       
40 
41 }; 

2. Add additional meta data and function in class

1  QMetaObject 
2 {
3     const QMetaObject *superdata ;
4     const char *stringdata ; //save class name, signal name, parameter name,slot name, all ended by ‘/0‘
5     const uint *data ; //int array, store information of QMetaObjectPrivate
6 }
 1 struct QMetaObjectPrivate
 2 
 3 {
 4 
 5     int revision ;
 6     int className ;
 7     int classInfoCount , classInfoData ;
 8     int methodCount , methodData ;
 9     int propertyCount , propertyData ;
10     int enumeratorCount , enumeratorData ;
11     int constructorCount , constructorData ;
12 }; 

 

 

3. Define your own macro, translate user input string into the real code

All below is added in order to find the right ID for cooresponding method of signal and slot

Example:

 

 1 static const uint qt_meta_data_Q QExampleData[] = {
 2        2,       // revision
 3        0,       // classname
 4        0,    0, // classinfo  
 5        4,   12, // methods and its data
 6        0,    0, // properties
 7        0,    0, // enums/sets
 8        0,     0, // constructors
 9        // 以上部分 是QMetaObjectPrivate 结构信息
10        // signals: signature, parameters, type, tag, flags
11        8,    7,    7,    7, 0x05,
12       21,   19,    7,    7, 0x05,
13       // slots: signature, parameters, type, tag, flags
14       35,    7,    7,    7, 0x0A,
15       56,   44,    7,    7, 0x0A,
16       0        // end
17 }; 

 

 

 

 

// slots: signature, parameters, type, tag, flags
1
static const char qt_meta_stringdata_Q QExampleMetaData [] = { 2 "QTestA/0/0SignalA1()/0i/0SignalA2(int)/0" 3 "SlotA1()/0szBuf,nSize/0SlotA2(char*,int)/0" 4 };

 

 

 

 

4.And connection object to list

 

 1 QMetaObject::connect(sender, signal_index,receiver, method_index,type, types);
 2 QObject *s =const_cast<QObject*>(sender);
 3 QObject*r = const_cast<QObject *>(receiver);
 4 
 5 QOrderedMutexLockerlocker(&s->d_func()->threadData->mutex,                       &r->d_func()->threadData->mutex);
 6 
 7 QObjectPrivate::Connection c;
 8 
 9 c.receiver = r;
10 c.method = method_index;
11 c.connectionType = type;
12 c.argumentTypes= types;
13 
14 s->d_func()->addConnection(signal_index, &c);
15 r->d_func()->refSender(s, signal_index);

 

 

 

 

Signal & Slot in QT

标签:style   blog   io   ar   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/iiiDragon/p/QT.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!