3

I am trying to port (some of) the logic of void QgsPluginManager::getCppPluginsMetadata() from QGIS from C++ to Python.

In C++, the relevant section looks about as follows:

/* `lib` is a path (string) to a QGIS plugin shared object file, e.g. `/lib/qgis/plugins/libtopolplugin.so` */
QLibrary *myLib = new QLibrary( lib );
bool loaded = myLib->load();
if ( !loaded ) { /* handle error */ }
name_t *pName = ( name_t * ) cast_to_fptr( myLib->resolve( "name" ) );
if ( pName ) { QgsDebugMsg( "Plugin name: " + pName() ); }

QLibrary is used to load the shared object file. Once it is loaded, function pointers are resolved, casted to actual functions and then called. In the above example, pName would return the name of the plugin as a string.

In Python, tried the following:

import sys
from PyQt5 import QtCore

lib = '/lib/qgis/plugins/libtopolplugin.so'
myLib = QtCore.QLibrary(lib)

loaded = myLib.load()
if not loaded:
    print("Failed to load library!")
    sys.exit()

print( myLib.resolve( "name" ) )
print( myLib.resolve( "name" )() )

print('The end.')

I got the following output:

<sip.voidptr object at 0x7fa046b214b0>
Traceback (most recent call last):
  File "test_QLibrary.py", line 17, in <module>
    print( myLib.resolve( "name" )() )
TypeError: 'sip.voidptr' object is not callable

It makes sense that I can not call a sip.voidptr directly - I'd need to cast it to a function somehow. Is this even possible (with sip or other tools) in Python? If yes, how?

s-m-e
  • 3,433
  • 2
  • 34
  • 71
  • Just came across `sip.wrapinstance`. Maybe it does the trick somehow ... – s-m-e Apr 19 '20 at 08:25
  • What do you need the return type to be? – xilpex Apr 25 '20 at 17:20
  • @Xilpex The return type of the `name` method is actually a `QString`. PyQt5 automatically maps `QString`s to Python strings (UTF-8), so I'd expect a Python string. – s-m-e Apr 25 '20 at 17:50
  • I don't know if this will help but look at this: https://stackoverflow.com/questions/47689778/how-do-you-go-from-a-sip-voidptr-qimage-constbits-to-a-ctypes-void-or-char-p – xilpex Apr 25 '20 at 17:52
  • What do you get when you add `print( myLib.resolve( "name" ).ascapsule() )`? – ands Apr 28 '20 at 00:20

0 Answers0