1. 判断信号是否正确连接
通过判断connect的返回值是否为true。
bool _ok = connect(this, SIGNAL(signal1()), this, SLOT(slot1())); // 打印true qDebug() << _ok;
2. 判断信号是否被连接了
receivers返回的是该信号的连接数,如果大于0则为信号有连接。
- 原型:
[protected] int QObject::receivers(const char *signal) const
- 实现
bool isSignalConnected = this->receivers(SIGNAL(signal1())) > 0;
Qt5.0版本以上可以这样做:
QMetaMethod _signal1 = QMetaMethod::fromSignal(&Object::signal1); isSignalConnected = this->isSignalConnected(_signal1);
3. 判断信号槽之间参数是否一致
- 原型:
[static] bool QMetaObject::checkConnectArgs(const char *signal, const char *method) [static] bool QMetaObject::checkConnectArgs(const QMetaMethod &signal, const QMetaMethod &method)
- 实现:
bool _ok = QMetaObject::checkConnectArgs(SIGNAL(signal2(int)), SLOT(slot2(int))); qDebug() << _ok;
4. 例子
1 class Object : public QObject 2 { 3 Q_OBJECT 4 public: 5 Object() 6 { 7 // 返回值可判断信号槽是否连接成功。 8 bool ok = connect(this, SIGNAL(signal1()), this, SLOT(slot1())); 9 // 打印true 10 qDebug() << ok; 11 12 // receivers返回的是该信号的连接数,如果大于0则为信号有连接。 13 bool isSignalConnected = this->receivers(SIGNAL(signal1())) > 0; 14 // 打印true 15 qDebug() << isSignalConnected; 16 17 QMetaMethod _signal1 = QMetaMethod::fromSignal(&Object::signal1); 18 isSignalConnected = this->isSignalConnected(_signal1); 19 // 打印true 20 qDebug() << isSignalConnected; 21 22 // 检查参数是否一致 23 ok = QMetaObject::checkConnectArgs(SIGNAL(signal2(int)), SLOT(slot2(int))); 24 // 打印true 25 qDebug() << ok; 26 } 27 28 signals: 29 void signal1(); 30 void signal2(int); 31 32 public slots: 33 void slot1() { } 34 void slot2(int) { } 35 };
标签:const,Qt,--,SIGNAL,绑定,signal1,bool,qDebug,true From: https://www.cnblogs.com/zzzsj/p/17767907.html