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. Is there safely about QScopedPointer?
Qt 6.11 is out! See what's new in the release blog

Is there safely about QScopedPointer?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 398 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Alexey Serebryakov
    wrote on last edited by Alexey Serebryakov
    #1

    Hi guys,

    consider following code of class Bar

    class Bar : QWidget {
    public:
        Bar(QWidget* w, ...) : widget_(w) {
            ...
        }
        void Foo() {
            if (widget_) { widget_->setParent(this); } // take ownership
        }
    private:
        QScopedPointer<QWidget, QScopedPointerObjectDeleteLater<QWidget>> widget_;  // some external widget from another library
    }
    

    What about deletion flow of widget_? Is there wrong code?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Alexey-Serebryakov said in Is there safely about QScopedPointer?:

      Is there wrong code?

      Yes because of parent-child QObject relationship. Rule of thumb: don't use auto-pointers with QObject derived classes.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • A Offline
        A Offline
        Alexey Serebryakov
        wrote on last edited by
        #3

        Ah, exactly! Thank you so much.

        1 Reply Last reply
        0

        • Login

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