Important: Please read the Qt Code of Conduct -

Porting Qt 4.8 to 5.9 - does QSharedPointer need QObject data now?

  • I'm porting an older Qt application from 4.8.3 to 5.9.1. The code uses QSharedPointer for singleton chunks of functionality which are shared between various places, so that they will clean up after themselves when things shut down. Inside the QSharedPointer::internalConstruct() function though I'm getting an error that "'QObject' is an inaccessible base of 'Elektron::MPS9000::DiagnosticsProtocolHandler'" (the class I'm creating a pointer to). This error did not occur on 4.8.3.

    Now it's right - that class does not derive from QObject. (To be more precise, the concrete class does derive from QObject, but the abstract base class used for the QSharedPointer typename doesn't need to know that.) But my understanding of QSharedPointer is that the data it's pointing to should be able to be any type - I've found examples online for "QSharedPointer<int>", for example.

    The line in question in QSharedPointer::internalConstruct() is

    d->setQObjectShared(ptr, true);

    Based on that, it looks to me like QSharedPointer in Qt 5.9.1 is now requiring the data it points to to derive from QObject. So "QSharedPointer<int>" or whatever my code does are no longer possible on Qt5.

    Can anyone confirm or deny this?

  • Lifetime Qt Champion

    This is working perfectly fine:

    class Foo
        Foo() = default;
        int myParam = 0;
    void test()
        QSharedPointer<Foo> f1(new Foo);
        QSharedPointer<Foo> f2 = QSharedPointer<Foo>::create();

  • Thanks Christian. Not sure what's going on then, for this code to break the internals of QSharedPointer! Ah well, more work needed to figure this out then. At least if it's not a Qt issue, that's one less thing to worry about.

Log in to reply