[Development] unique_ptr and Qt

Olivier Goffart olivier at woboq.com
Fri Jun 15 09:15:19 CEST 2018


On 2018-06-05 14:40, Daniel Teske wrote:
> Hi,
> 
> I've done some research into how supporting unique_ptr in Qt might look like.
> 
> I have a patch which adds apis using unique_ptr for ownership transfer to 
> almost all of QtBase and a patch for Qt Creator to show how using those apis 
> looks like.
> 
> Qt: https://codereview.qt-project.org/231543
> Qt Creator: https://codereview.qt-project.org/231545
 > [...]

Hi Daniel,

Thanks for doing this.

Here is what i suggest to make the change less intrusive:
For the constructors themself in every classes.

   // Removed the '=nullptr' default parent, and added QT_UNSAFE_API
   QT_UNSAFE_API explicit QLineEdit(QWidget *parent);
   QT_UNSAFE_API explicit QLineEdit(const QString &, QWidget *parent);
   // Added new non-deprecated constructor not taking parent.
   explicit QLineEdit(const QString & = {});


//The QObject::makeChild method:
template <class T, class... Args> T* QObject::makeChild(Args&&... args)
{
   auto n = new T(std::forward<Args>(args)...);
   n->setParent(this);
   return n;
}


That way, std::make_unique still work, and constructing objects on the stack 
does not change.

For the cases where the parent in the constructor has another meaning, we would 
need another factory function (makeChild) in the relevant class. Or interept 
the ParentChanged, or reimplement setParent in that class.

What do you think?

-- 
Olivier

Woboq - Qt services and support - https://woboq.com - https://code.woboq.org



More information about the Development mailing list