QObject::connect函数,顾名思义,链接函数,作用是链接信号(signal)和槽(slot), 基本格式如下:
QObject::connect(sender, signal, receiver, slot)
当sender发出了signal(这种信号类似于广播,谁需要谁就建立相应的槽去接收)之后,会自动调用receiver 的slot函数。connect函数也是信号槽机制的体现。
而QObject::connect( )这个函数有五种重载(overloaded)形式,下面给出来。
QMetaObject::Connection connect(const QObject *, const char *, const QObject *, const char *, Qt::ConnectionType); QMetaObject::Connection connect(const QObject *, const QMetaMethod &, const QObject *, const QMetaMethod &, Qt::ConnectionType); QMetaObject::Connection connect(const QObject *, const char *, const char *, //receiver为this指针 Qt::ConnectionType) const; QMetaObject::Connection connect(const QObject *, PointerToMemberFunction, const QObject *, PointerToMemberFunction, //指向成员函数的指针 Qt::ConnectionType) QMetaObject::Connection connect(const QObject *, PointerToMemberFunction, Functor); //Functor可以接受static函数,全局函数,以及lambda表达式
具体解释可以查看这个博客:https://www.devbean.net/2012/08/qt-study-road-2-signal-slot/
另外加点容易遗漏的知识点:可以用父类的构造函数来初始化子类的一部分数据。比方说MainWindow(QWidget *parent):QMainWindow(parent){...}。