Adding QWidget to layout of other QWidget without reparenting



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


  • Moderators

    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().



  • @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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.