使用 connect() 和 tr() 而不使用 QObject::
Use connect() and tr() without QObject::
我经常看到人们在没有QObject::
的情况下使用connect()
。
我该怎么做?当我只写connect()
时,我收到错误:
'connect' was not declared in this scope
我不能使用using namespace QObject
,因为QObject是类,而不是命名空间。
当您处于从 QObject
继承的类的成员函数中时,可以执行此操作。当您不在从QObject
继承的对象范围内时,您应该使用对象实例而不是范围。
所以例如:
class MyClass : public QObject
{
...
void myMemberFunction()
{
...
connect(...); // Implicitly uses this->connect(...)
...
}
...
};
void myNonmemberFunction(MyClass& instanceOfMyClass)
{
...
instanceOfMyClass.connect(...)
...
}
对于翻译,您可以使用Q_DECLARE_TR_FUNCTIONS
(此处的示例)。
对于连接,QObject 类定义了很多静态连接方法。
相关文章:
- 删除 QSharedPointer 指向的 QObject
- QObject::连接无法将信号连接到*this*对象的插槽
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- QML/C++/QObject Struct Copy
- QObject::d eleteLater在我的Qt测试中没有像预期的那样调用
- 具有 QObject 继承的单例 - Qt
- QObject::连接不起作用 - 使用函数语法找不到信号
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- 智能指针作为 QObject::d eleteLater() 的替代品
- Shiboken2 qobject.h:46:10:致命:找不到'QtCore/qobjectdefs.h'文件
- 在 dll 中为 qLibrary 编译 qobject 时出错
- 将复制构造函数设置为默认值在继承自 QObject 时不起作用
- 我应该使用多个角色还是一个角色,将实际属性推迟到将数据包装/公开为其属性的QObject
- 在另一个线程上发出 QObject 信号的正确方法?
- 为什么 QObject::d isconnect(const QMetaObject::Connection &connection) 采用 const 参数并打算修改它?
- 点云库 (PCL) - 声明点云时何时应使用 ::P tr 的经验法则?
- Qt5 QT_TRANSLATE_NOOP不适用于 tr()
- 如何记录QObject::tr的原始字符串,并仍然向GUI显示翻译
- 使用 connect() 和 tr() 而不使用 QObject::