PDA

View Full Version : Trouble with plugin system's documentation



niklas
23rd February 2009, 19:51
Hello
I'm trying to implement dynamic loading of plugins. But I ran into trouble.
The plugin compiles, but I cannot load it.

I've been reading http://doc.trolltech.com/4.3/plugins-howto.html over and over(section "The Lower-Level API: Extending Qt Applications"), but I don't get what's wrong!
Been taking a look at the examples echo server and plug and paint too.

I am confused about the checklist:

Making an application extensible through plugins involves the following steps:
Define a set of interfaces (classes with only pure virtual functions) used to talk to the plugins.
Use the Q_DECLARE_INTERFACE() macro to tell Qt's meta-object system about the interface.
Use QPluginLoader in the application to load the plugins.
Use qobject_cast() to test whether a plugin implements a given interface.
Writing a plugin involves these steps:

Declare a plugin class that inherits from QObject and from the interfaces that the plugin wants to provide.
Use the Q_INTERFACES() macro to tell Qt's meta-object system about the interfaces.
Export the plugin using the Q_EXPORT_PLUGIN2() macro.
Build the plugin using an suitable .pro file.
Under "Writing a plugin involves these steps" I'm wondering about the 3rd point. This is not used in the example code a few lines down.
Anyways I tried using it. I include <QtPlugin> and then use it in my plugin implementation.

I did a small example with my problem:
PluginInterface.h http://pastebin.com/m570b0214
PluginExample.h http://pastebin.com/m76fa2ca6
PluginExample.cpp http://pastebin.com/m699b7259
My .pro-file http://pastebin.com/m4bac5157

There probably is something I'm missing but I still get the feeling the documentation is wrong here? There's a semicolon missing after Q_DECLARE_INTERFACE in the interface example, and if you check out the documentation for 4.5 that line is missing completely even though it seems its still necessary to use in 4.5?

I'm using Qt 4.3 on Mac OS X. Would be very helpful if someone could help me out. I'm stuck :crying:

wysota
24th February 2009, 01:44
Please don't link to code pasted on 3rd party sites. It tends to disappear over time and the forum thread becomes practically useful. Instead use the attachment feature of the forum.

niklas
25th February 2009, 20:01
Please don't link to code pasted on 3rd party sites. It tends to disappear over time and the forum thread becomes practically useful. Instead use the attachment feature of the forum.

You're right. The files are attached to this post.

fullmetalcoder
25th February 2009, 20:20
Typical errors encountered on plugins load (may or may not be relevant in your case) :


the shared library export symbols that are not actually implemented leading to an error upon symbol resolution which on some systems prevent the shared library from being loaded at all (probably not relevant here given your code)
the plugin and the application that attempt to load are not compiled against the same version of Qt. You should pay particular attention to the build mode (release or debug). It must be the same for the app and the plugins AFAIK.

A good way to determine what's wrong is to load the shared lib by hand using QLibrary instead of QPluginLoader and to watch the console output to see if relevant messages are displayed. Now this won't load the actual plugin class for you but it should give an explaination if loading fails.

niklas
25th February 2009, 22:12
Thank you. I've gotten the plugin to load now. Can't figure out what was wrong though, but I dead a clean project in QtCreator and now it works. Have not been able to reproduce the problem.

But now I cannot call member functions on the object. Getting bus error :(

What Im doing is I create a QPluginLoader object for my plugin, make sure it is loaded, get the instance using instance() and then typecast it with qobject_cast to my interface class. Cannot see what I'm doing wrong.

See attached file which is a small test case.

fullmetalcoder
25th February 2009, 22:53
Thank you. I've gotten the plugin to load now. Can't figure out what was wrong though, but I dead a clean project in QtCreator and now it works. Have not been able to reproduce the problem.
Things like that happen sometimes. It usually comes from the way qmake dependency handling accross subdirs (or lack thereof). When you change a header file for your interface the plugin won't be recompiled unless that file appears in the HEADERS variable of the plugin project file.


But now I cannot call member functions on the object. Getting bus error :(

See attached file which is a small test case.
Actual console output and/or full code (including plugin) would help.

niklas
5th March 2009, 02:08
I am still stuck with this problem :(
Not getting bus error though, but the app unexpectedly quits.

My code is here: http://rafb.net/p/AvStNW98.html
Quitting when reaching line 22 where I call a function in the plugin.

The full console output from running the app is:
Starting /Users/niklas/code/test/PluginLoadingTest/PluginLoadingTest...
plugin loaded!

The program has unexpectedly finished.

niklas
5th March 2009, 02:15
I just now got it to work. Even though it seemed as if it was successfully loaded it was not. I checked if instance() was null and it was not, but the error string was not empty - it said the build key differed.
It differed even though I had recompiled both the plugin and the app since I updated to Qt 4.5.

I tried removing the plugin binary and it worked - solved my problem!
Wonder what I did wrong in QtCreator when trying to recompile...

wysota
6th March 2009, 23:07
Please stop storing your code on external sites.