PDA

View Full Version : Qlibrary



rianquinn
30th January 2006, 14:09
Ok, I have an interesting problem. Looking for as many ideas as possible here. I am building an application that uses plugins. The plugins add additional functionality to the main application without the application having any knowledge of its existance. The user can setup configuration files that specify what the app should do if a particular event or action is created (i.e. clicking a menu item, pressing a key combo). How would you guys allow the main application call a procedure inside the plugin with only a QObject pointer (given to you by the QPluginLoader) and a string consisting of the function to call.

I have three different idea, and I am not sure if any will even work.

Using QLibrary (this would be my last resort seeing as how these functions would have to be externed as "C" functions.

Using QActions (not sure how I would do this, but I bet there is a way).

Using Signals and Slots somehow (once again not sure).

Currently, I am in the proof of concept phase of my project, and am looking for the cleanest ways to do this (that actually work). I might also be thinking about this in the wrong direction, so any suggestions are welcome.

So to some things up here, how to you call a function from a library with nothing more than a QObject pointer, and a string?

Thanks Everyone,
Rian

Mike
30th January 2006, 14:43
Well I think Qt has great plugin support. Take a look at this sample:
Main App:
http://doc.trolltech.com/4.1/tools-plugandpaint.html
It's plugins:
http://doc.trolltech.com/4.1/tools-plugandpaintplugins-basictools.html

1) I would imagine that you would have to define interfaces that can be implemented by those plugins.
2) The plugins should be placed in a known directory like done e.g. by Eclipse.
3) Your app should offer a config dialog that would load all plugins and offer some details about each plugin to the user. (Like Name, What it does, ...)
4) Let the user check the one's he would like to use with your app
5) Save that configuration
6) When your app starts, read the config and load the user selected plugins.

Qt offers the QPluginLoader class for doing exactly that.

If your interface defines methods, like getPluginName, getPluginDescription, getPluginCategory... then you would even be able to group those plugins in your app's config dialog. Or you group the plugins, based on the interface they implement from your app...

there are lot's of possibilities ...

rianquinn
30th January 2006, 17:12
This part I have working very nicely.The problem is. lets say a menu plugin has a function called test. How do you call test with nothing move than a string, "test" and a Object*

orb9
31st January 2006, 00:49
How do you call test with nothing move than a string, "test" and a Object*

If test() is a slot and the Object* is named foo:

QMetaObject::invokeMethod( foo, "test" );

Hope this helps.

rianquinn
1st February 2006, 03:28
That is a huge help. I had no idea that this even existed. Thanks, you probably saved the day!!!

wysota
4th February 2006, 13:23
You can always have a method that returns a QAction * object (or better yet a list of QAction* objects). Your plugin doesn't have to return QObject * objects. You can create an "interface" containing custom methods too. Take a look at the docs about plugins in Qt4 for examples.