运行 C++通过 boost python 模块的 Qt 应用程序

发布于 2024-11-29 09:19:41 字数 869 浏览 1 评论 0原文

是否可以通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-12-06 09:19:42

您可以在 PyQt 中编写应用程序设置,这就像

import sys
from PyQt4 import QtGui    
QtGui.QApplication(sys.argv)

脚本开头一样简单。然后可以在模块中调用可以打开/关闭/...窗口的 C++ 代码。 (我有这样工作的代码)

我认为在c++中调用main是非法的,这可能是段错误的原因。

You can write your application setup in PyQt, which is as simple as

import sys
from PyQt4 import QtGui    
QtGui.QApplication(sys.argv)

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.

谁与争疯 2024-12-06 09:19:42

通常也会调用 main

argc == 1

嗯,即使没有参数,

argc[0] 是可执行文件名称。此外,argv 预计是一个指向以空指针结尾的字符串的指针列表,而您不传递任何内容。根据 QApplication 解析参数列表的方式(它可能根据 argc 进行循环,或者可能只是查找空指针),如果传递的 argc 为零,它可能会崩溃。
尝试

char *args[1] = { NULL }; main(0, args);

char *args[2] = { "Dummy name", NULL }; main(1, args);

Hmm usually main is called with

argc == 1

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

char *args[1] = { NULL }; main(0, args);

or

char *args[2] = { "Dummy name", NULL }; main(1, args);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文