PySide6.QtCore.QLibrary¶
- class QLibrary¶
- The - QLibraryclass loads shared libraries at runtime. More…- Synopsis¶- Properties¶- fileNameᅟ- The file name of the library
- loadHintsᅟ- Give the load() function some hints on how it should behave
 - Methods¶- def - __init__()
- def - errorString()
- def - fileName()
- def - isLoaded()
- def - load()
- def - loadHints()
- def - resolve()
- def - setFileName()
- def - setLoadHints()
- def - unload()
 - Static functions¶- def - isLibrary()
- def - resolve()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - An instance of a - QLibraryobject operates on a single shared object file (which we call a “library”, but is also known as a “DLL”). A- QLibraryprovides access to the functionality in the library in a platform independent way. You can either pass a file name in the constructor, or set it explicitly with- setFileName(). When loading the library,- QLibrarysearches in all the system-specific library locations (e.g.- LD_LIBRARY_PATHon Unix), unless the file name has an absolute path.- If the file name is an absolute path then an attempt is made to load this path first. If the file cannot be found, - QLibrarytries the name with different platform-specific file prefixes, like “lib” on Unix and Mac, and suffixes, like “.so” on Unix, “.dylib” on the Mac, or “.dll” on Windows.- If the file path is not absolute then - QLibrarymodifies the search order to try the system-specific prefixes and suffixes first, followed by the file path specified.- This makes it possible to specify shared libraries that are only identified by their basename (i.e. without their suffix), so the same code will work on different operating systems yet still minimise the number of attempts to find the library. - The most important functions are - load()to dynamically load the library file,- isLoaded()to check whether loading was successful, and- resolve()to resolve a symbol in the library. The- resolve()function implicitly tries to load the library if it has not been loaded yet. Multiple instances of- QLibrarycan be used to access the same physical library. Once loaded, libraries remain in memory until the application terminates. You can attempt to unload a library using- unload(), but if other instances of- QLibraryare using the same library, the call will fail, and unloading will only happen when every instance has called- unload().- A typical use of - QLibraryis to resolve an exported symbol in a library, and to call the C function that this symbol represents. This is called “explicit linking” in contrast to “implicit linking”, which is done by the link step in the build process when linking an executable against a library.- The following code snippet loads a library, resolves the symbol “mysymbol”, and calls the function if everything succeeded. If something goes wrong, e.g. the library file does not exist or the symbol is not defined, the function pointer will be - Noneand won’t be called.- myLib = QLibrary("mylib") void = typedef(MyPrototype)() myFunction = (MyPrototype) myLib.resolve("mysymbol") if myFunction: myFunction() - The symbol must be exported as a C function from the library for - resolve()to work. This means that the function must be wrapped in an- extern "C"block if the library is compiled with a C++ compiler. On Windows, this also requires the use of a- dllexportmacro; see- resolve()for the details of how this is done. For convenience, there is a static- resolve()function which you can use if you just want to call a function in a library without explicitly loading the library first:- void = typedef(MyPrototype)() myFunction = (MyPrototype) QLibrary.resolve("mylib", "mysymbol") if myFunction: myFunction() - See also - class LoadHint¶
- (inherits - enum.Flag) This enum describes the possible hints that can be used to change the way libraries are handled when they are loaded. These values indicate how symbols are resolved when libraries are loaded, and are specified using the- setLoadHints()function.- Constant - Description - QLibrary.ResolveAllSymbolsHint - Causes all symbols in a library to be resolved when it is loaded, not simply when - resolve()is called.- QLibrary.ExportExternalSymbolsHint - Exports unresolved and external symbols in the library so that they can be resolved in other dynamically-loaded libraries loaded later. - QLibrary.LoadArchiveMemberHint - Allows the file name of the library to specify a particular object file within an archive file. If this hint is given, the filename of the library consists of a path, which is a reference to an archive file, followed by a reference to the archive member. - QLibrary.PreventUnloadHint - Prevents the library from being unloaded from the address space if close() is called. The library’s static variables are not reinitialized if open() is called at a later time. - QLibrary.DeepBindHint - Instructs the linker to prefer definitions in the loaded library over exported definitions in the loading application when resolving external symbols in the loaded library. This option is only supported on Linux. - See also 
 - class LoadStatusTag¶
 - Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property fileNameᅟ: str¶
 - This property holds the file name of the library. - We recommend omitting the file’s suffix in the file name, since - QLibrarywill automatically look for the file with the appropriate suffix (see- isLibrary()).- When loading the library, - QLibrarysearches in all system-specific library locations (for example,- LD_LIBRARY_PATHon Unix), unless the file name has an absolute path. After loading the library successfully, fileName() returns the fully-qualified file name of the library, including the full path to the library if one was given in the constructor or passed to setFileName().- For example, after successfully loading the “GL” library on Unix platforms, fileName() will return “libGL.so”. If the file name was originally passed as “/usr/lib/libGL”, fileName() will return “/usr/lib/libGL.so”. - Access functions:
 - property loadHintsᅟ: Combination of QLibrary.LoadHint¶
 - This property holds Give the - load()function some hints on how it should behave..- You can give some hints on how the symbols are resolved. Usually, the symbols are not resolved at load time, but resolved lazily, (that is, when - resolve()is called). If you set the loadHints to- ResolveAllSymbolsHint, then all symbols will be resolved at load time if the platform supports it.- Setting - ExportExternalSymbolsHintwill make the external symbols in the library available for resolution in subsequent loaded libraries.- If - LoadArchiveMemberHintis set, the file name is composed of two components: A path which is a reference to an archive file followed by the second component which is the reference to the archive member. For instance, the- fileName- libGL.a(shr_64.o)will refer to the library- shr_64.oin the archive file named- libGL.a. This is only supported on the AIX platform.- The interpretation of the load hints is platform dependent, and if you use it you are probably making some assumptions on which platform you are compiling for, so use them only if you understand the consequences of them. - By default, none of these flags are set, so libraries will be loaded with lazy symbol resolution, and will not export external symbols for resolution in other dynamically-loaded libraries. - Note - Hints can only be cleared when this object is not associated with a file. Hints can only be added once the file name is set ( - hintswill be or’ed with the old hints).- Note - Setting this property after the library has been loaded has no effect and loadHints() will not reflect those changes. - Note - This property is shared among all - QLibraryinstances that refer to the same library.- Access functions:
 - Constructs a library with the given - parent.- __init__(fileName[, parent=None])
- Parameters:
- fileName – str 
- parent – - QObject
 
 
 - Constructs a library object with the given - parentthat will load the library specified by- fileName.- We recommend omitting the file’s suffix in - fileName, since- QLibrarywill automatically look for the file with the appropriate suffix in accordance with the platform, e.g. “.so” on Unix, “.dylib” on macOS and iOS, and “.dll” on Windows. (See- fileName.)- __init__(fileName, version[, parent=None])
- Parameters:
- fileName – str 
- version – str 
- parent – - QObject
 
 
 - Constructs a library object with the given - parentthat will load the library specified by- fileNameand full version number- version. Currently, the version number is ignored on Windows.- We recommend omitting the file’s suffix in - fileName, since- QLibrarywill automatically look for the file with the appropriate suffix in accordance with the platform, e.g. “.so” on Unix, “.dylib” on macOS and iOS, and “.dll” on Windows. (See- fileName.)- __init__(fileName, verNum[, parent=None])
- Parameters:
- fileName – str 
- verNum – int 
- parent – - QObject
 
 
 - Constructs a library object with the given - parentthat will load the library specified by- fileNameand major version number- verNum. Currently, the version number is ignored on Windows.- We recommend omitting the file’s suffix in - fileName, since- QLibrarywill automatically look for the file with the appropriate suffix in accordance with the platform, e.g. “.so” on Unix, “.dylib” on macOS and iOS, and “.dll” on Windows. (See- fileName.)- errorString()¶
- Return type:
- str 
 
 - Returns a text string with the description of the last error that occurred. Currently, errorString will only be set if - load(),- unload()or- resolve()for some reason fails.- fileName()¶
- Return type:
- str 
 - See also 
 - Getter of property - fileNameᅟ.- static isLibrary(fileName)¶
- Parameters:
- fileName – str 
- Return type:
- bool 
 
 - Returns - trueif- fileNamehas a valid suffix for a loadable library; otherwise returns- false.- Platform - Valid suffixes - Windows - .dll,- .DLL- Unix/Linux - .so- AIX - .a- HP-UX - .sl,- .so(HP-UXi)- macOS and iOS - .dylib,- .bundle,- .so- Trailing versioning numbers on Unix are ignored. - isLoaded()¶
- Return type:
- bool 
 
 - Returns - trueif- load()succeeded; otherwise returns- false.- Note - Prior to Qt 6.6, this function would return - trueeven without a call to- load()if another- QLibraryobject on the same library had caused it to be loaded.- See also - load()¶
- Return type:
- bool 
 
 - Loads the library and returns - trueif the library was loaded successfully; otherwise returns- false. Since- resolve()always calls this function before resolving any symbols it is not necessary to call it explicitly. In some situations you might want the library loaded in advance, in which case you would use this function.- See also - Getter of property - loadHintsᅟ.- resolve(symbol)¶
- Parameters:
- symbol – str 
- Return type:
- QFunctionPointer
 
 - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Returns the address of the exported symbol - symbol. The library is loaded if necessary. The function returns- Noneif the symbol could not be resolved or if the library could not be loaded.- Example: - typedef int (AvgFunction)(int, int) avg = (AvgFunction) library.resolve("avg") if avg: return avg(5, 8) else: return -1 - The symbol must be exported as a C function from the library. This means that the function must be wrapped in an - extern "C"if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the- __declspec(dllexport)compiler directive, for example:- extern "C" MY_EXPORT int avg(int a, int b) return (a + b) / 2 - with - MY_EXPORTdefined as- #ifdef Q_OS_WIN #define MY_EXPORT __declspec(dllexport) #else #define MY_EXPORT #endif - static resolve(fileName, symbol)
- Parameters:
- fileName – str 
- symbol – str 
 
- Return type:
- QFunctionPointer
 
 - This is an overloaded function. - Loads the library - fileNameand returns the address of the exported symbol- symbol. Note that- fileNameshould not include the platform-specific file suffix; (see- fileName). The library remains loaded until the application exits.- The function returns - Noneif the symbol could not be resolved or if the library could not be loaded.- See also - static resolve(fileName, version, symbol)
- Parameters:
- fileName – str 
- version – str 
- symbol – str 
 
- Return type:
- QFunctionPointer
 
 - This is an overloaded function. - Loads the library - fileNamewith full version number- versionand returns the address of the exported symbol- symbol. Note that- fileNameshould not include the platform-specific file suffix; (see- fileName). The library remains loaded until the application exits.- versionis ignored on Windows.- The function returns - Noneif the symbol could not be resolved or if the library could not be loaded.- See also - static resolve(fileName, verNum, symbol)
- Parameters:
- fileName – str 
- verNum – int 
- symbol – str 
 
- Return type:
- QFunctionPointer
 
 - This is an overloaded function. - Loads the library - fileNamewith major version number- verNumand returns the address of the exported symbol- symbol. Note that- fileNameshould not include the platform-specific file suffix; (see- fileName). The library remains loaded until the application exits.- verNumis ignored on Windows.- The function returns - Noneif the symbol could not be resolved or if the library could not be loaded.- See also - setFileName(fileName)¶
- Parameters:
- fileName – str 
 - See also 
 - Setter of property - fileNameᅟ.- setFileNameAndVersion(fileName, version)¶
- Parameters:
- fileName – str 
- version – str 
 
 
 - Sets the - fileNameproperty and full version number to- fileNameand- versionrespectively. The- versionparameter is ignored on Windows.- See also - setFileNameAndVersion(fileName, verNum)
- Parameters:
- fileName – str 
- verNum – int 
 
 
 - Sets the - fileNameproperty and major version number to- fileNameand- versionNumberrespectively. The- versionNumberis ignored on Windows.- See also - Setter of property - loadHintsᅟ.- unload()¶
- Return type:
- bool 
 
 - Unloads the library and returns - trueif the library could be unloaded; otherwise returns- false.- This happens automatically on application termination, so you shouldn’t normally need to call this function. - If other instances of - QLibraryare using the same library, the call will fail, and unloading will only happen when every instance has called unload().- Note that on macOS, dynamic libraries cannot be unloaded. QLibrary::unload() will return - true, but the library will remain loaded into the process.