使用Shiboken为C++和Qt库创建Python绑定(5)

foo

同前面一样,此处是生成绑定的地方,也是我们最重要的部分,先看CMakeLists.txt文件:

project(foo) set(foo_SRC ${CMAKE_CURRENT_BINARY_DIR}/foo/foo_module_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/foo/math_wrapper.cpp ) set(foo_INCLUDE_DIRECTORIES ${SHIBOKEN_INCLUDE_DIR} ${SHIBOKEN_Python_INCLUDE_DIR} ${PYSIDE_INCLUDE_DIR} ${PYSIDE_INCLUDE_DIR}/QtCore ${libfoo_SOURCE_DIR} ) set(foo_LINK_LIBRARIES ${SHIBOKEN_PYTHON_LIBRARIES} ${SHIBOKEN_LIBRARY} ${PYSIDE_LIBRARY} ${QT_LIBRARIES} libfoo ) include_directories(foo ${foo_INCLUDE_DIRECTORIES}) add_library(foo MODULE ${foo_SRC}) set_property(TARGET foo PROPERTY PREFIX "") if(WIN32) set_property(TARGET foo PROPERTY SUFFIX ".pyd") endif() target_link_libraries(foo ${foo_LINK_LIBRARIES}) add_custom_command(OUTPUT ${foo_SRC} COMMAND ${GENERATOR} --generatorSet=shiboken ${GENERATOR_EXTRA_FLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/global.h --include-paths=${libfoo_SOURCE_DIR}${PATH_SEP}${QT_INCLUDE_DIR}${PATH_SEP}${PYSIDE_INCLUDE_DIR} --output-directory=${CMAKE_CURRENT_BINARY_DIR} --typesystem-paths=${typesystem_path}${PATH_SEP}${PYSIDE_TYPESYSTEMS} ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_foo.xml WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Running generator for libfoo..." )

注意和它前任比较,变复杂的部分:

头文件路径,需要Qt的头文件路径和PySide的头文件路径(注意子路径)

链接库,需要Qt的库和PySide的库

generatorrunner 参数中的 --include-paths,添加Qt和PySide路径

generatorrunner 参数中 --typesystem-paths 添加相应路径

看一下另外两个文件:

global.h #undef QT_NO_STL #undef QT_NO_STL_WCHAR #ifndef NULL #define NULL 0 #endif #include "pyside_global.h" #include "foo.h"

几个宏看不太懂,多了一个pyside_global.h文件,属于PySide自带的文件

typesystem_foo.xml <?xml version="1.0"?> <typesystem package="foo"> <load-typesystem generate="no"/> <object-type /> </typesystem>

需要加载PySide 的 typesystem_core.xml

tests

生成东西能否工作,仍然需要测试来说话:

CMakeLists.txt if(WIN32) set(TEST_PYTHONPATH "${foo_BINARY_DIR};${PYSIDE_PYTHONPATH}") set(TEST_LIBRARY_PATH "${libfoo_BINARY_DIR};$ENV{PATH}") set(LIBRARY_PATH_VAR "PATH") string(REPLACE "\\" "/" TEST_PYTHONPATH "${TEST_PYTHONPATH}") string(REPLACE "\\" "/" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}") string(REPLACE ";" "\\;" TEST_PYTHONPATH "${TEST_PYTHONPATH}") string(REPLACE ";" "\\;" TEST_LIBRARY_PATH "${TEST_LIBRARY_PATH}") else() set(TEST_PYTHONPATH "${foo_BINARY_DIR}:${PYSIDE_PYTHONPATH}") set(TEST_LIBRARY_PATH "${libfoo_BINARY_DIR}:$ENV{LD_LIBRARY_PATH}") set(LIBRARY_PATH_VAR "LD_LIBRARY_PATH") endif() add_test(math ${SHIBOKEN_PYTHON_INTERPRETER} ${CMAKE_CURRENT_SOURCE_DIR}/test_foo.py) set_tests_properties(math PROPERTIES ENVIRONMENT "PYTHONPATH=${TEST_PYTHONPATH};${LIBRARY_PATH_VAR}=${TEST_LIBRARY_PATH}")

恩,基本没变化,就是多了个PYSIDE_PYTHONPATH

至于 test_foo.py 文件,和第一部分完全一样,此处略。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxysw.html