运行 C++通过 boost python 模块的 Qt 应用程序
是否可以通过 python 将 Qt gui 应用程序作为 boost 模块运行?它曾作为标准 C++ 可执行文件工作,但现在我将其编译为共享库并尝试从 python 启动它。现在,每次我从解释器运行 simpleMain() 时,它都会进入 python 解释器。例如,我每次都会收到新的“Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)”问候语,当我关闭解释器时,我的程序会出现段错误。另外,我无法直接调用 main 函数,因为我不确定如何将 python 列表转换为 char*。字符串到 char 似乎很自然地工作。
这是我启动它的 python 代码:
import libsunshine
libsunshine.simpleMain()
这是我的 C++ 代码:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(libsunshine)
{
def("say_hello", say_hello);
def("simpleMain", simpleMain);
def("main", main);
}
int simpleMain()
{
char* args[] = {};
main(0,args);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Sunshine w;
w.show();
return a.exec();
}
Is it possible to run a Qt gui application as a boost module through python? It was working as a standard C++ executable, but now I'm compiling it down to a shared library and trying to launch it from python. Right now it just goes into the python interpreter every time I run simpleMain() from the interpreter. As in, I get a new "Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)" greetings every time and my program segfaults when I close the interpreter. Also, I can't call the main function directly because I'm not sure how to convert a python list to a char*. A string to char seems to wrok naturally.
This is my python code to launch it:
import libsunshine
libsunshine.simpleMain()
and here's my C++ code:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(libsunshine)
{
def("say_hello", say_hello);
def("simpleMain", simpleMain);
def("main", main);
}
int simpleMain()
{
char* args[] = {};
main(0,args);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Sunshine w;
w.show();
return a.exec();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 PyQt 中编写应用程序设置,这就像
脚本开头一样简单。然后可以在模块中调用可以打开/关闭/...窗口的 C++ 代码。 (我有这样工作的代码)
我认为在c++中调用main是非法的,这可能是段错误的原因。
You can write your application setup in PyQt, which is as simple as
at the beginning of your script. Then can call c++ code in your modules which can open/close/... windows. (I have code which works this way)
I think it is illegal to call main in c++, which might be perhaps the reason for segfault.
通常也会调用 main
嗯,即使没有参数,
,
argc[0]
是可执行文件名称。此外,argv
预计是一个指向以空指针结尾的字符串的指针列表,而您不传递任何内容。根据 QApplication 解析参数列表的方式(它可能根据 argc 进行循环,或者可能只是查找空指针),如果传递的 argc 为零,它可能会崩溃。尝试
或
Hmm usually main is called with
even when there are no params,
argc[0]
being the executable name.Also
argv
is expected to be a list of pointers to strings terminated with a null pointer, while you're passing nothing. Depending on how QApplication parses the argument list (it may loop depending on argc, or it may just look for a null pointer), it can crash if passed even an argc of zero.Try
or