连接到派生类中的受保护插槽
Connect to protected slot in derived class
这是基类中的声明的外观:
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本身时才有必要。
相关文章:
- C++:无法访问声明的受保护成员
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 继承和友元函数,从基类访问受保护的成员
- 从Q_INVOKABLE功能或插槽中更改QQuick图像源
- 为什么派生类的好友不能使用受保护的成员?
- C++:为什么无法在派生类中访问受保护的构造函数?
- 升压信号2将插槽传递到成员功能以断开连接
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- C++ 信号和插槽不工作:插槽不响应事件
- NS3 插槽混淆(需要帮助理解)
- 公共/私有/受保护是否会更改内存中结构的排列?
- QObject::连接无法将信号连接到*this*对象的插槽
- QT 插槽未在主线程上调用
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 控制带有信号/插槽的Qt QML滑动视图
- 在父类中公开受保护的构造函数
- Qt 信号/插槽问题
- Qt:将受保护的 QListWidget::itemChanged 信号连接到插槽
- 连接到派生类中的受保护插槽