插件架构中的Qt Signal插槽

时间:2016-06-07 11:21:32

标签: c++ qt signals-slots plugin-architecture qtplugin

我正在尝试使用菜单按钮创建应用程序。   单击按钮,将出现一个表单。      表单是使用插件创建的。问题是 -             在第一次单击时,一个表单正常生成。但在第二次点击时,会生成2个而不是一个的表单。第3次点击可以提供3种表格,依此类推。每次点击我只需要一个表格。

        Is this has anything to do with Q_PLUGIN_METADATA(IID "Camel1") in interface?

这是我的应用程序Mainwindow.cpp

        spPlugin *objSpPlugin=new spPlugin;

            QSqlQuery qryPlugin=objSpPlugin->view_Plugin_Path(this,publicVariables::inEmployeeId,strFormName,evt::onLoad,true);

            while(qryPlugin.next())
            {


                QString   

    strPluginPath=qryPlugin.value("Plugin_Path").toString();
                qDebug()<<strPluginPath;
                QDir pluginsDir(QDir::currentPath()+"/Plugin");

                QPluginLoader loader(pluginsDir.absoluteFilePath(strPluginPath));

                qDebug()<<loader.fileName();

                QObject *obj=loader.instance();
                qDebug()<<loader.errorString();
                if(obj)
                {

                    MainwindowInterface *objMainWindowInterface=qobject_cast<MainwindowInterface *>(obj);

                    if(objMainWindowInterface)
                    {
                      connect(objMainWindowInterface,SIGNAL(CreateNewFormInstance(QWidget*)),SLOT(createNewFormInstance(QWidget*)));

                        objMainWindowInterface->run();


                    }

                }

            }



        void MainWindow::createNewFormInstance(QWidget*frmInstance)
        {

            qDebug()<<"createNewFormInstance";

                if( frmInstance!=NULL)
                {

                    //
                }
                else
                {

                    ui->mdiArea->addSubWindow(frmInstance);
                }
        }


        My plugin InterFace included in pluginproduct.h

        //#ifndef PLUGININTERFACE_H
        //#define PLUGININTERFACE_H

        //#include<QObject>
        //#include<QWidget>
        //#include<QtSql/QSqlQuery>
        //#include<qsqldatabase.h>


        class FormInterface:public QObject
        {

            Q_OBJECT
        public:
            virtual void Show()=0;

        };





        Q_DECLARE_INTERFACE(MainwindowInterface,"Cam1")


        //#endif // PLUGININTERFACE_H




   My Pluginproduct.h 


        class  LibPluginProductForm:public FormInterface

        {


           Q_OBJECT
           Q_PLUGIN_METADATA(IID "Camel1")
           Q_INTERFACES(FormInterface)



        public:
            LibPluginProductForm();

            ~ LibPluginProductForm();
            void Show();


        private:

            QWidget *frm;


        };


        my pluginProduct.cpp


        LibPluginProductForm::LibPluginProductForm()

        {


         frm=new QWidget;



        }

        LibPluginProductForm::~LibPluginProductForm()
        {


        }

        void LibPluginProductForm::Show()

        {


            emit CreateNewFormInstance(frm);
            qDebug()<<"LibPluginProductForm::Show";


            frm->show();`enter code here`

        }

1 个答案:

答案 0 :(得分:0)

The signal will be raised when the 'CreaeNewFormInstance' is called but also from LibPluginProduceForm::Show where you are manually 'emitting' the signal.

Check that your 'Show' method is not being called multiple times. Are you seeing your debug statement in the Application Output ?