这是基类中的声明的样子:
protected: void indexAll(); void cleanAll();
在派生类中,以下内容无法编译:
indexAll(); // OK connect(&_timer,&QTimer::timeout,this,&FileIndex::indexAll); // ERROR connect(&_timer,SIGNAL(timeout()),SLOT(indexAll())); // OK
我想使用connect的第一个变种,因为它做了一些编译时间检查.为什么这会返回错误:
error: 'void Files::FileIndex::indexAll()' is protected void FileIndex::indexAll() ^ [...].cpp:246:58: error: within this context connect(&_timer,&FileIndex::indexAll); ^
解决方法
“旧”样式语法有效,因为信号发射通过qt_static_metacall(..)运行,它是FileIndex的成员,因此具有受保护的访问权限.
‘new’样式语法确实有效,但for this reason不允许直接获取父类方法的地址.然而,它将采用indexAll()的“继承”地址,因此只需将代码更改为:
connect(&_timer,&Derived::indexAll);