Results 1 to 5 of 5

Thread: Qt4 Plugins How-to

  1. #1
    Join Date
    Mar 2006
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Qt4 Plugins How-to

    Hi, men!

    I have one question and proposition:
    there are many people who want to do the plugin support in their applications, but many of them don't know how to do this (such as I am).

    Documentation from Trolltech is not enough, but many people in this forum were talking, asking about plugins.

    So, if you know how to do the plugin support, or have good documentation or want (and have time) to write it - please help!

    I think that many people will say THANKS later!

    thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 Plugins How-to

    But do you have problems with plugins in Qt or maybe you don't know how to write an application that will use plugins?

  3. #3
    Join Date
    Mar 2006
    Posts
    10
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 Plugins How-to

    Yes I don't know how to write an application that will use plugins.
    Also I don't how to write these plugins.

    I have some ideas, but...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 Plugins How-to

    Quote Originally Posted by Chaid
    Yes I don't know how to write an application that will use plugins.
    Also I don't how to write these plugins.
    You must start from designing interfaces --- one for each kind of a plugin. You will also need a set of interfaces through which the plugins will communicate with your application.

    For example if you want to have plugins that will allow users to export the document to some kind of a format. You would need something like this:
    Qt Code:
    1. class DocumentExporterInterface
    2. {
    3. public:
    4. // returns default extension for the file format known to plugin
    5. virtual QString defaultExtension() const = 0;
    6.  
    7. // returns a string that can be used in QFileDialog as a filter
    8. virtual QString filter() const = 0;
    9.  
    10. // stores document in a given file, returns true on success[1]
    11. virtual bool export( const Document& document, const QString& fileName ) const = 0;
    12. };
    13.  
    14. Q_DECLARE_INTERFACE( DocumentExporterInterface,
    15. "org.qtcentre.examples.DocumentExporterInterface/1.0" )
    To copy to clipboard, switch view to plain text mode 
    All of the plugins should implement this interface and when your application will want to use a plugin, it will have to use one of those three methods.

    On other hand plugins will see the application's data through Document interface --- it should define all methods required for successful export (but not more).

    Now when you have all interfaces, another things you need are hooks. Application must be aware that there user might want to use a plugin and it must be prepared for this. You must leave special places in your code in which you will pass the control to plugins (if they are present).

    In the above case, you will have to create a list of DocumentExporterInterface * that contains pointers to all loaded plugins that implement that interface and when user wants to export document, you will have to:
    • query all elements of that list and collect filter strings,
    • open QFileDialog and let user choose a file where document should be saved,
    • then you have to check which filter was chosen, find the corresponding plugin and invoke export() --- that's all.


    How to load plugins? You will need something like:
    Qt Code:
    1. QDir dir( QCoreApplication::applicationDirPath() + QDir::separator() + "plugins" );
    2. // or
    3. // QDir dir( QLibraryInfo::location( QLibraryInfo::PluginsPath );
    4.  
    5. QStringList files( dir.entryList( QDir::Files | QDir::Executable ) ); // get a list of files
    6. foreach( const QString& file, files ) {
    7. if( QLibrary::isLibrary( file ) ) { // if it looks like library, try to load it
    8. loader.setFileName( dir.absoluteFilePath( file ) );
    9. QObject *plugin = loader.instance();
    10. if( plugin != 0 ) { // if was loaded successfully, register the plugin
    11. registerPlugin( plugin );
    12. }
    13. }
    14. }
    15. ...
    16. void PluginManager::registerPlugin( QObject *plugin )
    17. {
    18. DocumentExporterInterface *exporter = qobject_cast< DocumentExporterInterface * >( plugin );
    19. if( exporter != 0 ) {
    20. // store exporter pointer somewhere
    21. }
    22. // check for another interface (plugins might implement multiple interfaces, for example exporter and importer)
    23. ...
    24. }
    To copy to clipboard, switch view to plain text mode 

    The last thing left is how to implement plugins. Well... it's pretty straightforward:
    Qt Code:
    1. #include "DocumentExporterInterface.h"
    2. #include "Document.h"
    3.  
    4. #include <QtPlugin>
    5. ...
    6. #incluse <QString>
    7.  
    8. class CsvExporterPlugin : public QObject, public DocumentExporterInterface // QObject must be first!
    9. {
    10. Q_OBJECT
    11. Q_INTERFACES( DocumentExporterInterface )
    12. public:
    13. QString defaultExtension() const;
    14. QString filter() const;
    15. bool export( const Document& document, const QString& fileName ) const;
    16. };
    17.  
    18. #ifdef QT_NO_DEBUG
    19. Q_EXPORT_PLUGIN2( csvexporter, CsvExporterPlugin )
    20. #else
    21. Q_EXPORT_PLUGIN2( csvexporter_debug, CsvExporterPlugin )
    22. #endif
    23.  
    24. QString CsvExporterPlugin::defaultExtension() const
    25. {
    26. return "csv";
    27. }
    28.  
    29. QString CsvExporterPlugin::filter() const
    30. {
    31. return "Comma separated values (*.csv)";
    32. }
    33.  
    34. bool CsvExporterPlugin::export( const Document& document, const QString& fileName ) const
    35. {
    36. ...
    37. }
    38.  
    39. // this line is needed, because we have a Q_OBJECT macro in a .cpp file
    40. #include "csvexporter.moc"
    To copy to clipboard, switch view to plain text mode 
    Important: First argument of Q_EXPORT_PLUGIN2 must match the name of a file with plugin (without extension).


    [1] Of course in real life it might be wiser to use exceptions to report problems, since there are a lot of things that might go wrong during export, but it's just an example.

  5. The following 2 users say thank you to jacek for this useful post:

    Chaid (8th July 2006), MTK358 (17th April 2011)

  6. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 Plugins How-to

    Sometimes it can be good to do the low-level yourself and not let Qt do it for you...

    If you want your plugins to interact with your app deeply you'll have to split it up into one wrapper executable and one core library which contains the classes definition. With such a structure you can drop the interface system recommended by the docs. Indeed you're not forced to use as pure virtual classes as interface since their definition will be present in the core library and thus accessible to the plugins.

    Another interesting thing I've tried with plugins is doing the loading/verification myself using QLibrary. The main advantage is that if an error occurs you know precisely which one (symbol lookup error, no instance function found, ...)

    In the example given by Jacek, going so deep would be needed if, and only if, the Document class is not pure virtual...
    Current Qt projects : QCodeEdit, RotiDeCode

  7. The following user says thank you to fullmetalcoder for this useful post:

    Chaid (8th July 2006)

Similar Threads

  1. Plugins support Help!
    By Chaid in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2006, 15:07
  2. Arthur Plugins demos and designer
    By antonio.r.tome in forum Installation and Deployment
    Replies: 4
    Last Post: 21st March 2006, 14:01
  3. How to reload widget plugins?
    By victorng in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2006, 23:27
  4. Plugins that use *.ui based classes
    By blackliteon in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2006, 09:33
  5. Plugins as small application
    By blackliteon in forum Qt Programming
    Replies: 4
    Last Post: 12th January 2006, 09:39

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.