这是基类中的声明的外观:
protected:
void indexAll();
void cleanAll();
在派生类中,以下内容不编译:
indexAll(); // OK
connect(&_timer, &QTimer::timeout, this, &FileIndex::indexAll); // ERROR
connect(&_timer, SIGNAL(timeout()), this, SLOT(indexAll())); // OK
我想使用connect
的第一个变体,因为它会进行一些编译时检查。为什么会返回错误:
error: 'void Files::FileIndex::indexAll()' is protected
void FileIndex::indexAll()
^
[...].cpp:246:58: error: within this context
connect(&_timer, &QTimer::timeout, this, &FileIndex::indexAll);
^
"旧"样式语法之所以有效,是因为信号发射贯穿qt_static_metacall(..)
,它是FileIndex
的成员,因此具有受保护的访问。
"新"样式语法确实有效,但因此不允许直接获取父类方法的地址。 但是,它将采用indexAll()
的"继承"地址,因此只需将代码更改为:
connect(&_timer, &QTimer::timeout, this, &Derived::indexAll);
第一个由正常C++可访问性规则规则统治。QTimer::timeout 信号直接在提供的函数指针上调用 FileIndex::indexAll。当然,这只有在此函数指针是公共的(忽略可能的友元解决方案)时才有可能。如果使用函数指针,则甚至不需要在头文件中将函数标记为 SLOT。
第二个是moc魔法。通过元对象系统调用。我从来没有深入研究过这个话题......它只是工作。:-)
好吧,不是最好的解释。如果您想了解更多信息:
http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
http://woboq.com/blog/how-qt-signals-slots-work.html
http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html
好读,但是...只有当你对Qt的更深层次的工作感兴趣时才感兴趣,恕我直言,只有当你想开发Qt本身时才有必要。