PDA

View Full Version : Dynamic load QT Plugin - symbol lookup error



qlands
2nd May 2010, 18:22
Hi, I'm writing a program that uses plugins to extend it. I followed the examples in QT. But when I run the application it gives the error: symbol lookup error: /home/cquiros/MyApplication/plugins/libtnkbasicplugins.so: undefined symbol: _ZN16tnkBasicPolygons13createPolygonE7QString.

From the error I can see that the problem is in the function createPolygon. This fuction is private for the plugin.

If I remove the private function createPolygon. The application works just fine.

Any idea what I am doing wrong?

Many thanks.

Here is the code:

tnkinterfaces.h


#ifndef TNKINTERFACES_H
#define TNKINTERFACES_H

#include <QtPlugin>
#include "tnkdelegatorfactory.h"

class QStringList;
class QIcon;

class tnkPolygonInterface
{
public:
virtual ~tnkPolygonInterface() {}
virtual QStringList pluginNames() const = 0;
virtual QList<QIcon> pluginIcons() = 0;
virtual tnkPolygonSymbol* constructNewItem(const QString &pluginName, bool inDesignMode) = 0;
virtual tnkPolygonSymbol* loadItemFromData(const QString &pluginName, QByteArray &data, bool inDesignMode) = 0;
};

Q_DECLARE_INTERFACE(tnkPolygonInterface,"com.personal.MyApplication.tnkPolygonInterface/1.0")

#endif // TNKINTERFACES_H


tnkbasicplugins.h


#ifndef TNKBASICPLUGINS_H
#define TNKBASICPLUGINS_H

#include <QtGui>
#include <QObject>
#include <src/tnkinterfaces.h>

class tnkBasicPolygons : public QObject, public tnkPolygonInterface
{
Q_OBJECT
Q_INTERFACES(tnkPolygonInterface)
public:
QStringList pluginNames() const;
QList<QIcon> pluginIcons();
tnkPolygonSymbol* constructNewItem(const QString &pluginName, bool inDesignMode);
tnkPolygonSymbol* loadItemFromData(const QString &pluginName, QByteArray &data, bool inDesignMode);
private:
QPolygonF createPolygon(QString polygonType);
};

#endif // TNKBASICPLUGINS_H


in the .h createPolygon(QString polygonType) is private and not declared in the interfaces.

tnkbasicplugins.cpp


#include "tnkbasicplugins.h"


QStringList tnkBasicPolygons::pluginNames() const
{
return QStringList() << tr("Box");
}


QList<QIcon> tnkBasicPolygons::pluginIcons()
{
QList<QIcon> result;
QImage image;
QPainter painter(&image);

// Create a 32x32 representation of the box
// and appends it to the list of icons
QGraphicsScene scene;
scene.setSceneRect(QRectF(0,0,101,101));
scene.addPolygon(createPolygon("Box"));
scene.render(&painter);
image.scaledToHeight(32);
image.scaledToWidth(32);
result.append(QIcon(QPixmap::fromImage(image)));


return result;
}

tnkPolygonSymbol* tnkBasicPolygons::constructNewItem(const QString &pluginName, bool inDesignMode)
{
tnkPolygonSymbol *result;
result = new tnkPolygonSymbol(0,0,inDesignMode); //Must change
if (pluginName == tr("Box"))
{
QPolygonF sqPolygon;
sqPolygon << QPointF(-100, -100) << QPointF(100, -100)
<< QPointF(100, 100) << QPointF(-100, 100)
<< QPointF(-100, -100);
result->setPolygon(createPolygon("Box"));

//Need to see how to save data!

}
return result;
}

tnkPolygonSymbol* tnkBasicPolygons::loadItemFromData(const QString &pluginName, QByteArray &data, bool inDesignMode)
{
tnkPolygonSymbol *result;
result = new tnkPolygonSymbol(0,0,inDesignMode); //Must change
if (pluginName == tr("Box"))
{

result->setPolygon(createPolygon("Box"));

//Need to see how to save data!

}
return result;
}


QPolygonF createPolygon(QString polygonType)
{
QPolygonF sqPolygon;
if (polygonType == "Box")
{
sqPolygon << QPointF(-100, -100) << QPointF(100, -100)
<< QPointF(100, 100) << QPointF(-100, 100)
<< QPointF(-100, -100);
}
return sqPolygon;
}

Q_EXPORT_PLUGIN2(tnk_basicpolygons, tnkBasicPolygons)

SixDegrees
2nd May 2010, 18:52
What happens if you make createPolygon() protected rather than private?

qlands
2nd May 2010, 19:01
The same error happens whether is private, protected or public.

SixDegrees
2nd May 2010, 19:07
It sounds like the loader can't find your plugin. Are you checking results to see if it located it, loaded it and cast it to the correct type?

qlands
2nd May 2010, 19:37
oh Man!!!! After maybe 8 hours in this crazy bug I just realized that I declared in the cpp:

QPolygonF createPolygon(QString polygonType) and not QPolygonF tnkBasicPolygons::createPolygon(QString polygonType)

And that was the error!..

Many thanks for the replies!