我想知道是否可以在 PyQt 和 Boost.Python 之间共享小部件。
我将在我的使用 Qt 的应用程序中嵌入一个 Python 解释器。我希望我的应用程序的用户能够将他们自己的 UI 小部件嵌入到用 C++ 编程并通过 Boost.Python 公开的 UI 小部件中。
这是可能的,人们将如何去做?
我有一些 C++ 方法std::set<std::string>作为参数或返回值。我想将其映射到 Python frozenset(或常规set),但似乎没有一种直接的方法可以做到这一点。有谁知道如何完成这项任务。
我应该按值还是按 const 引用将 boost::python::object 对象传递给 C++ 函数?我几乎总是通过 const 引用传递 C++ 中的重要对象。然而,在 Boost Python 文档示例中,它们总是按值传递 boost::python::object 。所以我想知道这背后是否有原因,或者他们这样做只是为了让文本更容易阅读或其他什么。
我在 Visual Studio 2005 中遇到了一个错误。我使用boost python(boost_1_54_0) 将 python 3.3.2 连接到 c++。我遵循了这个教程。
http://www.thalesians.com/finance/index.php/Knowledge_Base/CPP/Boost_Python_Step_By_Step。
以及来自 StackOverflow 的问题
Visual Studio:LINK:致命错误 LNK1181:无法打开输入文件
我在链接器输入中包含了所需的库,但仍然收到此链接错误:
fatal error LNK1181: cannot open input file 'C:\Python33\Lib.obj'
Run Code Online (Sandbox Code Playgroud) 下面是 Boost.Python源代码的一个片段:
template <class T> struct null_ok; // how's it working?
template <class T>
inline null_ok<T>* allow_null(T* p)
{
return (null_ok<T>*)p;
}
Run Code Online (Sandbox Code Playgroud)
可以确定的是,前向声明的 struct 没有定义null_ok,并且null_ok与模板参数无关T。
在Python wiki 中,给出了一些提示:
handle<> y(null_ok(x))允许 y 变为 NULL
handle<> y(x),其中 x 不是 null_ok 的结果,永远不会导致 NULL y。如果 x 为 NULL 将抛出异常
我无法弄清楚结构模板的声明(没有定义)如何null_ok达到上述目的?
I'm writing a python program that includes a c++ module (.so, using boost.python).
I'm starting several python threads that run a c++ function.
This is how the C++ code looks like:
#include <boost/python.hpp>
using namespace boost;
void f(){
// long calculation
// call python function
// long calculation
}
BOOST_PYTHON_MODULE(test)
{
python::def("f", &f);
}
Run Code Online (Sandbox Code Playgroud)
And the python code:
from test import f
t1 = threading.Thread(target=f)
t1.setDaemon(True)
t1.start()
print "Still running!"
Run Code Online (Sandbox Code Playgroud)
I encounter a problem: the "Still running!" …
我在名为 cpp_examples 的文件夹中有以下文件。
#include <boost/python.hpp>
#include <string>
const std::string hello() {
return std::string("hello, zoo");
}
BOOST_PYTHON_MODULE(zoo) {
// An established convention for using boost.python.
using namespace boost::python;
def("hello", hello);
}
Run Code Online (Sandbox Code Playgroud)
我运行以下命令进行编译。
sumith@rztl516-Lenovo-G575:~/cpp_examples$ g++ zoo.cpp -I/usr/include/python2.7 -I/usr/lib/x86_64-linux-gnu/ -lboost_python -lpython2.7 -o zoo.so -shared -fPIC
Run Code Online (Sandbox Code Playgroud)
它被编译并给了我一个 zoo.so 文件。当我尝试在同一个文件夹中导入和运行 zoo.hello() 时,它可以工作,但没有在 cpp_examples 文件夹外导入
sumith@rztl516-Lenovo-G575:~/cpp_examples$ python2
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zoo
>>> zoo.hello()
'hello, zoo'
>>> exit() …Run Code Online (Sandbox Code Playgroud) 目前,我已经编写了一个Makefile,它读取ldflagsusing pkg-config。
pkg-config --libs python3
Run Code Online (Sandbox Code Playgroud)
现在,我已经找到了python3-config --ldflags,python3-config --libs在我看来哪个更好。
对我来说仍然存在的问题是,我也在使用 boost_python 并且我在我的Makefile. IE -lboost_python-py35。这样做的问题是,如果其他人使用它Makefile,python3.4他们必须将其更改为-lboost_python-py34。那么,这是否可以以某种方式变得动态化,python3-config并pkg-config允许我们做类似的事情。
我有一个抽象类Shape(来自 C++ 代码),并且我为它做了一个包装器,这样我就可以在 Python 中继承这个抽象类。除了一些内存管理问题之外,一切都运行良好。Python 对象被创建和销毁,而ShapeWrapperC++ 对象仍然存在。基本上,对象被销毁是因为Python中没有对它的引用,但在C++中存在对包装类的引用,所以我的想法是该类会wrapper增加初始化时的引用计数,并在C++时减少析构函数中的计数包装类ShapeWrap被破坏。但似乎这并没有发生在这里。这是一些代码:
类Shape(C++ 中):
struct Shape {
virtual inline string get_name() const = 0;
};
Run Code Online (Sandbox Code Playgroud)
班上ShapeWrap:
struct ShapeWrap : public Shape, public wrapper<Shape> {
string get_name() const override {
if (auto py_func = this->get-override("get_name")) {
return py_func();
}
return "Default String";
}
}
Run Code Online (Sandbox Code Playgroud)
我这样定义类:
class_<ShapeWrap, shared_ptr<ShapeWrap>, boost::noncopyable>("Shape")
.def("get_name", pure_virtual(&Shape::get_name));
Run Code Online (Sandbox Code Playgroud)
现在我这里有一个小函数来获取一个形状并打印出它的名称:
void print_name(shared_ptr<ShapeWrap> shape) {
cout << shape->get_name() << endl;
}
Run Code Online (Sandbox Code Playgroud)
在 Python 领域我这样做: …
我正在使用 Boost Python 为 Linux 和 Windows (Visual Studio) 开发 C++ 库的 Python 绑定。
在 Windows 中,静态 Boost Python 库依赖于 Python(这是另一个线程的动机,此处),因此,在我的 CMake 配置中,我需要执行以下操作:
if((${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR APPLE)
target_link_libraries(my_python_module ${Boost_LIBRARIES})
elseif(WIN32 AND MSVC)
add_definitions(/DBOOST_PYTHON_STATIC_LIB)
target_link_libraries(my_python_module ${Boost_LIBRARIES}) #This includes the Boost Python library
# Even though Boost Python library is included statically, in Windows it has a dependency to the Python library.
target_link_libraries(my_python_module ${Python_LIBRARIES})
endif()
Run Code Online (Sandbox Code Playgroud)
这在 Linux 中工作得很好,但在 Windows 中,它只能在发布模式下工作,而不能在调试模式下工作,在这种情况下我总是得到:
LINK : fatal error LNK1104: Can't open file 'python37.lib' …
boost-python ×10
c++ ×7
python ×7
boost ×4
cmake ×1
gil ×1
makefile ×1
pyqt ×1
python-3.x ×1
python-sip ×1
qt ×1
templates ×1