PDA

View Full Version : QT plugins: a plugin that is a GUI



trallallero
28th October 2009, 13:50
I don't understand how to create an empy application that only loads a plugin that is a GUI itself.
I cannot make it work ... :confused:

Actually I have a main.cpp that creates and executes a QApplication

int main(int argc, char *argv[])
{
MainApp app(argc, argv);
return app.exec();
}



class MainApp : public QApplication
{
Q_OBJECT

public:
MainApp(int &argc, char **argv);
~MainApp();

private:
void loadPlugins();

QDir pluginsDir;
QStringList pluginFileNames;
}

when MainApp is created it calls loadPlugins:

MainApp::MainApp(int &argc, char **argv) :
QApplication(argc, argv)
{
loadPlugins();
}


QPluginLoader loader(..) fails as I get:

ERROR !!! loader.isLoaded(): <0> - loader.errorString(): <Unknown error>

Does anybody know something about it ?
Thanks.

wysota
29th October 2009, 09:51
What is the contents of your loadPlugins() method?

trallallero
29th October 2009, 10:22
No it's ok now, I've forgotten to write that the problem was this one:
http://www.qtcentre.org/forum/f-qt-programming-2/t-problem-with-guis-25178.html

in few words the gui I've created could not start properly for some stupid problem (the more the problem is stupid and near you, the more far you search for a solution :D) so the plugin was not loaded.

But the strange thing is that I had to remove the check (if loader.isLoaded()) as it returned false also after having correted the error.

anyway, my loadPlugins is the following:


void MainApp::loadPlugins()
{
pluginsDir = QDir(qApp->applicationDirPath());
pluginsDir.cd("plugins");

foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));

//if ( ! loader.isLoaded() )
//{
// puts("ERROR: plugin not loaded !");
// return;
//}

QObject *plugin = loader.instance();
if (plugin)
{
BaseInterface * b = qobject_cast<BaseInterface*>(plugin);
if (b)
{
puts("Plugin loaded - setting config file ...");
b->SetConfigFileName("objects.xml");
puts("Starting plugin ...");
b->Start();
}
else
{
puts("ERROR: Could not convert Plugin !!! ");
}
pluginFileNames += fileName;
}
}
}

Thanks :)

TMan
29th October 2009, 10:48
I've never programmed with plugins, but this may be the reason your check fails:

bool QPluginLoader::load ()

Loads the plugin and returns true if the plugin was loaded successfully; otherwise returns false. Since instance() always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the plugin loaded in advance, in which case you would use this function.

trallallero
29th October 2009, 10:59
Uhm ... probably you're right but look at qt's doc (4.5):


QPluginLoader::QPluginLoader ( const QString & fileName, QObject * parent = 0 )

Constructs a plugin loader with the given parent that will load the plugin specified by fileName.

To be loadable, the file's suffix must be a valid suffix for a loadable library in accordance with the platform, e.g. .so on Unix, - .dylib on Mac OS X, and .dll on Windows. The suffix can be verified with QLibrary::isLibrary().

I understand that the plugin is loaded by the constructor so, if it fails here, why does it work in the method instance() :confused:

wysota
29th October 2009, 11:11
Either way the plugin object shouldn't inherit QWidget. It should create one when asked for it. I suspect this is not the case here.

trallallero
29th October 2009, 11:39
That was exactly my problem explained in the other thread.
Acutally my plugin's main class inherits from QGraphicsView

class KofaGuiDev : public QGraphicsView
...
as I have to paint all by my self ... I cannot use the normal qt's objects because when they blink (using stylesheet) the cpu's usage is around 90% :crying: