Load a .so object with QLibrary from PyQt
-
Hello, I've been using
QLibrarymany times, however now I have a case when I want to load.sofromPyQt. Following their docs it should be done the same way, but since it'spythonI don't follow whyload()returns withFalse. Here is the simple.socode:interface.h:#ifndef INTERFACE_H #define INTERFACE_H #ifdef __cplusplus extern "C" { #endif int init(int argc, char** argv); #ifdef __cplusplus }; #endifI have and a
.cppfile :
interface.cpp#include "interface.h" #include "testwidget.h" #include <QApplication> int init(int argc, char **argv) { QApplication a(argc, argv); MyWidget w; w.init(); return a.exec(); }Assume that
testwidgetis just a 200 x 200 empty window. So here is what is happening in python:import os as UNIX import sys from PyQt5 import QtCore def main(*args, **kwargs): testlib = QtCore.QLibrary('/home/ilian/Qt/build-testpyqtlib-Desktop_Qt_5_8_0_GCC_64bit-Debug/libtestpyqtlib.so') res = testlib.load() if res: print("OK, loaded library!") else: print("Failed to load library!") if __name__ == "__main__": main(sys.argv)Don't fire at me why I am doing it with 'PyQt
, it's something inherited and I have to work with it. Just tell me why this call fails here? Everything is in place as directories. It just does not loads the.so` file.Regards.
#endif // INTERFACE_H
-
Hello, I've been using
QLibrarymany times, however now I have a case when I want to load.sofromPyQt. Following their docs it should be done the same way, but since it'spythonI don't follow whyload()returns withFalse. Here is the simple.socode:interface.h:#ifndef INTERFACE_H #define INTERFACE_H #ifdef __cplusplus extern "C" { #endif int init(int argc, char** argv); #ifdef __cplusplus }; #endifI have and a
.cppfile :
interface.cpp#include "interface.h" #include "testwidget.h" #include <QApplication> int init(int argc, char **argv) { QApplication a(argc, argv); MyWidget w; w.init(); return a.exec(); }Assume that
testwidgetis just a 200 x 200 empty window. So here is what is happening in python:import os as UNIX import sys from PyQt5 import QtCore def main(*args, **kwargs): testlib = QtCore.QLibrary('/home/ilian/Qt/build-testpyqtlib-Desktop_Qt_5_8_0_GCC_64bit-Debug/libtestpyqtlib.so') res = testlib.load() if res: print("OK, loaded library!") else: print("Failed to load library!") if __name__ == "__main__": main(sys.argv)Don't fire at me why I am doing it with 'PyQt
, it's something inherited and I have to work with it. Just tell me why this call fails here? Everything is in place as directories. It just does not loads the.so` file.Regards.
#endif // INTERFACE_H
What does http://doc.qt.io/qt-5.9/qlibrary.html#errorString say after load()?
-
What does http://doc.qt.io/qt-5.9/qlibrary.html#errorString say after load()?
-
@jsulm That is dull now... it loaded. Maybe it was something with the python interpreter setup from the
PyCharmdon't know exactly. -
@ilian In any case you should print out the output of errorString() to get more information next time it fails.
-
@jsulm Are you aware of
PyQt, it resolved myinitfunction with somesip.voidptrthing, that I don't know how to call. Originally I'd get aCfunction pointer but thissip.voidptris not callable.@ilian What init function do you mean? Is it a function from your library? If so then sip.voidptr probably means that it could not resolve the function.
See http://doc.qt.io/qt-5.9/qlibrary.html#resolve, you need to export your functions to be able to resolve them. -
@ilian What init function do you mean? Is it a function from your library? If so then sip.voidptr probably means that it could not resolve the function.
See http://doc.qt.io/qt-5.9/qlibrary.html#resolve, you need to export your functions to be able to resolve them.@jsulm I have an init function
extern "C" int init(int argc, char **argv) { QApplication a(argc, argv); MyWidget w; w.init(); return a.exec(); }resolve()returns a non null value, which issip.voidptr, and I am expecting this to be aCfunction pointer but, I have no idea how to call it. -
@jsulm I have an init function
extern "C" int init(int argc, char **argv) { QApplication a(argc, argv); MyWidget w; w.init(); return a.exec(); }resolve()returns a non null value, which issip.voidptr, and I am expecting this to be aCfunction pointer but, I have no idea how to call it. -
@ilian Please take a look at the link I posted before: you need to export your function, else it cannot be resolved.
You are aware that a.exec() will block until you close your app? That means init() call will block.
-
@jsulm I am aware yes, removed it, and just show the widget. However, I did the example with MY_EXPORT macro, I still get this voidptr. I will test in C++ program to see if there everything is OK.
-
@ilian This void pointer is valid, but I don't know how to use it if it points to a function. Never did this with PyQt.
@jsulm I've made a similar C++ program with the function pointer, and it does what I want, open a widget and enters it's event loop. In short, as C++ program, loading the library everything is fine, as PyQt - it's not, or the reslove form PyQt and that sip.voidptr are some weird stuff, we know not of.
-
@jsulm I've made a similar C++ program with the function pointer, and it does what I want, open a widget and enters it's event loop. In short, as C++ program, loading the library everything is fine, as PyQt - it's not, or the reslove form PyQt and that sip.voidptr are some weird stuff, we know not of.
@ilian said in Load a .so object with QLibrary from PyQt:
sip.voidptr
this is not weird stuff :-)
It is documented, I just don't know how to use it if it contains a function pointer - I could not find anything in SIP documentation for this use case. -
@ilian said in Load a .so object with QLibrary from PyQt:
sip.voidptr
this is not weird stuff :-)
It is documented, I just don't know how to use it if it contains a function pointer - I could not find anything in SIP documentation for this use case.