Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Shared Pointer in QT compared to C++11
Forum Update on Monday, May 27th 2025

Shared Pointer in QT compared to C++11

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 964 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TauCeti
    wrote on 7 Sept 2017, 14:10 last edited by
    #1

    Hello,

    I am looking for the QT equivalent of the following C++11
    formalism:

    auto ptr = std::make_unique<ClassA>(0,0);
    auto ptr = std::make_shared<ClassA>(0,0);

    When using :
    QSharedPointer<ClassA> ptr = QSharedPointer<ClassA>(new ClassA);

    some methods do not work because the types do not fit, e.g.
    addWidget(ptr);

    Thanks for help.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 7 Sept 2017, 14:42 last edited by VRonin 9 Jul 2017, 14:45
      #2
      auto ptr = std::make_unique<ClassA>(0,0);
      //is the same as
      auto ptr =  QSharedPointer<ClassA>::create(0,0);
      

      Above i lied as QSharedPointer ~= std::shared_ptr while QScopedPointer ~= std::unique_ptr but let me get away with it

      addWidget(ptr);

      just use addWidget(ptr.data());

      Be aware though that functions like the mentioned (I suppose) QLayout::addWidget reparent the widget and will take ownership of the object. Manual/smart deletion can lead to crash due to double delete

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2

      1/2

      7 Sept 2017, 14:10

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved