Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide2 / PyQT5 : Using QPluginLoader to import a QT C++ plugin

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bob40
    wrote on last edited by
    #1

    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

    jazzycamelJ 1 Reply Last reply
    0
    • B bob40

      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

      jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by jazzycamel
      #2

      @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)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • B Offline
        B Offline
        bob40
        wrote on last edited by
        #3

        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()
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved