连接到派生类中的受保护插槽

Man*_*d3r 7 c++ qt signals-slots qt5

这是基类中的声明的样子:

protected:
    void indexAll();
    void cleanAll();
Run Code Online (Sandbox Code Playgroud)

在派生类中,以下内容无法编译:

indexAll();  // OK
connect(&_timer, &QTimer::timeout, this, &FileIndex::indexAll);  // ERROR
connect(&_timer, SIGNAL(timeout()), this, SLOT(indexAll()));  // OK
Run Code Online (Sandbox Code Playgroud)

我想使用第一个变体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);
                                                          ^
Run Code Online (Sandbox Code Playgroud)

cma*_*t85 11

"旧"样式语法有效,因为信号发射贯穿qt_static_metacall(..)其中FileIndex,因此具有受保护的访问权限.

'new'样式语法确实有效,但由于这个原因,不会让你直接获取父类方法的地址.然而,它将采用'继承'的地址indexAll(),因此只需将代码更改为:

connect(&_timer, &QTimer::timeout, this, &Derived::indexAll);
Run Code Online (Sandbox Code Playgroud)