在没有 QObject:: 的情况下使用 connect() 和 tr()

Use connect() and tr() without QObject::

我经常看到人们在使用 connect() 而没有使用 QObject::
我怎样才能做到这一点?当我只写 connect() 时出现错误:
'connect' was not declared in this scope

我不能使用 using namespace QObject,因为 QObject 是 class,不是命名空间。

当您处于从 QObject 继承的 class 的成员函数中时,可以完成此操作。当您不在从 QObject 继承的对象的范围内时,您应该使用 对象实例 而不是范围。

例如:

class MyClass : public QObject
{
    ...
    void myMemberFunction()
    {
        ...
        connect(...);  // Implicitly uses this->connect(...)
        ...
    }
    ...
};

void myNonmemberFunction(MyClass& instanceOfMyClass)
{
    ...
    instanceOfMyClass.connect(...)
    ...
}