图解Qt信号槽实现

https://blog.csdn.net/sun222555888/article/details/80642740

信号槽实现需了解如下基础组件

    QObject - 核心数据

复制代码
 1 //QObject数据基类
 2 class Q_CORE_EXPORT QObjectData {
 3 public:
 4     virtual ~QObjectData() = 0;
 5     QObject *q_ptr;
 6     QObject *parent;
 7     QObjectList children;
 8 
 9     uint isWidget : 1;
10     uint blockSig : 1;
11     uint wasDeleted : 1;
12     uint isDeletingChildren : 1;
13     uint sendChildEvents : 1;
14     uint receiveChildEvents : 1;
15     uint isWindow : 1; //for QWindow
16     uint unused : 25;
17     int postedEvents;
18     QDynamicMetaObjectData *metaObject;
19     QMetaObject *dynamicMetaObject() const;
20 };
复制代码

//QObject数据基类

复制代码
 1 //QObjectPrivate扩展数据
 2 struct ExtraData
 3 {
 4     ExtraData() {}
 5 #ifndef QT_NO_USERDATA
 6     QVector<QObjectUserData *> userData;
 7 #endif
 8     QList<QByteArray> propertyNames;
 9     QList<QVariant> propertyValues;
10     QVector<int> runningTimers;
11     QList<QPointer<QObject> > eventFilters; //事件过滤器数据集
12     QString objectName;
13 };
复制代码
复制代码
1 //QObjectPrivate数据概览
2 public:
3     ExtraData *extraData;    // extra data set by the user
4     QThreadData *threadData; // id of the thread that owns the object
5 
6     QObjectConnectionListVector *connectionLists; //最重要的:信号槽关联关系链表!
7 
8     Connection *senders;     // linked list of connections connected to this object
9     Sender *currentSender;   // object currently activating the object
复制代码

    QMetaObject

复制代码
 1 //QMetaObject核心数据类
 2 struct { // private data
 3     const QMetaObject *superdata; //最重要的:父类元对象!
 4     const QByteArrayData *stringdata;   //最重要的:字符串流,多种类信息!
 5     const uint *data; //最重要的:二进制流,多种类信息。部分数据,通过Index索引从stringdata解析出来!
 6     typedef void (*StaticMetacallFunction)(QObject *, QMetaObject::Call, int, void **);
 7     StaticMetacallFunction static_metacall;
 8     const QMetaObject * const *relatedMetaObjects;
 9     void *extradata; //reserved for future use
10 } d;
复制代码


如何通过QObject/QMetaObject实现信号槽
举个例子:初始阶段,老师的一个信号与学生的三个槽分别链接。程序启动后,触发老师的信号,然后学生的三个槽函数分别执行。

    简述元对象、QObject、子对象的关系;信号槽的链接
  



    一个信号触发后,如何调到槽函数的大致流程
 

 



关注核心类、核心数据的流动,不要深陷细节。

posted on 2021-01-26 15:40  莫水千流  阅读(462)  评论(0编辑  收藏  举报