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. Adding QWidget to layout of other QWidget without reparenting
QtWS25 Last Chance

Adding QWidget to layout of other QWidget without reparenting

Scheduled Pinned Locked Moved Solved General and Desktop
qwidgetparentlayout
3 Posts 2 Posters 1.5k 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 22 May 2016, 13:12 last edited by
    #1

    I'm writing an application that allows loading plugins. Each plugin has to manage it's own settings, however, I'd still like to have a global location where I can change the settings of the plugins:
    Screenshot
    In the screenshot above, each widget is able to populate the Plugin Settings group box in the bottom right hand corner.

    To do that, the plugin interface contains the following methods:

    class Plugin
    {
    public:
        virtual ~Plugin() {}
    
        virtual QWidget* settingsWidget() = 0;
        virtual void settingsApply() = 0;
    };
    

    When a plugin is selected in the list box (left side in the screenshot) I simply delete all items from the groupbox layout and add Plugin::settingsWidget() to it. The problem is that when the preferences dialog closes the groubox and the layout are destroyed and the widget that was returned by Plugin::settingsWidget() gets destroyed too.

    The obvious solution to this is to make Plugin::settingsWidget() always return a new QWidget instance. However, I'd like to avoid that for two reasons: Firstly, the returned widget holds a reference to the settings data for that plugin. If Plugin::settingsWidget() gets called multiple times this becomes an issue. Secondly, as you can see there's a Plugins::settingsApply() function which is called when the preferences dialog is accepted(). The plugin MUST save it's own settings, therefore, it must have the ownership of the settings widget.

    Long story short: I need to be able to add a QWidget to my preferences dialog without changing the parent/ownership of the widget.
    Any ideas or recommendations? Maybe I miss an obvious solution to this problem...

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 22 May 2016, 13:22 last edited by
      #2

      Well no, you can't have a child that is not a child ;)
      Focus, painting and event propagation would be impossible if that was the case.

      A simple fix is to set the parent back to null when you no longer need the widget e.g. after you call settingsApply().

      J 1 Reply Last reply 22 May 2016, 13:31
      1
      • C Chris Kawa
        22 May 2016, 13:22

        Well no, you can't have a child that is not a child ;)
        Focus, painting and event propagation would be impossible if that was the case.

        A simple fix is to set the parent back to null when you no longer need the widget e.g. after you call settingsApply().

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 22 May 2016, 13:31 last edited by
        #3

        @Chris-Kawa said:

        A simple fix is to set the parent back to null when you no longer need the widget e.g. after you call settingsApply().

        Well that gets the job done. I was wondering whether there was a better / proper solution to this.

        Thank you for your help! I appreciate it a lot!

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0

        3/3

        22 May 2016, 13:31

        • Login

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