Important: Please read the Qt Code of Conduct -

Call destructor: QObject and QWidget

  • I'm developing a shared library that contains a window GUI. This library will be loaded dynamically by a main application.

    To load this shared library in my main application, I'm using QPluginLoader.

    void job_manager::test_plugins(QStringList plugins)
        foreach(QString file, plugins) {
            qInfo() << "Loading: " << file;
            QPluginLoader loader(file);
                qInfo() << "Loaded: " << loader.fileName();
                Plugin_API* plugin = qobject_cast<Plugin_API*>(loader.instance());
                if(plugin) {
                    qInfo() << "Plugin Name: " << plugin->get_name();
                    plugin_map.insert(plugin->get_name(), plugin);
                } else {
                    qInfo() << "Wrong Plugin API! Could not cast: " << loader.fileName();
            } else {
                qInfo() << "ERROR: " << loader.fileName() << " - Msg: " << loader.errorString();

    Now I can call the loaded plugins any time in my main application. It works!

    My problem is when the user hits the main application "close button" BEFORE close the plugin window.
    When this occours, the main application GUI closes Ok, but the plugin GUI window still visible and running.
    The main application is not calling the plugin destructor because I don't know how to pass the "parent" pointer to the plugin GUI window method:

    void plugin::run()
        form = new Form(); // 

    I know that the correct way to to this is passing the "parent" argument, like:

    void Analysis_x::run()
        // Compile error because my Plugin is a QObject type and the argument MUST be QWidget type 
        form = new Form(this); 

    My plugin header is:

    class ANALYSIS_XSHARED_EXPORT Analysis_x: public QObject, public Plugin_API
        Q_PLUGIN_METADATA(IID "com.lamar.plugin")
        explicit Analysis_x(QObject* parent = nullptr);
        ~Analysis_x() override;
        QString get_name(void) override;
        QString get_description(void) override;
        QJsonObject get_input_variables(void) override;
        QJsonObject get_output_variables(void) override;
        QString get_output_name(int output) override;
        void set_save_output(int output, bool save) override;
        void set_working_data_file(QString data_path) override;
        void set_working_log_file(QString log_path) override;
        void run(void) override;
        void set_console_output(bool enabled) override;
        Form* form;
    public slots:

    I would like to, when the user hits the main application "close button", the main application close the plugin window GUI automatically.

    So, how can I pass a valid "parent" argument (QWidget) to the Form?

    You can saw a close example of this problem in this YouTube tutorial video in the 10:00 minutes:
    [](link url)

  • Lifetime Qt Champion

    Just add a function in Plugin_API that takes a QWidget * that your
    internal forms can use as parent. (you store it in plugin)

Log in to reply