December 2, 2010

puelocesar puelocesar
Lab Rat
25 posts

Problem with plugins

Page  
1

How do I use plugins? I created one, folowing the example on “Chapter 5: Writing an Extension Plugin”, but it’s not working! I’m getting the following error when I try to run my main.qml:

  1. ElipsePlugin/main.qml:7:5: Elipse is not a type
  2.          Elipse {
  3.           ^

I have a qmldir on the same dir as main.qml:

  1. plugin ElipsePlugin lib

I also have a lib folder with the following files: libelipseplugin.a e elipseplugin.dll

Other question, I created a project with a elipse plugin. But I want to use this plugin on another project. How to do that? I thought I should just put the plugin’s dll on a lib folder, and then write a qmldir declaring the plugin, but that didn’t worked!

19 replies

December 2, 2010

Kxyu Kxyu
Lab Rat
139 posts

try putting qmldir with “plugin ElipsePlugin” into plugon directory and importing directory in qml file

December 2, 2010

puelocesar puelocesar
Lab Rat
25 posts

How?

I tried renaming the ‘lib’ folder to ‘plugin’ as you said, changed the qmldir as you said, and added this to main.qml:

  1. import plugin 1.0

Now I’m getting this:

  1. main.qml:2:1: module "plugin" is not installed
  2.      import plugin 1.0
  3.  
  4.      ^

December 2, 2010

puelocesar puelocesar
Lab Rat
25 posts

By the way, this isn’t working either:

  1. import Qt 4.7
  2. import ElipsePlugin 1.0
  3.  
  4. Rectangle {
  5.     width: 100
  6.     height: 62
  7.  
  8.     Elipse {
  9.         color: "red"
  10.     }
  11. }

  1. Prototipacao/fontes/ElipsePlugin/main.qml:2:1: module "ElipsePlugin" is not installed
  2.      import ElipsePlugin 1.0
  3.  
  4.      ^

December 2, 2010

Bradley Bradley
Lab Rat
300 posts

Perhaps you need to tell the qmlviewer the path to your module with one of the command-line options:

  1.   -I <directory> ........................... prepend to the module import search path,
  2.                                              display path if <directory> is empty
  3.   -P <directory> ........................... prepend to the plugin search path

 Signature 

Nokia Certified Qt Specialist.

December 2, 2010

Kxyu Kxyu
Lab Rat
139 posts

you should write

  1. import "plugin"

December 3, 2010

xsacha xsacha
Lab Rat
502 posts

In my docs it is ‘Chapter 6: Writing an Extension Plugin’ so I hope you have the latest docs.

Anyway if you use a plugin as you intended, you will not need an ‘import’ line.
The qmldir should take care of this. The qmldir should be in the same directory though. Is this where it is?

If it’s still not working, you may not have defined the Elipse type correctly.
Check
qmlRegisterType<Elipse>(“ElipsePlugin, 1, 0, “Elipse”);
or
Q_EXPORT_PLUGIN2(elipseplugin, ElipsePlugin);

If this still doesn’t work, can you test if you are able to run the Chapter 6 example unmodified?

 Signature 

- Sacha

December 6, 2010

puelocesar puelocesar
Lab Rat
25 posts
Kxyu wrote:
you should write @import “plugin”@

I get this: Library import requires a version

December 6, 2010

puelocesar puelocesar
Lab Rat
25 posts

I just can’t make it to work.. It’s identical to Chapter 5: Writing an Extension Plugin (that works here), but my code isn’t working… Here is all the code:

My elipseplugin.h:

  1. #ifndef ELIPSEPLUGIN_PLUGIN_H
  2. #define ELIPSEPLUGIN_PLUGIN_H
  3.  
  4. #include <QtDeclarative/QDeclarativeExtensionPlugin>
  5.  
  6. class ElipsePlugin : public QDeclarativeExtensionPlugin
  7. {
  8.     Q_OBJECT
  9.  
  10. public:
  11.     void registerTypes(const char *uri);
  12. };
  13.  
  14. #endif // ELIPSEPLUGIN_PLUGIN_H

My elipseplugin.cpp:

  1. #include "elipseplugin.h"
  2. #include "elipse.h"
  3.  
  4. #include <QtDeclarative/qdeclarative.h>
  5.  
  6. void ElipsePlugin::registerTypes(const char *uri)
  7. {
  8.     qmlRegisterType<Elipse>(uri, 1, 0, "Elipse");
  9. }
  10.  
  11. Q_EXPORT_PLUGIN2(elipseplugin, ElipsePlugin)

My elipse.h:

  1. #ifndef ELIPSE_H
  2. #define ELIPSE_H
  3.  
  4. #include <QtDeclarative/QDeclarativeItem>
  5.  
  6. class Elipse : public QDeclarativeItem
  7. {
  8.     Q_OBJECT
  9.     Q_DISABLE_COPY(Elipse)
  10.  
  11.     Q_PROPERTY(QColor color READ color WRITE setColor)
  12.     Q_PROPERTY(QColor border_color READ border_color WRITE setBorderColor)
  13.     Q_PROPERTY(int border_size READ border_size WRITE setBorderSize)
  14.  
  15. public:
  16.     Elipse(QDeclarativeItem *parent = 0);
  17.     ~Elipse();
  18.  
  19.     QColor color() const;
  20.     void setColor(const QColor &color);
  21.  
  22.     QColor border_color() const;
  23.     void setBorderColor(const QColor &border_color);
  24.  
  25.     int border_size() const;
  26.     void setBorderSize(const int &border_size);
  27.  
  28.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
  29.  
  30. private:
  31.     QColor m_color;
  32.     QColor m_border_color;
  33.     int m_border_size;
  34. };
  35.  
  36. QML_DECLARE_TYPE(Elipse)
  37.  
  38. #endif // ELIPSE_H

My qmldir:

  1. plugin elipseplugin lib

My main.qml:

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 100
  5.     height: 62
  6.  
  7.     Elipse {
  8.         color: "red"
  9.     }
  10. }

My elipseplugin.pro:

  1.  TEMPLATE = lib
  2.  CONFIG += qt plugin
  3.  QT += declarative
  4.  
  5.  DESTDIR = lib
  6.  OBJECTS_DIR = tmp
  7.  MOC_DIR = tmp
  8.  
  9. # Input
  10. SOURCES += \
  11.     elipse.cpp \
  12.     elipseplugin.cpp
  13.  
  14. HEADERS += \
  15.     elipse.h \
  16.     elipseplugin.h
  17.  
  18. OTHER_FILES = qmldir \
  19.     main.qml

December 6, 2010

xsacha xsacha
Lab Rat
502 posts

Which part isn’t working? Still the QML?

How are you running the QML file? QMLViewer?

Does your directory look like this?

  1. .
  2. ..
  3. app.qml
  4. qmldir
  5. lib\elipseplugin.dll

P.S. Won’t change anything but should be using (new notation):
  1. import QtQuick 1.0

 Signature 

- Sacha

December 6, 2010

puelocesar puelocesar
Lab Rat
25 posts

Yes, the file structure is like you said, and I’m running with QMLViewer, but the plugin from the examples works fine with QMLViewer.

December 6, 2010

xsacha xsacha
Lab Rat
502 posts

Do you remember what you changed?
You could try running a `diff` between your directory and the original plugin.

 Signature 

- Sacha

December 6, 2010

Bradley Bradley
Lab Rat
300 posts

You didn’t provide the implementation of Elipse (elipse.cpp), but a common mistake (at least for me) is clearing the QGraphicsItem::ItemHasNoContents flag which is set by QDeclarativeItem.

Works for me with the files above, plus these:

main.qml

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 100
  5.     height: 62
  6.  
  7.     Elipse {
  8.         anchors.fill: parent
  9.         color: "red"
  10.     }
  11. }

elipse.cpp

  1. #include "elipse.h"
  2.  
  3. #include <QtGui/QPainter>
  4.  
  5. Elipse::Elipse(QDeclarativeItem *parent) :
  6.     QDeclarativeItem(parent)
  7. {
  8.     setFlag(QGraphicsItem::ItemHasNoContents, false);
  9. }
  10.  
  11. Elipse::~Elipse() {}
  12.  
  13. QColor Elipse::color() const { return m_color; }
  14. void Elipse::setColor(const QColor &color) { m_color = color; }
  15.  
  16. QColor Elipse::border_color() const { return m_border_color; }
  17. void Elipse::setBorderColor(const QColor &border_color) { m_border_color = border_color; }
  18.  
  19. int Elipse::border_size() const { return m_border_size; }
  20. void Elipse::setBorderSize(const int &border_size) { m_border_size = border_size; }
  21.  
  22. void Elipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  23. {
  24.     painter->setPen(QPen(m_border_color, m_border_size));
  25.     painter->setBrush(m_color);
  26.     painter->drawEllipse(boundingRect());
  27. }

 Signature 

Nokia Certified Qt Specialist.

January 10, 2011

jalomic jalomic
Lab Rat
16 posts

I have same trouble. Can anybody tell me stap-by-stap “howto” ?

September 14, 2011

remy_david remy_david
Lab Rat
21 posts

I have the same troubles.
For me the main problem is to understand how to correctly import the plugin to the application.

If the plugin is in another location than the application, how do you tell the build system were to look ?
Since I have several targets (simulator, device…) + release (debug, release…), I have different version of my plugin library in different folders. How does the build system makes the difference ?

September 14, 2011

xsacha xsacha
Lab Rat
502 posts

In your .pro you can specify it depending on target and other such variables.

symbian: { …}
win32: { … }

 Signature 

- Sacha

Page  
1

  ‹‹ Use an existing SQLite DB with QML      QML and frame rate ››

You must log in to post a reply. Not a member yet? Register here!