Hello,

i try to load a plugin into my application. I have followed the "Text Area" example from the book "C++ GUI Programming with Qt4" but my application refuses to load the plugin.

That's how i have defined the plugin interface:

Qt Code:
  1. #ifndef INTERFACE_H
  2. #define INTERFACE_H
  3.  
  4. #include <QtPlugin>
  5.  
  6. class PluginInterface
  7. {
  8. public:
  9. virtual ~PluginInterface() { }
  10.  
  11. virtual QString pluginName();
  12. };
  13.  
  14. Q_DECLARE_INTERFACE(PluginInterface, "cops.demo.PluginInterface/1.0")
  15.  
  16. #endif
To copy to clipboard, switch view to plain text mode 

Here is the code of the plugin:

header-file:
Qt Code:
  1. #ifndef MYPLUGIN_H
  2. #define MYPLUGIN_H
  3.  
  4. #include <QString>
  5.  
  6. #include "myPlugin.h"
  7. #include "../pluginInterface.h"
  8.  
  9.  
  10. class MyPlugin : public QObject, public PluginInterface
  11. {
  12.  
  13. Q_OBJECT
  14. Q_INTERFACES(PluginInterface)
  15.  
  16. public:
  17. QString pluginName();
  18.  
  19. };
  20.  
  21. #endif
To copy to clipboard, switch view to plain text mode 

and source file:
Qt Code:
  1. #include <QtGui>
  2. #include <iostream>
  3.  
  4. #include "myPlugin.h"
  5.  
  6. QString MyPlugin::pluginName()
  7. {
  8. return "My Plugin Name";
  9. }
  10.  
  11. Q_EXPORT_PLUGIN2(myplugin, MyPlugin)
To copy to clipboard, switch view to plain text mode 

I can compile the plugin and get a libmyPlugin.so in my plugin directory.

Here you can see how i try to load the plugin in my application:

Qt Code:
  1. void MainWindow::loadPlugins()
  2. {
  3. QDir pluginDir(QApplication::applicationDirPath());
  4.  
  5. if (!pluginDir.cd("plugins"))
  6. return;
  7.  
  8. foreach (QString fileName, pluginDir.entryList(QDir::Files)) {
  9. std::cout << qPrintable(fileName) << "\n";
  10. QPluginLoader loader(pluginDir.absoluteFilePath(fileName));
  11. if (PluginInterface *interface = qobject_cast<PluginInterface *>(loader.instance())) {
  12. interfaces.append(interface);
  13. std::cout << qPrintable(interface->pluginName()) << "loaded \n";
  14. } else {
  15. std::cout << "not loaded!\n";
  16. }
  17. }
  18. }
To copy to clipboard, switch view to plain text mode 

The program finds the plugin ("std::cout << qPrintable(fileName) << "\n";" shows the filename of the plugin) but the if statement is always false so that i end in the else-arm.

Do you have an idea what's wrong here?

Thanks a lot!