[Interest] Doubt extending my app functions ability

Ernesto ernestoac at icrt.cu
Fri Jan 13 00:33:28 CET 2017


Hello again, first, thanks to Bob Hood for his quickly reply.

I understood your explanation about my second problem, but I have 
another issue writing your solution in an example project. I have a 
MainApp (QT Widgets project) and a Lib1 (C++ Static Library project) and 
here is the code:

//This is header file of *Lib1*

#include"lib1_global.h"

#include<QObject>

#include<QDebug>

#include"pinterface.h"

//this is my interface

classLIB1SHARED_EXPORTLib1:publicPInterface

{

Q_OBJECT

public:

Lib1();

QString/Name/()const; //example method

};

//This is cpp file of *Lib1*

QStringLib1::/Name/()const

{

return"HellofromDLL";

}

//This is cpp file of *MainApp*

MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow)

{

ui->setupUi(this);

QDirplugins("C:/lib");

QStringListplugins_list=plugins.entryList(QStringList()<<"*.dll"<<"*.so",QDir::Files);

foreach(constQString&filename,plugins_list)

{

QStringplugin_path=QDir::toNativeSeparators(QString("%1/%2").arg(plugins.absolutePath()).arg(filename));

QPluginLoaderpl(plugin_path);

QObject*instance=pl.instance();

if(instance)

{

PInterface*iernestofactory=reinterpret_cast<PInterface*>(instance);

if(iernestofactory)

{

QMessageBox::information(NULL,"hello",iernestofactory->/Name/());

}

}else{

//Always throw this error

QMessageBox::information(NULL,"error","badcreatedinstance");

}

}

}

//This is header file of my interface, is included by both projects

classPInterface:publicQObject

{

Q_OBJECT

public:

explicitPInterface(QObject*parent=0);

virtualQString/Name/()const=0;

signals:

publicslots:

};

If a run the application he discover the Lib1.dll file, but the MainApp 
throw the “bad created instance” error, that is in cpp file of MainApp. 
InQObject* instance = pl.instance(); when I evaluate the “instance” 
variable, takes 0 value and the Qt documentation says: “/The function 
returns 0 if the plugin could not be loaded or if the root component 
object could not be instantiated.”/It refers to instance () function.

Until now I can´t solve this logical error.

Regards,

Ernesto


El 12/01/2017 a las 21:29, Bob Hood escribió:
> On 1/12/2017 6:23 AM, Ernesto wrote:
>>
>> Hello to everyone,
>>
>> My name is Ernesto. I am a beginner in Qt programming and I have an 
>> specifics problems whit my first big project (desktop app). Let me 
>> explain myself:
>>
>> I have to include plugins or add-ons to my application for new file 
>> format types; I looked around QPluginLoader class, Dynamic and Static 
>> libraries classes to achieve this goal, and here is my problem, in 
>> each one of this I have to include the header file and the library 
>> (or plugins) in my project. I have to develop other mini app to 
>> create those plugins or libraries. So here is _my first problem_, I 
>> don´t know how many plugins or libraries will be development to 
>> include in my app.
>>
>
> Hello, Ernesto.
>
> I will not address your entire problem (because I'm not clearly 
> understanding the second one), but your first is easy enough to 
> answer.  The answer is:  You don't /need/do know how many plug-ins 
> will be in development.
>
> Plug-ins, by definition, are external code to your main application.  
> You application needs to /discover/those pieces of external code when 
> it runs.  This way, any number of plug-ins can be included with your 
> application--or even added later by the user to an existing 
> installation of your application--without having to modify any of your 
> main application's code.
>
> For example, when your main application starts, it looks in a 
> location--either hard-coded, or defined by the user--and discovers all 
> the available shared libraries:
>
>     ...
>     QDir plugins("ernestos_plugins");
>     QStringList plugins_list = plugins.entryList(QStringList() << 
> "*.dll" << "*.so", QDir::Files);
>     ...
>
> With each shared library it finds, it tests each to see if it is a 
> plug-in designed for your application by coercing it to the interface 
> contract, something like:
>
>     foreach(const QString& filename, plugins_list)
>     {
>         QString plugin_path = 
> QDir::toNativeSeparators(QString("%1/%2").arg(plugins.absolutePath()).arg(filename));
>         QPluginLoader(plugin_path);
>         QObject* instance = plugin.instance();
>         if(instance)
>         {
>             IErnestoPlugin* iernestofactory = 
> reinterpret_cast<IErnestoPlugin*>(instance);
>             if(iernestofactory)
>             {
>                 ...
>
> Each one that supports the contract of your interface 
> ("IErnestoPlugin") can be safely used within your application as a 
> plug-in designed for your main application.
>
> I hope I understood the first problem correctly. Apologies if I didn't.
>
>
> __________ Información de ESET NOD32 Antivirus, versión de la base de 
> firmas de virus 13498 (20160516) __________
>
> ESET NOD32 Antivirus ha comprobado este mensaje.
>
> http://www.eset.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170113/573c04f8/attachment.html>


More information about the Interest mailing list