PDA

View Full Version : use the plugin and QT script at the same time



SamT
25th April 2011, 07:37
Can I use the plugin function and the QT script at the same program? That means to build a plugin DLL and load by plugin loader then I can use write a easy script to drive that plugin.

I try to implement this idea into my program. The plugin load successfully but I can't drive by the script. Does anybody uses this idea in your program? Any example for me to study?
:confused:

My plugin interface

class TaInterface
{
public:
virtual ~TaInterface() {}
virtual QString pluginName() = 0;

public slots:
virtual bool initialize(const QStringList &arguments, QString *errorString) = 0;
virtual QString function() = 0;

Load plugin and set to script engine

void MainWindow::loadPlugins()
{
engine = new QScriptEngine(this);

pluginsDir = QDir(qApp->applicationDirPath());

if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
pluginsDir.cdUp();

pluginsDir.cd("plugins");

foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = loader.instance();
if (plugin)
{
pluginFileNames += fileName;
Interface *ta = qobject_cast<Interface *>(plugin);

QScriptValue TaToScript = engine->newQObject(plugin);
engine->globalObject().setProperty(ta->pluginName(), TaToScript);

}

}
}

DLL plugin

class AD : public QObject, public TaInterface
{
Q_OBJECT
Q_INTERFACES(TaInterface)

public:
~AD();
bool initialize(const QStringList &arguments, QString *errorString);
QString pluginName();

public slots:
QString function();

};

wysota
25th April 2011, 12:10
So what exactly doesn't work?

SamT
26th April 2011, 03:25
The script file can't find the function which listed in the public slot in the interface.
I am new to the script. Can you please guide me what's wrong?


var functionName = AD.function();
print(functionName);

SamT
27th April 2011, 07:52
does anybody have idea? I still can't figure out the way to use QT script to drive a plugin function.

Thanks for your attention!

wysota
27th April 2011, 09:23
Does the AD object exist? Please provide a print of its properties (if you don't know how, search the web for printing properties of javascript objects).

SamT
28th April 2011, 14:04
I think AD object is exist. After I load the AD plugin, I can use
QObject *plugin = loader.instance(); to get he QObject correctly. And I can use
Interface *ta = qobject_cast<Interface *>(plugin); to set the QObject to correct class.
The ta->pluginName() is functional. However, when I assign this QObject to the script engine, the debugger can't recognize this object. I also do a comparison with QPushButton. If I sent the QPushButton to the script engine. The debugger shows every properties. Please find the detail pic in attached.
I don't know why the my plugin properties can't work well after I send the plugin object to script engine but the QPushButton works just fine?

wysota
28th April 2011, 14:15
I think AD object is exist.
I mean does it exist in the script context. Looks like it does (based on the pics you provide) and it seems a function() function is there, so everything should work. What's the problem then?

SamT
28th April 2011, 15:04
Finally I got my script working.

I modify the implement of function()


void AD::function()
{
qDebug() << "This is a test";
}

and the script file

AD.function();

after runt the script, I could find my qDebug() window shows

This is a test
nan

The problem might go with the syntax error of my script file. Thanks for your help!