Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: How to create a plugin

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to create a plugin

    I created a test application that searches for plugins in a directory, and for every one calls their getWidget() function and adds the widget to a layout.

    I have a .h file that contains the plugin interface, including the getWidget() method.

    How do I create the plugins (using Qt Creator)? I'm completely clueless.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    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 a suitable .pro file.
    OK, what's a suitable .pro file?

  4. #4
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    I figured it out, but when I compile it says that "myplugin.h:1:17: fatal error: QtGui: No such file or directory".

    ?????????????????????????

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    Here's a pro file from the examples:

    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += plugin static
    3. INCLUDEPATH += ../..
    4. HEADERS = basictoolsplugin.h
    5. SOURCES = basictoolsplugin.cpp
    6. TARGET = $$qtLibraryTarget(pnp_basictools)
    7. DESTDIR = ../../plugandpaint/plugins
    8.  
    9. # install
    10. target.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaint/plugins
    11. sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS basictools.pro
    12. sources.path = $$[QT_INSTALL_EXAMPLES]/tools/plugandpaintplugins/basictools
    13. INSTALLS += target sources
    To copy to clipboard, switch view to plain text mode 

    The file is explained at the end of this webpage:
    http://doc.qt.nokia.com/4.6/tools-pl...asictools.html

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    Quote Originally Posted by MTK358 View Post
    I figured it out, but when I compile it says that "myplugin.h:1:17: fatal error: QtGui: No such file or directory".

    ?????????????????????????
    Did you use "INCLUDEPATH +=" (note the +) in your pro file?

  7. #7
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    static? I thought it had to be dynamic!

  8. #8
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    INCLUDEPATH += what?

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    Plugins do not have to be dynamic. Usually, plugins are a special kind of library, not used in other programs.
    But, if you have the need to base other plugins on a certain base plugin, than you find it much easier when they are shared instead of static. Well, that's what I think.

    Forget about the includepath. I thought you might have added such a line in your .pro file. And if you do not add the +, it will overwrite all the include paths resulting in some includes not being found.
    But I guess in your case the problem is somewhere else.

    Can you post the code you have please?

  10. #10
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    myplugin.h

    Qt Code:
    1. #include <QLabel>
    2.  
    3. #include "/home/michael/Projects/QPanel/qpanelappletinterface.h"
    4.  
    5. class ClockPlugin : public QObject, public QPanelAppletInterface
    6. {
    7. Q_OBJECT
    8. Q_INTERFACES(QPanelAppletInterface)
    9.  
    10. public:
    11. MyPlugin();
    12. QWidget* getWidget();
    13. void showPreferencesDialog();
    14.  
    15. private:
    16. QLabel* label;
    17. };
    To copy to clipboard, switch view to plain text mode 

    myplugin.cpp

    Qt Code:
    1. #include "myplugin.h"
    2.  
    3. MyPlugin::MyPlugin()
    4. {
    5. label = new QLabel("Time goes here");
    6. }
    7.  
    8. QWidget* MyPlugin::getWidget()
    9. {
    10. return label;
    11. }
    12.  
    13. void MyPlugin::showPreferencesDialog()
    14. {
    15. // no preferences
    16. }
    To copy to clipboard, switch view to plain text mode 

    myplugin.pro

    Qt Code:
    1. ######################################################################
    2. # Automatically generated by qmake (2.01a) Fri Aug 27 08:29:32 2010
    3. ######################################################################
    4.  
    5. TEMPLATE = lib
    6. CONFIG = plugin
    7. TARGET =
    8. DEPENDPATH += .
    9. INCLUDEPATH += .
    10.  
    11. # Input
    12. HEADERS += myplugin.h
    13. SOURCES += myplugin.cpp
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    I'm not sure about the QtGui error at the moment.

    Check the source code, your class is called ClockPlugin while everywhere else it is called MyPlugin.

    You also need to set a target.
    And you need to call that target in the Q_EXPORT_PLUGIN2 macro. Otherwise you're not exporting symbols and the plugin will not be able to be loaded.

    Example:
    myplugin.pro:
    Qt Code:
    1. TARGET = $$qtLibraryTarget(mysuperplugin)
    To copy to clipboard, switch view to plain text mode 

    myplugin.cpp: (at the very end of the file)
    Qt Code:
    1. Q_EXPORT_PLUGIN2(mysuperplugin, MyPlugin)
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    What is "mysuperplugin"?

  13. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    It's the library name of the plugin. You can call it whatever you want.

  14. #14
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    OK, but what about the fact that QtGui is not found?

  15. #15
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    Ohh yes, now I see it.

    Add QT += gui to your pro file.

    Edit: On the other hand, this should be default, at least for the app template.

  16. #16
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    Didn't help.

  17. #17
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    This is weird. I don't see the problem. QMake should find it automatically.

  18. #18
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    Qt Code:
    1. $ ls
    2. Makefile myplugin.cpp myplugin.h myplugin.pro
    3. $ cat myplugin.pro
    4. ######################################################################
    5. # Automatically generated by qmake (2.01a) Fri Aug 27 08:29:32 2010
    6. ######################################################################
    7.  
    8. QT += gui
    9. TEMPLATE = lib
    10. CONFIG = plugin
    11. TARGET = $$qtLibraryTarget(myplugin)
    12. DEPENDPATH += .
    13. INCLUDEPATH += .
    14.  
    15. # Input
    16. HEADERS += myplugin.h
    17. SOURCES += myplugin.cpp
    18. $ qmake
    19. $ make
    20. g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -fPIC -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I. -o myplugin.o myplugin.cpp
    21. In file included from myplugin.cpp:1:0:
    22. myplugin.h:1:17: fatal error: QtGui: No such file or directory
    23. compilation terminated.
    24. make: *** [myplugin.o] Error 1
    To copy to clipboard, switch view to plain text mode 

  19. #19
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to create a plugin

    As you can see in the following line:

    Qt Code:
    1. g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -fPIC -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I. -o myplugin.o myplugin.cpp
    To copy to clipboard, switch view to plain text mode 

    The following include paths are at least missing:

    Qt Code:
    1. -I/usr/include/.../QtCore -I/usr/include/.../QtGui
    To copy to clipboard, switch view to plain text mode 
    These are pseudo paths, I don't know where they are on your system.

    This is mighty strange as, like I said, when QT += gui and QT += core (which are default, you don't generally have to add those manually to the .pro file) are added to your .pro file, the include paths should be set by qmake.

    One thing I can think of is to manually delete the Makefile, and try qmake and make again. But I guess this won't solve it either.

    Edit: try this too:

    Qt Code:
    1. CONFIG += plugin
    To copy to clipboard, switch view to plain text mode 
    Note the +
    When you do not add the +, you erase all the configs

  20. #20
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to create a plugin

    Another error:

    $ make
    /usr/bin/qmake -unix -o Makefile myplugin.pro
    g++ -c -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_PLUGIN -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o myplugin.o myplugin.cpp
    myplugin.cpp: In function ‘QObject* qt_plugin_instance()’:
    myplugin.cpp:18:1: error: cannot allocate an object of abstract type ‘MyPlugin’
    myplugin.h:6:1: note: because the following virtual functions are pure within ‘MyPlugin’:
    /home/michael/junk/QPanel/qpanelappletinterface.h:13:18: note: virtual void QPanelAppletInterface::showSettingsDialog()
    make: *** [myplugin.o] Error 1
    The interface:

    Qt Code:
    1. #ifndef QPANELAPPLETINTERFACE_H
    2. #define QPANELAPPLETINTERFACE_H
    3.  
    4. #include <QtPlugin>
    5.  
    6. class QPanelAppletInterface
    7. {
    8. public:
    9. virtual ~QPanelAppletInterface() {}
    10.  
    11. virtual QWidget *getWidget() = 0;
    12.  
    13. virtual void showSettingsDialog() = 0;
    14. };
    15.  
    16. Q_DECLARE_INTERFACE(QPanelAppletInterface, "com.trolltech.PlugAndPaint.QPanelAppletInterface/1.0")
    17.  
    18. #endif // QPANELAPPLETINTERFACE_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 11th April 2013, 11:55
  2. Plugin and shared class between plugin and application
    By wishper in forum Qt Programming
    Replies: 7
    Last Post: 23rd August 2010, 18:00
  3. Create plugin in Visual Studio
    By estel in forum Installation and Deployment
    Replies: 2
    Last Post: 10th March 2010, 00:40
  4. Replies: 4
    Last Post: 1st May 2009, 12:00
  5. Replies: 7
    Last Post: 23rd August 2007, 13:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.