Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PySide2 / PyQT5 : Using QPluginLoader to import a QT C++ plugin



  • Hi

    I try to import a QT C++ plugin into Python using PySide2 / PyQT5.

    The dll plugin works fine when using QT C++ QPluginLoader because I can cast the QObject with qobject_cast :
    ope_int = qobject_cast<Operations_Interface *>(plugin);

    The dll plugin is loaded when using PySide2 or PyQT5 but I don't know how to use qobject_cast in Python to cast to a class interface.

    An idea

    Thanks



  • @bob40
    First, which plugin are you trying to use and could you give us a minimal example of your C++/Python code please (just so we can get a handle on what it is you're doing).

    Assuming it's a C++ Qt plugin, I think you would probably have to create a SIP (for PyQt5) wrapper around the C++ interface for the plugin and then use sip.cast() to cast the QObject returned from QPluginLoader to a Python representation of the type the plugin returns.

    Hope this helps :o)



  • Thank you for your help :

    1/ The .h interface :

    #include <QObject>
    #include <QString>
    
    class Operations_Interface
    {
    public:
        Operations_Interface() = default;
        virtual ~Operations_Interface() = default;
        virtual int addition (int a, int b) = 0;
        virtual int soustraction (int a, int b) = 0;
    };
    
    
    QT_BEGIN_NAMESPACE
    
    #define Operations_Interface_iid "Chargement_Plugin.Operations_Interface"
    
    Q_DECLARE_INTERFACE(Operations_Interface, Operations_Interface_iid)
    QT_END_NAMESPACE
    

    2/ The header of the class in the plugin

    #ifndef OPERATION_PLUGIN_H
    #define OPERATION_PLUGIN_H
    
    #include <QObject>
    #include <QtPlugin>
    #include "operations_interface.h"
    
    class Operations_plugin : public QObject, Operations_Interface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID "Chargement_Plugin.Operations_Interface" FILE "Operations_plugin.json")
        Q_INTERFACES(Operations_Interface)
    
    public:
        Operations_plugin() {};
        int addition (int a, int b) override;
        int soustraction (int a, int b) override ;
    };
    
    #endif
    

    3/ With the .cpp

    #include "Operations_plugin.h"
    
    int Operations_plugin::addition(int a, int b)
    {
        return (a + b);
    }
    
    int Operations_plugin::soustraction(int a, int b)
    {
        return (a - b);
    }
    

    4/ And the Python code

    from PyQt5.QtCore import QPluginLoader
    
    def read_dll_PyQt():
    
        path_dll = "c:/Operations_Plugin/Release/Operations_plugin.dll"
    
        pluginLoader = QPluginLoader()
        pluginLoader.setFileName(path_dll)
        plugin_obj = pluginLoader.instance()
    
        
    # TODO Code for :
    # res = plugin_obj.Operations_Interface.Addition(10,5)
    # ==> How to cast plugin_obj to Operations_Interface ?   
    
    # Main
    if __name__ == '__main__':
        read_dll_PyQt()
    

Log in to reply