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. setCentralWidget() and automatic deletion
Forum Updated to NodeBB v4.3 + New Features

setCentralWidget() and automatic deletion

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 254 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.
  • I Offline
    I Offline
    Ignis
    wrote on last edited by Ignis
    #1

    I'm a newbie at Qt and I'm building my first app.
    In order to make things more modular, I created three QWidgets that reflect the three very different states of the app. A QMainWindow-derived class is responsible to "load and unload" those widgets through setCentralWidget(), managing transition from one to another.
    However, I found out that, when you put a QWidget inside something (in my case set it as the central widget of the MainWindow), that "something" acquires ownership of the QWidget and is responsible for its deletion. My problem is that I want to preserve the state of those QWidgets between transitions, so I can't allow them to be deleted.

    Is there any elegant solution to this? I tried to find out how are those pointers stored in the parent widget in order to check if it's possible to force the framework to preserve QWidgets through the use of shared pointers, but wasn't able to.

    JonBJ kshegunovK 2 Replies Last reply
    0
    • I Ignis

      I'm a newbie at Qt and I'm building my first app.
      In order to make things more modular, I created three QWidgets that reflect the three very different states of the app. A QMainWindow-derived class is responsible to "load and unload" those widgets through setCentralWidget(), managing transition from one to another.
      However, I found out that, when you put a QWidget inside something (in my case set it as the central widget of the MainWindow), that "something" acquires ownership of the QWidget and is responsible for its deletion. My problem is that I want to preserve the state of those QWidgets between transitions, so I can't allow them to be deleted.

      Is there any elegant solution to this? I tried to find out how are those pointers stored in the parent widget in order to check if it's possible to force the framework to preserve QWidgets through the use of shared pointers, but wasn't able to.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Ignis
      Before you spend any more time on

      "load and unload" those widgets through setCentralWidget(), managing transition from one to another.

      have you seen https://doc.qt.io/qt-5/qstackedwidget.html, which is commonly used for what sounds like your usage?

      I 1 Reply Last reply
      4
      • I Ignis

        I'm a newbie at Qt and I'm building my first app.
        In order to make things more modular, I created three QWidgets that reflect the three very different states of the app. A QMainWindow-derived class is responsible to "load and unload" those widgets through setCentralWidget(), managing transition from one to another.
        However, I found out that, when you put a QWidget inside something (in my case set it as the central widget of the MainWindow), that "something" acquires ownership of the QWidget and is responsible for its deletion. My problem is that I want to preserve the state of those QWidgets between transitions, so I can't allow them to be deleted.

        Is there any elegant solution to this? I tried to find out how are those pointers stored in the parent widget in order to check if it's possible to force the framework to preserve QWidgets through the use of shared pointers, but wasn't able to.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Ignis said in setCentralWidget() and automatic deletion:

        Is there any elegant solution to this?

        Yes. Put a stack widget (QStackedWidget) as your central widget and add your widgets to it. Then you can manually switch between them. If you want to take the widget out of the stack (as it takes ownership by default too), you can use QStackedWidget::removeWidget.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • JonBJ JonB

          @Ignis
          Before you spend any more time on

          "load and unload" those widgets through setCentralWidget(), managing transition from one to another.

          have you seen https://doc.qt.io/qt-5/qstackedwidget.html, which is commonly used for what sounds like your usage?

          I Offline
          I Offline
          Ignis
          wrote on last edited by
          #4

          @JonB Thanks, I actually used QStackedWidget inside those QWidget but didn't think about it in this case.

          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