Important: Please read the Qt Code of Conduct -

Can't call QObject::setParent()

  • I have some code that used to compile and run. However, my latest attempt is refusing to compile. Even though my classes publicly extend QObject, I'm getting errors saying 'class Gf::Attribute' has no member named 'setParent'.

    Infact, even the following code causes 'no member named 'setParent' in 'QObject'':

        QObject* a = new QObject();
        QObject* b = new QObject();

    Even qobject.h doesn't list setParent() as a member. It still appears as a public member function in the docs, though. I'm using Qt 5.12.3. What's going on? Was QObject::setParent() taken out of the API? If so, how do I set the parent?

  • Moderators

    @kitfox said in Can't call QObject::setParent():

    Even qobject.h doesn't list setParent() as a member.

    It sounds like your local copy of qobject.h was (accidentally?) modified. QObject::setParent() definitely exists in Qt 5.12.3:

    A quick fix is to replace your qobject.h with the version in the link above, but this won't repair other files that might have been modified. To be sure that everything is fixed, uninstall and reinstall your copy of Qt 5.12.3.

  • Reinstalling Qt worked. Thanks.

    I still have no idea what I could have done to corrupt things.

  • Lifetime Qt Champion

    @kitfox If you do global rename/refactor, it is possible to modify the Qt or compiler headers too.

    I'd recommend to make them readonly to prevent such accidental changes.


Log in to reply