Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reparenting and undo reparenting QWidget
Qt 6.12 LTS is out! See what's new in the release blog

Reparenting and undo reparenting QWidget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.6k 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.
  • napajejenunedk0N Offline
    napajejenunedk0N Offline
    napajejenunedk0
    wrote on last edited by
    #1

    Here's the scenario:

    1. I have a given set of data models - let's say they are just integer values.
    2. I have one page - simple QWidget with vertical layout for instance, that contains views to all models - let's say they are just QLabels that print the integer value, and another one that contains views for a subset of those models.
    3. Only one page is visible at a time.

    The question:
    I want to save memory and create one-to-one relationship between models not one-to-many. I want to reparent the views between the pages.

    Problems:
    What could be lost is the layout order.

    Any suggestions how to best employ this technique?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi there,
      Don't really get want you want to do, but in Qt all pareting is done by the QObject class. When you set the parent of a widget to a different widget, the old parent looses it's child automaticly.
      The problem you encounter is that the layout order is lost? What do you mean by that? If the models them selfs stay in memory and are only set to a different parent, no data should be lost at all, or am I missing something?
      You post is a bit abstract to comment on.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • napajejenunedk0N Offline
        napajejenunedk0N Offline
        napajejenunedk0
        wrote on last edited by
        #3

        "Here":https://docs.google.com/open?id=0B2SoBuawTfgOQk04SEg3bGEzY0E's what I want to do.

        What is the best way to transfer the widgets parenthood between the pages?
        A solution could be to subclass the QWidget that represents the page and make the page reparent all shared widgets in the show event.

        Or another solution could be to keep track of when the QWidget's parent gets changed:

        @
        class QWidget {
        ...
        protected:
        ...
        virtual void changeEvent(QEvent* event)
        {
        if (event->type() == QEvent::ParentChange)

        or
        
        if (event->type() == QEvent::ParentAboutToChange)
        

        }
        ...
        };
        @

        Probably the best solution is to rework the whole page's layout everytime.

        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
        • Unsolved