boost::shared_ptr pimpl and Qt

Posted on 2009-04-22
Last Modified: 2012-05-06
I recently adopted the pimpl idiom in my code as a clean way of ensuring appropriate behavior and destruction and encapsulation of the shared_ptrs I was passing around previously. I've been implementing it as a boost::shared_ptr to a forward-declared "-Impl" class defined as a struct in the .cpp file.

What I've found, however, is that Qt doesn't like the idea of shared state across objects much at all. I have found a consistent need to create QLists and QMaps of objects that extend QObject, and I had become tired of the extra typing required in making everything a list of MyObject* or shared_ptr<MyObject>, so I had hoped pimpl would resolve this difficulty. As it turns out, connected Qt signals only work on the original object, not its copy, so I had to implement assignment operators and copy constructors to ensure the signals connected in the constructor stayed connected. This has the unfortunate side effect of allowing an object to be copied AFTER signals are assigned externally, and the object has no way of knowing dynamically what it needs to reassign.

It gets worse when multiple threads are involved. If an object creates a background thread for itself during construction, it passes a boost binding to "this" for the thread to call, then after construction it gets copied and destructed, and the "this" reference the thread has is no longer valid. I got around this by passing the pimpl to the thread instead, and modifying the "parent" object of the pimpl during copy and assignment.

Now my code at least works, but in all reality it's no more than an auto_ptr in shared_ptr's clothing, with none of the type safety.

Is there an obvious solution to this? Can Qt support shared pimpls, or is it a lost cause? Do I need to go back to shared_ptrs if I need shared objects?
Question by:dberner9
    1 Comment
    LVL 30

    Accepted Solution

    Consider using a clone_ptr or smart_ptr.
    The following link has a smart_ptr which can be use to create a clone_ptr.

    Clone pointers have logic which allows object to clone itself, even if it's a derived class pointing to a base class smart pointer.

    Check out following link for more info:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now