Qt plug-in mechanism

Source: Internet
Author: User
Tags ibm db2 qt designer

Qt plug-in mechanism (transfer)

There are two types of plug-in-related APIs. A function used to expand QT itself, such as custom database driver, image format, text codec, custom Shard, and so on.
Higher-level API. Another function extension for applications is called lower-level.
API. The former is based on the latter. The latter type is discussed here, that is, the lower-level API used to extend the application.

Steps to enable the application to support extension:
1. Define an interface set (only classes with pure virtual functions) for communication with the plug-in.
2. Use the macro q_declare_interface () to notify the QT object system.
Self-built http://yanboo.ycool.com/post.2749491.html

Qt plug-in mechanism

Yanboo was published on 16:42:24
Qt

1 q_declare_interface (brushinterface, "com. trolltech. plugandpaint. brushinterface/1.0 ")

3. The application uses qpluginloader to load the plug-in.
4. Use macro qobject_cast () to determine whether an interface is implemented by a plug-in.

1 qobject * OBJ = new qtimer;
2 qtimer * timer = qobject_cast (OBJ );

 

Steps for writing a plug-in:
1. Declare the plug-in class, which inherits from the qobject and the interface that the plug-in wants to implement.
2. Use macro q_interfaces () to notify the QT object system.

1 class basictoolsplugin: Public qobject,
2 Public brushinterface,
3 Public shapeinterface,
4 Public filterinterface
5 {
6 q_object
7 q_interfaces (brushinterface shapeinterface filterinterface)
8 public:
9...
10 };

3. Use macro q_export_plugin2 () to export the plug-in.

1 q_export_plugin2 (pluginname, classname)

4. Build the plug-in with the appropriate. Pro file.

 

The following Code declares an interface class:

1 class filterinterface
2 {
3 public:
4 virtual ~ Filterinterface (){}
5 virtual qstringlist filters () const = 0;
6 virtual qimage filterimage (const qstring & filter, const qimage & image, qwidget * parent) = 0;
7 };
8
9 q_declare_interface (filterinterface, "com. trolltech. plugandpaint. filterinterface/1.0 ")

Here is the definition of the plug-in class implementing this interface:

1 # include
2 # include
3 # include
4
5 # include

6
7 class extrafiltersplugin: Public qobject, public filterinterface
8 {
9 q_object
10 q_interfaces (filterinterface)
11
12 public:
13 qstringlist filters () const;
14 qimage filterimage (const qstring & filter, const qimage & image,
15 qwidget * parent );
16 };

 

The document of the example plug & paint explains this process in detail. For more information about QT designer, see creating M.
Widgets for Qt designer. Echo plugin example is a detailed example of how to implement the extended QT application.

Loading and verifying plugins dynamically

When loading the plug-in. The QT library has some sound checks to determine whether the plug-in can be loaded and used. This allows you to install multiple versions and QT library configurations at the same time.

· The plug-in linked to the QT library with a higher primary version and (or) minor version cannot be loaded by the primary version and/or libraries with a lower version.

Principle: A plug-in using the new QT library may use new features not available in the old version. Trolltech has a policy that adds new features and APIs only for minor version updates. This is why this test only looks at the minor version, not the patch number.

· The QT library and all plug-ins are associated with one join keyword. The concatenation keywords in the QT library are compared with those in the plug-in. If they are consistent, the plug-in is loaded. If the concatenation keyword does not match, the QT library rejects the plug-in.

Principle: See the following description of the couplet keyword.

It is important to make sure that the plug-in and application use the same configuration when compiling plug-ins to expand the application. This means that if the application is compiled in the release mode, the plug-in should also be in the release mode.

If you configure QT to both Debug and release modes, but only compile the application in release mode, make sure that your plug-in is also compiled in release mode.
. By default, if the debug compilation of QT is available, the plug-in is compiled only in debug mode. To force the plug-in to compile in release mode, add the following to the project:

Config + = release

This ensures that the plug-in is compatible with the Library version used in the application.

The build key

During plug-in loading, QT checks that the keywords of each plug-in must match their own to ensure that the plug-in is installed with a compatible one. No plug-in that does not match the plug-in will be loaded.

The concatenation keyword contains the following information:

· Architecture, operating system and compiler.

Principle: when different versions of the same compiler do not generate Binary compatible code, the version of the compiler is also reflected in the concatenation keyword.

· QT library configuration. This configuration is a list of missing features in the library, because the APIS corresponding to these features are unavailable in the library.

Principle: different configurations of two QT databases of the same version are not Binary compatible. The QT library that loads the plug-in uses this (missing) feature list to determine whether the plug-in is Binary compatible.

Note:
In this case, the plug-in can use the features available in two different configurations. However, developers who write plug-ins need to know which features are used in their plug-ins and QT public tool classes.
During plug-in loading, the QT Library requires complex features and dependent query confirmation. These requirements impose unnecessary burdens on developers and increase the system overhead for loading plug-ins. To reduce development time and
You can use the simple string comparison of the couplet keyword.

· Optional. You can specify an additional string optionally, an extra string may be specified on the configure script command line in the script command line.

Principle: When publishing the binary code of the QT library with an application, this provides developers with a method to compile the plug-in. In this way, the written plug-in can only be loaded by the library linked to the plug-in.

In order to debug, you may need to disable the federated keyword verification function. This can be achieved by setting the environment variable qt_no_plugin_check of the application environment to a non-zero value.

Static plugins

The plug-in can be statically linked to the application. If you have created a static version of QT, this is only an option to include the predefined plug-in of QT.

When compiled as a static library, QT provides the following static plug-ins:

Plugin name
Type
Description
 
Qtaccessiblecompatwidgets
Accessibility
Accessibility for Qt 3 Support Widgets
 
Qtaccessiblewidgets
Accessibility
Accessibility for Qt Widgets
 
Qdecorationdefault
Decorations (qtopia)
Default Style
 
Qdecorationwindows
Decorations (qtopia)
Windows style
 
Qgif
Image formats
GIF
 
Qjpeg
Image formats
JPEG
 
Qmng
Image formats
MNG
 
Qimsw_multi
Input Methods (qtopia)
Input Method switcher
 
Qwstslibmousehandler
Mouse drivers (qtopia)
Tslib mouse
 
Qgfxtransformed
Graphic drivers (qtopia)
Transformed Screen
 
Qgfxvnc
Graphic drivers (qtopia)
VNC
 
Qscreenvfb
Graphic drivers (qtopia)
Virtual Frame Buffer
 
Qsqldb2
SQL driver
IBM DB2
 
Qsqlibase
SQL driver
Borland InterBase
 
Qsqlite
SQL driver
SQLite Version 3
 
Qsqlite2
SQL driver
SQLite Version 2
 
Qsqlmysql
SQL driver
MySQL
 
Qsqloci
SQL driver
Oracle (OCI)
 
Qsqlodbc
SQL driver
Open Database Connectivity (ODBC)
 
Qsqlpsql
SQL driver
PostgreSQL
 
Qsqltds
SQL driver
Sybase Adaptive Server (TDS)
 
Qcncodecs
Text codecs
Simplified Chinese (People //'s Republic of China)
 
Qjpcodecs
Text codecs
Japan
 
Qkrcodecs
Text codecs
Korean
 
Qtwcodecs
Text codecs
Traditional Chinese (Taiwan)

To statically link these plug-ins, you must use macro q_import_plugin () in your application and use qtplugin to add the required plug-ins to your compilation. For example, in Main. cpp:

# Include

# Include

 

Q_import_plugin (qjpeg)

Q_import_plugin (qgif)

Q_import_plugin (qkrcodecs)

 

Int main (INT argc, char * argv [])

{

Qapplication app (argc, argv );

...

Return app.exe C ();

}

Use the following entries in the. Pro file of the application:

Qtplugin + = qjpeg/

Qgif/

Qkrcodecs

You can also create your own static library as follows:

1. Add config + = static to the. Pro file of the plug-in.

2. Use macro q_import_plugin () in the application ().

3. Use libs to link the static library in the. Pro file of the application.

See the example plug & paint and related plug-in basic tools for details.

The plugin Cache

To accelerate plug-in loading and validation, information collected during plug-in loading is cached in qsettings. This includes information about whether the plug-in is successfully loaded, so that subsequent loading operations do not need to try again
Contains invalid plug-ins. However, if a plug-in's last modified
When the timestamp is modified, the cache entry of the plug-in is invalid and the plug-in will be reloaded regardless of the value in the cache entry, and the cache entry itself will be replaced by a new value.

This also means that the timestamp must be updated after each plug-in or any dependent Resource (such as the shared library) is updated, because the dependent resource may affect the loading result of a plug-in.

Sometimes, when developing a plug-in, you need to remove entries from the plug-in cache. Because QT uses qsettings to manage the plug-in cache, the plug-in location depends on the platform. For more information about each platform, see the qsettings documentation.

For example, in windows, these entries are stored in the registry. The path of each plug-in starts with one of the following two strings:

HKEY_CURRENT_USER/software/trolltech/organizationdefaults/Qt plugin cache 4.2.debug

HKEY_CURRENT_USER/software/trolltech/organizationdefaults/Qt plugin cache 4.2.false

Debugging plugins

Many problems may affect the normal operation of the plug-in the application. Many of them are related to different plug-ins and application creation methods, which usually occur in different systems and processes.

The following table describes the common causes of problems that developers encounter when creating plug-ins:

Problem
Cause
Solution
 
When the application opens the plug-in, the plug-in fails to be loaded and no prompt is displayed. The help | about plugins dialog box of QT designer shows the plug-in library, but there is no plug-in here.
Applications and plug-ins are created in different modes.
Use the same creation information; or add debug_and_release to the config variable in their project file to create the plug-in debug and release modes.
 
Failed to replace invalid plug-ins with valid plug-ins
The inserted entry in the plug-in cache shows that the original plug-in cannot be uninstalled, which causes QT to ignore the replacement.
Make sure that the timestamp of the plug-in is updated or the entry in the plug-in cache is deleted.

You can also use the qt_debug_plugins environment variable to obtain the diagnostic information of each plug-in that you are trying to load from QT. Set the value of this variable to non-zero in the running environment of the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.